Socket 英文直译为“孔或插座”,也称为套接字。用于描述 IP 地址和端口号,是一种进程间的通讯机制。你能够理解为 IP 地址肯定了网内的惟一计算机,而端口号则指定了将消息发送给哪个应用程序(大多应用程序启动时会主动绑定一个端口,若是不主动绑定,操做系统自动为其分配一个端口)。 数组
一台主机通常运行了多个软件并同时提供一些服务。每种服务都会打开一个 Socket,并绑定到一个端口号上,不一样端口对应于不一样的应用程序。例如 http 使用 80 端口;ftp 使用 21 端口;smtp 使用 23 端口。 安全
上述过程就像是实现了一次三方会谈。服务器端的 Socket 至少会有 2 个。一个是 Watch Socket,每成功接收到一个客户端的链接,便在服务器端建立一个通讯 Socket。客户端 Socket 指定要链接的服务器端地址和端口,建立一个 Socket 对象来初始化一个到服务器的 TCP 链接。 服务器
下面就看一个最简单的 Socket 示例,实现了网络聊天通讯的雏形。 网络
服务器端:socket
public partial class ChatServer : Form { public ChatServer() { InitializeComponent(); ListBox.CheckForIllegalCrossThreadCalls = false; } /// <summary> /// 监听 Socket 运行的线程 /// </summary> Thread threadWatch = null; /// <summary> /// 监听 Socket /// </summary> Socket socketWatch = null; /// <summary> /// 服务器端通讯套接字集合 /// 必须在每次客户端链接成功以后,保存新建的通信套接字,这样才能和后续的全部客户端通讯 /// </summary> Dictionary<string, Socket> dictCommunication = new Dictionary<string, Socket>(); /// <summary> /// 通讯线程的集合,用来接收客户端发送的信息 /// </summary> Dictionary<string, Thread> dictThread = new Dictionary<string, Thread>(); private void btnBeginListen_Click(object sender, EventArgs e) { // 建立服务器端监听 Socket (IP4寻址协议,流式链接,TCP协议传输数据) socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // 监听套接字绑定指定端口 IPAddress address = IPAddress.Parse(txtIP.Text.Trim()); IPEndPoint endPoint = new IPEndPoint(address, int.Parse(txtPort.Text.Trim())); socketWatch.Bind(endPoint); // 将监听套接字置于侦听状态,并设置链接队列的最大长度 socketWatch.Listen(20); // 启动监听线程开始监听客户端请求 threadWatch = new Thread(Watch); threadWatch.IsBackground = true; threadWatch.Start(); ShowMsg("服务器启动完成!"); } Socket socketCommunication = null; private void Watch() { while (true) { // Accept() 会建立新的通讯 Socket,且会阻断当前线程,所以应置于非主线程上使用 // Accept() 与线程上接受的委托类型不符,所以需另建一方法作桥接 socketCommunication = socketWatch.Accept(); // 将新建的通讯套接字存入集合中,以便服务器随时能够向指定客户端发送消息 // 如不置于集合中,每次 new 出的通讯线程都是一个新的套接字,那么原套接字将失去引用 dictCommunication.Add(socketCommunication.RemoteEndPoint.ToString(), socketCommunication); lbSocketOnline.Items.Add(socketCommunication.RemoteEndPoint.ToString()); // Receive 也是一个阻塞方法,不能直接运行在 Watch 中,不然监听线程会阻塞 // 另外,将每个通讯线程存入集合,方便从此的管理(如关闭、或挂起) Thread thread = new Thread(() => { while (true) { byte[] bytes = new byte[1024 * 1024 * 2]; int length = socketCommunication.Receive(bytes); string msg = Encoding.UTF8.GetString(bytes, 0, length); ShowMsg("接收到来自" + socketCommunication.RemoteEndPoint.ToString() + "的数据:" + msg); } }); thread.IsBackground = true; thread.Start(); dictThread.Add(socketCommunication.RemoteEndPoint.ToString(), thread); ShowMsg("客户端链接成功!通讯地址为:" + socketCommunication.RemoteEndPoint.ToString()); } } delegate void ShowMsgCallback(string msg); private void ShowMsg(string msg) { if (this.InvokeRequired) // 也能够启动时修改控件的 CheckForIllegalCrossThreadCalls 属性 { this.Invoke(new ShowMsgCallback(ShowMsg), new object[] { msg }); } else { this.txtMsg.AppendText(msg + "\r\n"); } } private void btnSendMsg_Click(object sender, EventArgs e) { if (lbSocketOnline.Text.Length == 0) MessageBox.Show("至少选择一个客户端才能发送消息!"); else { // Send() 只接受字节数组 string msg = txtSendMsg.Text.Trim(); dictCommunication[lbSocketOnline.Text].Send(Encoding.UTF8.GetBytes(msg)); ShowMsg("发送数据:" + msg); } } private void btnSendToAll_Click(object sender, EventArgs e) { string msg = txtSendMsg.Text.Trim(); foreach (var socket in dictCommunication.Values) { socket.Send(Encoding.UTF8.GetBytes(msg)); } ShowMsg("群发数据:" + msg); } }
客户端:ui
public partial class ChatClient : Form { public ChatClient() { InitializeComponent(); } /// <summary> /// 此线程用来接收服务器发送的数据 /// </summary> Thread threadRecive = null; Socket socketClient = null; private void btnConnect_Click(object sender, EventArgs e) { // 客户端建立通信套接字并链接服务器、开始接收服务器传来的数据 socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socketClient.Connect(IPAddress.Parse(txtIP.Text.Trim()), int.Parse(txtPort.Text.Trim())); ShowMsg(string.Format("链接服务器({0}:{1})成功!", txtIP.Text.Trim(), txtPort.Text.Trim())); threadRecive = new Thread(new ThreadStart(() => { while (true) { // Receive 方法从套接字中接收数据,并存入接收缓冲区 byte[] bytes = new byte[1024 * 1024 * 2]; int length = socketClient.Receive(bytes); string msg = Encoding.UTF8.GetString(bytes, 0, length); ShowMsg("接收到数据:" + msg); } })); threadRecive.IsBackground = true; threadRecive.Start(); } delegate void ShowMsgCallback(string msg); private void ShowMsg(string msg) { if (this.InvokeRequired) // 也能够启动时修改控件的 CheckForIllegalCrossThreadCalls 属性 { this.Invoke(new ShowMsgCallback(ShowMsg), new object[] { msg }); } else { this.txtMsg.AppendText(msg + "\r\n"); } } private void btnSend_Click(object sender, EventArgs e) { string msg = txtSendMsg.Text.Trim(); socketClient.Send(Encoding.UTF8.GetBytes(msg)); ShowMsg("发送数据:" + msg); } }
如今全部客户都能和服务器进行通讯,服务器也能和全部客户进行通讯。那么,客户端之间互相通讯呢? this
显然,在客户端界面也应建立在线列表,每次有人登陆后,服务器端除了刷新自身在线列表外,还需将新客户端的套接字信息发送给其余在线客户端,以便它们更新本身的在线列表。 spa
客户端发送消息给服务器,服务器转发此消息给另外一个客户端。固然,这个消息须要进行一些处理,至少要包含目标套接字和发送内容。 操作系统
更为完善的是,服务器必须定时按制定的规则检测列表中套接字通讯的有效性,经过发送响应信号,并接收客户端应答信号以确认客户端的链接性是真实的(不然,需剔除无效客户端)。 线程
客户端:
private void btnChooseFile_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { txtFilePath.Text = ofd.FileName; } } private void btnSendFile_Click(object sender, EventArgs e) { using (FileStream fs = new FileStream(txtFilePath.Text, FileMode.Open)) { byte[] bytes = new byte[1024 * 1024 * 2]; // 假设第一个字节为标志位:0 表示传送文件 // 方式一:总体向后偏移 1 个字节;但这样有潜在缺点, // 有时在通讯时会很是准确的按照约定的字节长度来传递, // 那么这种偏移方案显然是不可靠的 // bytes[0] = 0; // int length = fs.Read(bytes, 1, bytes.Length); // 方式二:建立多出 1 个字节的数组发送 int length = fs.Read(bytes, 0, bytes.Length); byte[] newBytes = new byte[length + 1]; newBytes[0] = 0; // BlockCopy() 会比你本身写for循环赋值更为简单合适 Buffer.BlockCopy(bytes, 0, newBytes, 1, length); socketClient.Send(newBytes); } }
服务器端(Receive 方法中修改为这样):
Thread thread = new Thread(() => { while (true) { byte[] bytes = new byte[1024 * 1024 * 2]; int length = socketCommunication.Receive(bytes); if (bytes[0] == 0) // File { SaveFileDialog sfd = new SaveFileDialog(); if (sfd.ShowDialog() == DialogResult.OK) { using (FileStream fs = new FileStream(sfd.FileName, FileMode.Create)) { fs.Write(bytes, 1, length - 1); fs.Flush(); ShowMsg("文件保存成功,路径为:" + sfd.FileName); } } } else // Msg { string msg = Encoding.UTF8.GetString(bytes, 0, length); ShowMsg("接收到来自" + socketCommunication.RemoteEndPoint.ToString() + "的数据:" + msg); } } });
Socket 通讯属于网络通讯程序,会有许多的意外,必须进行异常处理以便程序不会被轻易的击垮。不论是客户端仍是服务器端,只要和网络交互的环节(Connect、Accept、Send、Receive 等)都要作异常处理。
本例中对服务器端 Receive 方法环节作了一些异常处理,并移除了相应的资源,例以下面:
try { length = socketCommunication.Receive(bytes); } catch (SocketException ex) { ShowMsg("出现异常:" + ex.Message); string key = socketCommunication.RemoteEndPoint.ToString(); lbSocketOnline.Items.Remove(key); dictCommunication.Remove(key); dictThread.Remove(key); break; }