unity游戏开发-C#语言基础篇(SocketServer与SocketClient)

namespace SocketServer
{
    class Program
    {
        public  const string _ip = "192.168.31.105";
        public  const int port = 4747;
        static void Main(string[] args)
        {
            Socket serverSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            IPAddress add = IPAddress.Parse(_ip);
            IPEndPoint ipend = new IPEndPoint(add,port);
            serverSocket.Bind(ipend);
            serverSocket.Listen(10);
            Socket socketSend = serverSocket.Accept();
     

            string str = "";
            byte[] strByte = new byte[1024];
            int strLength = socketSend.Receive(strByte,strByte.Length,0);
            str = Encoding.UTF8.GetString(strByte,0,strLength);
            Console.WriteLine(str);


            string str1 = "你好.欢迎来到地球";
            byte[] strByte1 = Encoding.UTF8.GetBytes(str1);
            socketSend.Send(strByte1);

            



            Console.ReadKey();
        }
    }
}

namespace SocketClient
{
    class Program
    {
        public  const string _ip = "192.168.31.105";
        public  const int port=4747;
        static void Main(string[] args)
        {
            Socket clientSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            IPAddress add = IPAddress.Parse(_ip);
            IPEndPoint ipend = new IPEndPoint(add,port);
            clientSocket.Connect(ipend);

            string str = "你好,世界!";

            byte[] strByte = Encoding.UTF8.GetBytes(str);
            clientSocket.Send(strByte);


            string str1 = "";
            byte[] strByte1 = new byte[1024];
            int strLength1 = clientSocket.Receive(strByte1,strByte1.Length,0);
            str1 = Encoding.UTF8.GetString(strByte1,0,strByte1.Length);



            Console.WriteLine(str1);
            Console.ReadKey();
        }
    }
}

你可能感兴趣的:(unity游戏开发-C#语言基础篇(SocketServer与SocketClient))