C# Socket异步通信
Posted 竹林听雨行
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# Socket异步通信相关的知识,希望对你有一定的参考价值。
转载
https://www.cnblogs.com/llllll/archive/2009/05/13/1455703.html
服务器端
TCPServer
1、使用的通讯通道:socket
2、用到的基本功能:
Bind,
Listen,
BeginAccept
EndAccept
BeginReceive
EndReceive
3、函数参数说明
SocketType.Stream, ProtocolType.Tcp);
新建socket所使用的参数均为系统预定义的量,直接选取使用。
localEndPoint 表示一个定义完整的终端,包括IP和端口信息。
//new IPEndPoint(IPAddress,port)
//IPAdress.Parse("192.168.1.3")
监听
new AsyncCallback(AcceptCallback),
listener);
AsyncCallback(AcceptCallback),一旦连接上后的回调函数为AcceptCallback。当系统调用这个函数时,自动赋予的输入参数为IAsyncResoult类型变量ar。
listener,连接行为的容器。
完成连接,返回此时的socket通道。
new AsyncCallback(ReadCallback), state);
接收的字节,0,字节长度,0,接收时调用的回调函数,接收行为的容器。
========
容器的结构类型为:
public class StateObject
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
容器至少为一个socket类型。
===============
int bytesRead = handler.EndReceive(ar);
完成一次连接。数据存储在state.buffer里,bytesRead为读取的长度。
new AsyncCallback(SendCallback), handler);
发送数据byteData,回调函数SendCallback。容器handler
发送完毕,bytesSent发送字节数。
4 程序结构
主程序:
byte[] bytes = new Byte[1024];
IPAddress ipAddress = IPAddress.Parse("192.168.1.104");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// 生成一个TCP的socket
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
listener.Bind(localEndPoint);
listener.Listen(100);
while (true)
// Set the event to nonsignaled state.
allDone.Reset();
//开启异步监听socket
Console.WriteLine("Waiting for a connection");
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
// 让程序等待,直到连接任务完成。在AcceptCallback里的适当位置放置allDone.Set()语句.
allDone.WaitOne();
Console.WriteLine("\\nPress ENTER to continue");
Console.Read();
连接行为回调函数AcceptCallback:
public static void AcceptCallback(IAsyncResult ar)
//添加此命令,让主线程继续.
allDone.Set();
// 获取客户请求的socket
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// 造一个容器,并用于接收命令.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
读取行为的回调函数ReadCallback:
public static void ReadCallback(IAsyncResult ar)
String content = String.Empty;
// 从异步state对象中获取state和socket对象.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
// 从客户socket读取数据.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
// 如果接收到数据,则存起来
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
// 检查是否有结束标记,如果没有则继续读取
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
//所有数据读取完毕.
Console.WriteLine("Read 0 bytes from socket. \\n Data : 1",
content.Length, content);
// 给客户端响应.
Send(handler, content);
else
// 接收未完成,继续接收.
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
发送消息给客户端:
private static void Send(Socket handler, String data)
// 消息格式转换.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// 开始发送数据给远程目标.
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
private static void SendCallback(IAsyncResult ar)
// 从state对象获取socket.
Socket handler = (Socket)ar.AsyncState;
//完成数据发送
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent 0 bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
在各种行为的回调函数中,所对应的socket都从输入参数的AsyncState属性获得。使用(Socket)或者(StateObject)进行强制转换。BeginReceive函数使用的容器为state,因为它需要存放传送的数据。
而其余接收或发送函数的容器为socket也可。
完整代码
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
// State object for reading client data asynchronously
public class StateObject
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
public class AsynchronousSocketListener
// Thread signal.
public static ManualResetEvent allDone = new ManualResetEvent(false);
public AsynchronousSocketListener()
public static void StartListening()
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];
// Establish the local endpoint for the socket.
// The DNS name of the computer
// running the listener is "host.contoso.com".
//IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = IPAddress.Parse("192.168.1.104");
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Bind the socket to the local endpoint and listen for incoming connections.
try
listener.Bind(localEndPoint);
listener.Listen(100);
while (true)
// Set the event to nonsignaled state.
allDone.Reset();
// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection");
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
// Wait until a connection is made before continuing.
allDone.WaitOne();
catch (Exception e)
Console.WriteLine(e.ToString());
Console.WriteLine("\\nPress ENTER to continue");
Console.Read();
public static void AcceptCallback(IAsyncResult ar)
// Signal the main thread to continue.
allDone.Set();
// Get the socket that handles the client request.
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
public static void ReadCallback(IAsyncResult ar)
String content = String.Empty;
// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket handler = state.workSocket;
// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);
if (bytesRead > 0)
// There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(
state.buffer, 0, bytesRead));
// Check for end-of-file tag. If it is not there, read
// more data.
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
// All the data has been read from the
// client. Display it on the console.
Console.WriteLine("Read 0 bytes from socket. \\n Data : 1",
content.Length, content);
// Echo the data back to the client.
Send(handler, content);
else
// Not all data received. Get more.
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
private static void Send(Socket handler, String data)
// Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
// Begin sending the data to the remote device.
handler.BeginSend(byteData, 0, byteData.Length, 0,
new AsyncCallback(SendCallback), handler);
private static void SendCallback(IAsyncResult ar)
try
// Retrieve the socket from the state object.
Socket handler = (Socket)ar.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = handler.EndSend(ar);
Console.WriteLine("Sent 0 bytes to client.", bytesSent);
handler.Shutdown(SocketShutdown.Both);
handler.Close();
catch (Exception e)
Console.WriteLine(e.ToString());
public static int Main(String[] args)
StartListening();
return 0;
Socket通信实例(C#)
SOCKET原理
一、套接字(socket)概念
套接字(socket)是通信的基石,是支持TCP/IP协议的网络通信的基本操作单元。它是网络通信过程中端点的抽象表示,包含进行网络通信必须的五种信息:连接使用的协议,本地主机的IP地址,本地进程的协议端口,远地主机的IP地址,远地进程的协议端口。
应用层通过传输层进行数据通信时,TCP会遇到同时为多个应用程序进程提供并发服务的问题。多个TCP连接或多个应用程序进程可能需要通过同一个 TCP协议端口传输数据。为了区别不同的应用程序进程和连接,许多计算机操作系统为应用程序与TCP/IP协议交互提供了套接字(Socket)接口。应 用层可以和传输层通过Socket接口,区分来自不同应用程序进程或网络连接的通信,实现数据传输的并发服务。
二、建立socket连接
建立Socket连接至少需要一对套接字,其中一个运行于客户端,称为ClientSocket ,另一个运行于服务器端,称为ServerSocket 。
套接字之间的连接过程分为三个步骤:服务器监听,客户端请求,连接确认。
- 服务器监听:服务器端套接字并不定位具体的客户端套接字,而是处于等待连接的状态,实时监控网络状态,等待客户端的连接请求
- 客户端请求:指客户端的套接字提出连接请求,要连接的目标是服务器端的套接字。为此,客户端的套接字必须首先描述它要连接的服务器的套接字,指出服务器端套接字的地址和端口号,然后就向服务器端套接字提出连接请求。
- 连接确认:当服务器端套接字监听到或者说接收到客户端套接字的连接请求时,就响应客户端套接字的请求,建立一个新的线程,把服务器端套接字的描述发给客户 端,一旦客户端确认了此描述,双方就正式建立连接。而服务器端套接字继续处于监听状态,继续接收其他客户端套接字的连接请求。
三、SOCKET连接与TCP连接
创建Socket连接时,可以指定使用的传输层协议,Socket可以支持不同的传输层协议(TCP或UDP),当使用TCP协议进行连接时,该Socket连接就是一个TCP连接。
四、Socket连接与HTTP连接
由于通常情况下Socket连接就是TCP连接,因此Socket连接一旦建立,通信双方即可开始相互发送数据内容,直到双方连接断开。但在实际网络应用 中,客户端到服务器之间的通信往往需要穿越多个中间节点,例如路由器、网关、防火墙等,大部分防火墙默认会关闭长时间处于非活跃状态的连接而导致 Socket 连接断连,因此需要通过轮询告诉网络,该连接处于活跃状态。
而HTTP连接使用的是“请求—响应”的方式,不仅在请求时需要先建立连接,而且需要客户端向服务器发出请求后,服务器端才能回复数据。
很多情况下,需要服务器端主动向客户端推送数据,保持客户端与服务器数据的实时与同步。此时若双方建立的是Socket连接,服务器就可以直接将数据传送 给客户端;若双方建立的是HTTP连接,则服务器需要等到客户端发送一次请求后才能将数据传回给客户端,因此,客户端定时向服务器端发送连接请求,不仅可 以保持在线,同时也是在“询问”服务器是否有新的数据,如果有就将数据传给客户端。
五、Socket服务端与客户端通信示意图
六、服务端与客户端代码
1、服务端
(1)服务端窗口设计
1 namespace SocketForm 2 { 3 partial class Form1 4 { 5 /// <summary> 6 /// 必需的设计器变量。 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// 清理所有正在使用的资源。 12 /// </summary> 13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows 窗体设计器生成的代码 24 25 /// <summary> 26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// </summary> 29 private void InitializeComponent() 30 { 31 this.panel1 = new System.Windows.Forms.Panel(); 32 this.tb_ip = new System.Windows.Forms.TextBox(); 33 this.lb_Ip = new System.Windows.Forms.Label(); 34 this.lb_port = new System.Windows.Forms.Label(); 35 this.tb_port = new System.Windows.Forms.TextBox(); 36 this.bt_connnect = new System.Windows.Forms.Button(); 37 this.listBox1 = new System.Windows.Forms.ListBox(); 38 this.txt_msg = new System.Windows.Forms.TextBox(); 39 this.bt_send = new System.Windows.Forms.Button(); 40 this.panel1.SuspendLayout(); 41 this.SuspendLayout(); 42 // 43 // panel1 44 // 45 this.panel1.Controls.Add(this.bt_connnect); 46 this.panel1.Controls.Add(this.lb_port); 47 this.panel1.Controls.Add(this.tb_port); 48 this.panel1.Controls.Add(this.lb_Ip); 49 this.panel1.Controls.Add(this.tb_ip); 50 this.panel1.Location = new System.Drawing.Point(21, 12); 51 this.panel1.Name = "panel1"; 52 this.panel1.Size = new System.Drawing.Size(580, 70); 53 this.panel1.TabIndex = 0; 54 // 55 // tb_ip 56 // 57 this.tb_ip.Location = new System.Drawing.Point(44, 18); 58 this.tb_ip.Name = "tb_ip"; 59 this.tb_ip.Size = new System.Drawing.Size(100, 21); 60 this.tb_ip.TabIndex = 0; 61 this.tb_ip.Text = "127.0.0.1"; 62 // 63 // lb_Ip 64 // 65 this.lb_Ip.AutoSize = true; 66 this.lb_Ip.Location = new System.Drawing.Point(15, 21); 67 this.lb_Ip.Name = "lb_Ip"; 68 this.lb_Ip.Size = new System.Drawing.Size(23, 12); 69 this.lb_Ip.TabIndex = 1; 70 this.lb_Ip.Text = "IP:"; 71 // 72 // lb_port 73 // 74 this.lb_port.AutoSize = true; 75 this.lb_port.Location = new System.Drawing.Point(158, 24); 76 this.lb_port.Name = "lb_port"; 77 this.lb_port.Size = new System.Drawing.Size(35, 12); 78 this.lb_port.TabIndex = 3; 79 this.lb_port.Text = "Port:"; 80 // 81 // tb_port 82 // 83 this.tb_port.Location = new System.Drawing.Point(199, 21); 84 this.tb_port.Name = "tb_port"; 85 this.tb_port.Size = new System.Drawing.Size(100, 21); 86 this.tb_port.TabIndex = 2; 87 this.tb_port.Text = "80"; 88 // 89 // bt_connnect 90 // 91 this.bt_connnect.Location = new System.Drawing.Point(316, 15); 92 this.bt_connnect.Name = "bt_connnect"; 93 this.bt_connnect.Size = new System.Drawing.Size(132, 42); 94 this.bt_connnect.TabIndex = 4; 95 this.bt_connnect.Text = "开始监听"; 96 this.bt_connnect.UseVisualStyleBackColor = true; 97 this.bt_connnect.Click += new System.EventHandler(this.bt_connnect_Click); 98 // 99 // listBox1 100 // 101 this.listBox1.FormattingEnabled = true; 102 this.listBox1.ItemHeight = 12; 103 this.listBox1.Location = new System.Drawing.Point(21, 112); 104 this.listBox1.Name = "listBox1"; 105 this.listBox1.Size = new System.Drawing.Size(580, 124); 106 this.listBox1.TabIndex = 1; 107 // 108 // txt_msg 109 // 110 this.txt_msg.Location = new System.Drawing.Point(21, 255); 111 this.txt_msg.Multiline = true; 112 this.txt_msg.Name = "txt_msg"; 113 this.txt_msg.Size = new System.Drawing.Size(424, 73); 114 this.txt_msg.TabIndex = 2; 115 // 116 // bt_send 117 // 118 this.bt_send.Location = new System.Drawing.Point(471, 266); 119 this.bt_send.Name = "bt_send"; 120 this.bt_send.Size = new System.Drawing.Size(119, 52); 121 this.bt_send.TabIndex = 3; 122 this.bt_send.Text = "发送"; 123 this.bt_send.UseVisualStyleBackColor = true; 124 this.bt_send.Click += new System.EventHandler(this.bt_send_Click); 125 // 126 // Form1 127 // 128 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 129 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 130 this.ClientSize = new System.Drawing.Size(695, 340); 131 this.Controls.Add(this.bt_send); 132 this.Controls.Add(this.txt_msg); 133 this.Controls.Add(this.listBox1); 134 this.Controls.Add(this.panel1); 135 this.Name = "Form1"; 136 this.Text = "SocketForm"; 137 this.Load += new System.EventHandler(this.Form1_Load); 138 this.panel1.ResumeLayout(false); 139 this.panel1.PerformLayout(); 140 this.ResumeLayout(false); 141 this.PerformLayout(); 142 143 } 144 145 #endregion 146 147 private System.Windows.Forms.Panel panel1; 148 private System.Windows.Forms.Button bt_connnect; 149 private System.Windows.Forms.Label lb_port; 150 private System.Windows.Forms.TextBox tb_port; 151 private System.Windows.Forms.Label lb_Ip; 152 private System.Windows.Forms.TextBox tb_ip; 153 private System.Windows.Forms.ListBox listBox1; 154 private System.Windows.Forms.TextBox txt_msg; 155 private System.Windows.Forms.Button bt_send; 156 } 157 }
(2)服务端逻辑设计
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Net; 8 using System.Net.Sockets; 9 using System.Text; 10 using System.Threading; 11 using System.Windows.Forms; 12 13 namespace SocketForm 14 { 15 public partial class Form1 : Form 16 { 17 public Form1() 18 { 19 InitializeComponent(); 20 } 21 22 private void bt_connnect_Click(object sender, EventArgs e) 23 { 24 25 try 26 { 27 //点击开始监听时 在服务端创建一个负责监听IP和端口号的Socket 28 Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 29 IPAddress ip = IPAddress.Any; 30 //创建对象端口 31 IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(tb_port.Text)); 32 33 socketWatch.Bind(point);//绑定端口号 34 ShowMsg("监听成功!"); 35 socketWatch.Listen(10);//设置监听 36 37 //创建监听线程 38 Thread thread = new Thread(Listen); 39 thread.IsBackground = true; 40 thread.Start(socketWatch); 41 } 42 catch { } 43 44 } 45 46 /// <summary> 47 /// 等待客户端的连接 并且创建与之通信的Socket 48 /// </summary> 49 Socket socketSend; 50 void Listen(object o) 51 { 52 try 53 { 54 Socket socketWatch = o as Socket; 55 while (true) 56 { 57 socketSend = socketWatch.Accept();//等待接收客户端连接 58 ShowMsg(socketSend.RemoteEndPoint.ToString() + ":" + "连接成功!"); 59 //开启一个新线程,执行接收消息方法 60 Thread r_thread = new Thread(Received); 61 r_thread.IsBackground = true; 62 r_thread.Start(socketSend); 63 } 64 } 65 catch { } 66 } 67 /// <summary> 68 /// 服务器端不停的接收客户端发来的消息 69 /// </summary> 70 /// <param name="o"></param> 71 void Received(object o) 72 { 73 try 74 { 75 Socket socketSend = o as Socket; 76 while (true) 77 { 78 //客户端连接服务器成功后,服务器接收客户端发送的消息 79 byte[] buffer = new byte[1024 * 1024 * 3]; 80 //实际接收到的有效字节数 81 int len = socketSend.Receive(buffer); 82 if (len == 0) 83 { 84 break; 85 } 86 string str = Encoding.UTF8.GetString(buffer, 0, len); 87 ShowMsg(socketSend.RemoteEndPoint + ":" + str); 88 } 89 } 90 catch { } 91 } 92 /// <summary> 93 /// 服务器向客户端发送消息 94 /// </summary> 95 /// <param name="str"></param> 96 void Send(string str) { 97 byte[] buffer = Encoding.UTF8.GetBytes(str); 98 socketSend.Send(buffer); 99 } 100 101 void ShowMsg(string msg) 102 { 103 listBox1.Items.Add(msg + "\\r\\n"); 104 } 105 106 private void Form1_Load(object sender, EventArgs e) 107 { 108 Control.CheckForIllegalCrossThreadCalls = false; 109 } 110 111 private void bt_send_Click(object sender, EventArgs e) 112 { 113 Send(txt_msg.Text.Trim()); 114 } 115 } 116 }
2、客户端
(1)客户端窗口设计
1 namespace SocketClient 2 { 3 partial class Form1 4 { 5 /// <summary> 6 /// 必需的设计器变量。 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// 清理所有正在使用的资源。 12 /// </summary> 13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region Windows 窗体设计器生成的代码 24 25 /// <summary> 26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// </summary> 29 private void InitializeComponent() 30 { 31以上是关于C# Socket异步通信的主要内容,如果未能解决你的问题,请参考以下文章
C#定时器接收定时发送和处理接收socket异步通信,接收值放在静态变量里,有时候收到的数据不完整。