C#实践炸飞机socket通信( 二 )

2. 其次在连接妥当之后,必须进行信息传输,如果而现在假定客户端时先手,则要进行 send() 函数的调用,在函数中你可以发送任意的数据,但必须时btye数组(因为在物理层传输数据是发送的是比特,发送到对方物理层会进行解析还原,但是这些东西C#的Socket类已经封装好了,我们调用接口即可),需要注意的是在使用 send() 的时候必须调用(这个之后再详细说);
public void Send(int i,int x,int y) //这里面的参数 i,x,y 的含义分别是 模式0/1,x坐标, y坐标,可以根据需求改变{string str =Convert.ToString(i)+Convert.ToString(x) + Convert.ToString(y);//将数字转化为string类型字符串byte[] Btye = Encoding.UTF8.GetBytes(str);//将刚刚转化好的string类型字符串转化为byte类型数组ClientSocket.Send(Btye);//调用Socket类中的Send()函数发送数据if (str[0] == '0')//判断模式0/1,在己方控制台显示己方发送过去的内容,方便自己查看{Console.WriteLine($"已发送轰炸位置 {str}");}else if (str[0] == '1'){Console.WriteLine($"已发送对方轰炸位置结果{str}");}}3. 最后阐述一下 receive() 函数,再对方(服务器端)接收到你发送的信息之后,一定会返回一个信息(因为下棋是交互的嘛),这时候你便需要一个接收函数 receive() ,这个函数是用来接受对方发送的信息的,但是需要注意的是这个函数会随着你的进程一直运行,在from中是不需要调用的 。
public void Recive(){while (true)//因为是一直在另一个进程中运行,所以给一个死循环{byte[] Btye = new byte[1024];//接收也是byte数组的ClientSocket.Receive(Btye);receivestr = Encoding.UTF8.GetString(Btye,0,3);//转化为string类型if (receivestr[0] == '0')//判断模式0/1,在己方控制台显示对方发送过来的内容,方便查看对方信息{Console.WriteLine($"接受对方了轰炸位置{receivestr}");}else if(receivestr[0]=='1'){Console.WriteLine($"接受轰炸位置结果{receivestr}");}}}服务器类代码1.主体代码部分using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Net.Sockets;using System.Net;using System.Threading;namespace TestBoom{class Server{public String receivestr = null;private Socket SocketWatch;private Socket SocketSend;private static Server server = null;private Server(string ip1, int port1){SocketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);Init(ip1,port1);}public static Server serversocket(string ip1, int port1){if (server == null){server = new Server(ip1, port1);}return server;}private void Init(string ip1, int port1){SocketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);IPEndPoint iPEnd = new IPEndPoint(IPAddress.Parse(ip1), port1);SocketWatch.Bind(iPEnd);SocketWatch.Listen(1);System.Windows.Forms.MessageBox.Show("开始监听...");Thread thread = new Thread(Listen);thread.IsBackground = true;thread.Start();}void Listen(){while (SocketSend==null){SocketSend = SocketWatch.Accept();}System.Windows.Forms.MessageBox.Show("连接成功..." + SocketSend.RemoteEndPoint.ToString());Thread reciveThread = new Thread(Recive);reciveThread.IsBackground = true;reciveThread.Start();}public void Recive(){while (true){byte[] buffer = new byte[1024];SocketSend.Receive(buffer);receivestr = Encoding.UTF8.GetString(buffer, 0, 3);if (receivestr[0] == '0'){Console.WriteLine($"接受对方了轰炸位置{receivestr}");}else if (receivestr[0] == '1'){Console.WriteLine($"接受我方轰炸位置结果{receivestr}");}}}public void Send(int i,int x,int y){string str = Convert.ToString(i) + Convert.ToString(x) + Convert.ToString(y);byte[] buffer = Encoding.UTF8.GetBytes(str);SocketSend.Send(buffer);if (str[0] == '0'){Console.WriteLine($"已发送轰炸位置 {str}");}else if (str[0] == '1'){Console.WriteLine($"已发送对方轰炸位置结果{str}");}}}}

经验总结扩展阅读