zoukankan      html  css  js  c++  java
  • 网络游戏_客户端

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net.Sockets;
    using System.Net;

    namespace TCP客户端
    {
        class Program
        {
            static void Main(string[] args)
            {
                Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                clientSocket.Connect(new IPEndPoint(IPAddress.Parse("192.168.43.231"), 7788));
                //接收消息
                byte[] data = new byte[1024];
                int count= clientSocket.Receive(data);
                string message = Encoding.UTF8.GetString(data, 0, count);
                Console.WriteLine(message);
                //发送消息
                while (true)
                {
                    string messageSend = Console.ReadLine();    
                    //Console.Write(messageSend);
                    clientSocket.Send(Encoding.UTF8.GetBytes(messageSend));
                    if (messageSend == "c")
                    {
                        clientSocket.Close();
                        return;
                    }
                    clientSocket.Send(Encoding.UTF8.GetBytes(messageSend));

                }


                Console.ReadKey();
                clientSocket.Close();
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace TCP客户端
    {
        class Message
        {
            public static byte[] GetBytes(string data)
            {
                byte[] dataBytes = Encoding.UTF8.GetBytes(data);
                int dataLength = dataBytes.Length;
                byte[] lengthBytes = BitConverter.GetBytes(dataLength);
                byte[] newBytes = lengthBytes.Concat(dataBytes).ToArray();
                return newBytes;
            }
        }
    }

  • 相关阅读:
    经典笔试题:用C写一个函数测试当前机器大小端模式
    一个结构体传递方式的问题
    ESP8266 NON-OS SDK 和 RTOS SDK实现GPIO中断不同点
    关于C语言中内存的3个问题
    ESP8266 station模式下建立client、server TCP连接
    连续更新了42天早报之后
    简单socket()编程
    TCP协议学习
    linux系统如何管理文件
    Linux文件操作的主要接口API及相关细节
  • 原文地址:https://www.cnblogs.com/shirln/p/7874427.html
Copyright © 2011-2022 走看看