Unity网游开发2-开发服务器端的发送数据和接收数据

创建 一个控制台

static void Main(string【 】 args)

 {

//声明服务器Socket,绑定端口号ip,

   Socket serverSocket=new Socket(AddressFamily.InterNetWork,SocketType.Stream,ProtocolType.Tcp);

 IPAddress ipAddress=IPAddress.Parse("192.168.1.5");

IPEndPoint ipEndPoint=new IPEndPoint(ipAddress ,88);

serverSocket.Bind(ipEndPoint);

//开始监听客户端

serverSocket.Listen(0) 

//接受到的客户端

Socket clientSocket =serverSocket.Accept();


string msg="Hello client!你好";

byte[] data =System.Text.Encoding.UTF.8.GetBytes(msg);

//向客户端发送数据

clientSocket.Send(data);

//接受客户端的信息

byte[] dataBuffer =new byte[1024];

int count=clientSocket.Receive(dataBuffer);

string msgReceive=System.Text.Encoding.UTF8.GetString(dataBuffer,0,count);

Console.WriteLine(msgReceive);

Console.ReadKey();

//关闭客户端连接和服务端

clientSocket.Close();

serverSocket.Close();





}  

你可能感兴趣的:(网游开发)