C#socket异步怎么实现 线程间通信如何实现 c# 异步Socket于同步Socket的主要区别?

c# Socket\u5982\u4f55\u5f02\u6b65\u5206\u6279\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u5305\uff1f

\u5982\u679c\u5728\u540c\u6b65\u7684\u57fa\u7840\u4e0a\u53bb\u6539\uff0c\u6539\u52a8\u4f1a\u6bd4\u8f83\u5927\uff0c\u56e0\u4e3a\u540c\u6b65\u7684SOCKET\u901a\u8baf\u51e0\u4e4e\u4e0d\u9700\u8981\u4ec0\u4e48\u534f\u8bae\uff0c\u800c\u5f02\u6b65\u4e00\u822c\u662f\u9700\u8981\u4e00\u4e2a\u7b80\u5355\u7684\u534f\u8bae\uff0c\u5e76\u4e14\u590d\u6742\u7a0b\u5ea6\u4f1a\u9ad8\u5f88\u591a\uff0c\u56e0\u4e3a\u5f02\u6b65\u672c\u8eab\u5c31\u662f\u4e0d\u786e\u5b9a\u7684\u3002

\u6211\u4e0d\u592a\u6e05\u695a\u4f60\u8bf4\u7684\u201c\u5904\u7406\u5206\u6279\u53d1\u9001\u548c\u63a5\u6536\u5927\u6570\u636e\u5305\u201d\u662f\u4ec0\u4e48\u610f\u601d\uff0c\u6211\u7684\u7406\u89e3\u5c31\u662f\u7528\u4ee3\u7801\u628a\u6570\u636e\u4e00\u5757\u4e00\u5757\u7684\u53d1\u9001\u4e86\u7136\u540e\u518d\u5728\u76ee\u7684\u7aef\u53bb\u91cd\u65b0\u7ec4\u5408\uff0c\u4f60\u7684\u610f\u601d\u662f\u4e00\u4e2aSOCKET\u540c\u65f6\u53d1\u6d88\u606f\u4f20\u6587\u4ef6\u7b49\u7b49\uff1f\uff08\u957f\u8fde\u63a5\uff0c\u4e0d\u53ef\u53d6\uff0c\u534f\u8bae\u4f1a\u53d8\u5f97\u975e\u5e38\u590d\u6742\uff09\u6211\u5370\u8c61\u4e2dC#\u662f\u6709\u5f02\u6b65\u7684SOCKET\u53ef\u4ee5\u76f4\u63a5\u7528\u7684\uff0c\u5341\u5206\u7b80\u5355\uff0c\u53ef\u80fd\u9700\u8981\u6539\u52a8\u4e00\u4e9b\u7ed3\u6784\u3002

\u4e0d\u77e5\u9053\u662f\u5426\u6709\u7528\u4ee3\u7801\u9a8c\u8bc1\u8fc7\u53d1\u9001\u7684\u5305\u662f\u5426\u51fa\u9519\uff08\u4e00\u822c\u662f\u9700\u8981\u7684\uff0c\u5728\u5305\u7684\u524d\u9762\u6216\u8005\u540e\u9762\u9644\u4e00\u4e9bBIT\u5c31\u8db3\u591f\u4e86\uff09\uff0c\u5982\u679c\u60f3\u5c3d\u53ef\u80fd\u7b80\u5355\u7684\u4fee\u6539\uff0c\u5c31\u8fd8\u662f\u591a\u7ebf\u7a0b\u4ecd\u7136\u91c7\u7528\u540c\u6b65\u7684\u65b9\u5f0f\u597d\u4e86\uff0cSOCKET\u4e13\u7528\u53ea\u7ef4\u6301\u77ed\u8fde\u63a5\uff0c\u76d1\u542c\u7684\u76d1\u542c\uff0c\u53d1\u9001\u7684\u53d1\u9001\uff0c\u63a5\u6536\u7684\u63a5\u6536\uff0c\u5904\u7406\u7684\u5904\u7406\u3002\u5982\u679c\u9700\u8981\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6548\u7387\uff0c\u641c\u7d22\u4e00\u4e0b\u91cd\u53e0IO\uff0c\u8fd9\u4e2a\u662f\u9ad8\u6548\u7387IO\u7684\u4e0d\u4e8c\u9009\u62e9\u3002\u5e0c\u671b\u6211\u6ca1\u7406\u89e3\u9519

Socket\u4f20\u8f93\u4e2d\u62ffTCP\u4f20\u8f93\u4e3a\u4f8b\u3002\u5047\u8bbe\u670d\u52a1\u5668A \u5ba2\u6237\u673aB\u8fdb\u884c\u901a\u4fe1\u4f20\u8f93\u3002\u9996\u5148\u9700\u8981\u5728A\u673a\u5efa\u7acb\u76d1\u542c\u7ebf\u7a0b\u3002\u76d1\u542c\u67d0\u4e00\u7aef\u53e3\uff0c\u90a3\u4e48B\u673a\u53ef\u4ee5\u5411A\u673a\u53d1\u9001\u901a\u8baf\u8bf7\u6c42\uff0cB\u673a\u8fde\u63a5\u5230A\u673a\u4ee5\u540e\u3002A\u673a\u53ef\u4ee5\u4ece\u4ed6\u7684\u76d1\u542c\u961f\u5217\u4e2d\u53d6\u7684\u4e00\u4e2a\u76d1\u542c\u5bf9\u8c61\u3002\u5728A\u7aef\u62ff\u5230\u4e86\u8fd9\u4e2aSocket\u5bf9\u8c61\u5c31\u53ef\u4ee5\u8fdb\u884c\u63a5\u6536\u8ddf\u53d1\u9001\u6570\u636e\u4e86\u3002\u8fd9\u91cc\u95ee\u9898\u5c31\u51fa\u73b0\u4e86\u3002\u5047\u5982B\u7aef\u5728\u8bf7\u6c42A\u7aef\u7684\u65f6\u5019\u8bf7\u6c42\u6210\u529f\u5c31\u53d1\u9001\u4e00\u6761\u6570\u636e\u3002\u90a3\u4e48 A\u7aef\u5c31\u53ef\u4ee5\u76f4\u63a5\u62ffSocket\u5bf9\u8c61\u5f97\u5230\u4ed6\u7684\u4fe1\u606f\u3002\u4f46\u662f\u5047\u5982B\u7aef\u5e76\u6ca1\u6709\u5728\u8fde\u63a5\u6210\u529f\u540e\u76f4\u63a5\u53d1\u9001\u4fe1\u606f\u800c\u662f\u5728\u540e\u6765\u4e0d\u786e\u5b9a\u7684\u65f6\u95f4\u8fd9\u5185\u53d1\u9001\u7684\u4fe1\u606f\u3002\u90a3\u4e48A\u7aef\u5c31\u65e0\u6cd5\u5f97\u5230\u8fd9\u6761\u4fe1\u606f\u3002\u901a\u5e38\u7684\u505a\u6cd5\u662f\u7528\u4e00\u4e2a\u5b9a\u65f6\u5668\u53bb\u4e0d\u77ed\u7684\u626b\u63cf\u8fd9\u4e2a\u6570\u636e\u7f13\u5b58\u533a\u3002\u770b\u662f\u4e0d\u662f\u6709\u6570\u636e\u5b58\u5728\u8fd9\u6837\u6548\u7387\u975e\u5e38\u4f4e\u4e0b\u3002\u90a3\u4e48\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\u3002\u5c31\u7528\u5230\u4e86\u6211\u4eec\u7684\u5f02\u6b65\u4f20\u8f93\u3002\u5f02\u6b65\u4f20\u8f93\u7684\u539f\u7406\u662f\u3002\u5728A\u7aef\u5f97\u5230\u8fd9\u4e2aSOCKET\u5bf9\u8c61\u4ee5\u540e\u5e76\u4e0d\u662f\u76f4\u63a5\u53bb\u63a5\u6536\u6570\u636e\u800c\u662f\u5efa\u7acb\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u3002\u56de\u8c03\u51fd\u6570\u662f\u7531\u7cfb\u7edf\u7ef4\u62a4\u7684\u3002\u4ed6\u5728\u6307\u5b9a\u7684\u65f6\u95f4\u81ea\u52a8\u53bb\u626b\u63cf\u6570\u636e\u5b58\u50a8\u533a\u3002\u5047\u5982\u6709\u6570\u636e\u4ed6\u5c31\u628a\u6570\u636e\u5b58\u50a8\u5230\u6307\u5b9a\u7684\u5b57\u8282\u6570\u7ec4\u4e2d\u3002\u4e0d\u7528\u7528\u6237\u81ea\u5df1\u53bb\u5173\u5fc3\u3002

\u90a3\u4e48\u540c\u6b65\u4e0e\u5f02\u6b65\u5206\u522b\u5e94\u7528\u4e8e\u4ec0\u4e48\u60c5\u51b5\u5462\uff1f\u5047\u5982\u7528\u6237\u7684SOCKET\u8fde\u63a5\u6570\u636e\u6bd4\u8f83\u77ed\u6682\u7684\u3002\u4e00\u6b21\u8fde\u63a5\u76f4\u63a5\u53d1\u9001\u6570\u636e\u7684\u6216\u5ba2\u6237\u7aef\u6bd4\u8f83\u5c11\u7684\u5c31\u4f7f\u7528\u540c\u6b65\u5047\u5982\u7528\u6237\u7684SOCKET\u5c5e\u4e8e\u957f\u65f6\u95f4\u8fde\u63a5\u7684\u5c31\u4f7f\u7528\u5f02\u6b65\u65b9\u5f0f

基于C#的socket编程的TCP异步实现
一、摘要
  本篇博文阐述基于TCP通信协议的异步实现。

二、实验平台
  Visual Studio 2010

三、异步通信实现原理及常用方法
3.1 建立连接 
  在同步模式中,在服务器上使用Accept方法接入连接请求,而在客户端则使用Connect方法来连接服务器。相对地,在异步模式下,服务器可以使用BeginAccept方法和EndAccept方法来完成连接到客户端的任务,在客户端则通过BeginConnect方法和EndConnect方法来实现与服务器的连接。

  BeginAccept在异步方式下传入的连接尝试,它允许其他动作而不必等待连接建立才继续执行后面程序。在调用BeginAccept之前,必须使用Listen方法来侦听是否有连接请求,BeginAccept的函数原型为:

BeginAccept(AsyncCallback AsyncCallback, Ojbect state)
参数:

AsyncCallBack:代表回调函数

state:表示状态信息,必须保证state中包含socket的句柄

  使用BeginAccept的基本流程是:
(1)创建本地终节点,并新建套接字与本地终节点进行绑定;
(2)在端口上侦听是否有新的连接请求;
(3)请求开始接入新的连接,传入Socket的实例或者StateOjbect的实例。

  参考代码:

复制代码
//定义IP地址
IPAddress local = IPAddress.Parse("127.0,0,1");
IPEndPoint iep = new IPEndPoint(local,13000);
//创建服务器的socket对象
Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
server.Bind(iep);
server.Listen(20);
server.BeginAccecpt(new AsyncCallback(Accept),server);
复制代码
  当BeginAccept()方法调用结束后,一旦新的连接发生,将调用回调函数,而该回调函数必须包括用来结束接入连接操作的EndAccept()方法。

该方法参数列表为 Socket EndAccept(IAsyncResult iar)

下面为回调函数的实例:

复制代码
void Accept(IAsyncResult iar)
{
//还原传入的原始套接字
Socket MyServer = (Socket)iar.AsyncState;
//在原始套接字上调用EndAccept方法,返回新的套接字
Socket service = MyServer.EndAccept(iar);
}
复制代码
  至此,服务器端已经准备好了。客户端应通过BeginConnect方法和EndConnect来远程连接主机。在调用BeginConnect方法时必须注册相应的回调函数并且至少传递一个Socket的实例给state参数,以保证EndConnect方法中能使用原始的套接字。下面是一段是BeginConnect的调用:

Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp)
IPAddress ip=IPAddress.Parse("127.0.0.1");
IPEndPoint iep=new IPEndPoint(ip,13000);
socket.BeginConnect(iep, new AsyncCallback(Connect),socket);
  EndConnect是一种阻塞方法,用于完成BeginConnect方法的异步连接诶远程主机的请求。在注册了回调函数后必须接收BeginConnect方法返回的IASynccReuslt作为参数。下面为代码演示:

复制代码
void Connect(IAsyncResult iar)
{
Socket client=(Socket)iar.AsyncState;
try
{
client.EndConnect(iar);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{

}
}
复制代码

  除了采用上述方法建立连接之后,也可以采用TcpListener类里面的方法进行连接建立。下面是服务器端对关于TcpListener类使用BeginAccetpTcpClient方法处理一个传入的连接尝试。以下是使用BeginAccetpTcpClient方法和EndAccetpTcpClient方法的代码:

复制代码
public static void DoBeginAccept(TcpListener listner)
{
//开始从客户端监听连接
Console.WriteLine("Waitting for a connection");
//接收连接
//开始准备接入新的连接,一旦有新连接尝试则调用回调函数DoAcceptTcpCliet
listner.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpCliet), listner);
}

//处理客户端的连接
public static void DoAcceptTcpCliet(IAsyncResult iar)
{
//还原原始的TcpListner对象
TcpListener listener = (TcpListener)iar.AsyncState;

//完成连接的动作,并返回新的TcpClient
TcpClient client = listener.EndAcceptTcpClient(iar);
Console.WriteLine("连接成功");
}
复制代码
  代码的处理逻辑为:
(1)调用BeginAccetpTcpClient方法开开始连接新的连接,当连接视图发生时,回调函数被调用以完成连接操作;
(2)上面DoAcceptTcpCliet方法通过AsyncState属性获得由BeginAcceptTcpClient传入的listner实例;
(3)在得到listener对象后,用它调用EndAcceptTcpClient方法,该方法返回新的包含客户端信息的TcpClient。

  BeginConnect方法和EndConnect方法可用于客户端尝试建立与服务端的连接,这里和第一种方法并无区别。下面看实例:

复制代码
public void doBeginConnect(IAsyncResult iar)
{
Socket client=(Socket)iar.AsyncState;
//开始与远程主机进行连接
client.BeginConnect(serverIP[0],13000,requestCallBack,client);
Console.WriteLine("开始与服务器进行连接");
}
private void requestCallBack(IAsyncResult iar)
{
try
{
//还原原始的TcpClient对象
TcpClient client=(TcpClient)iar.AsyncState;
//
client.EndConnect(iar);
Console.WriteLine("与服务器{0}连接成功",client.Client.RemoteEndPoint);
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{

}
}
复制代码
  以上是建立连接的两种方法。可根据需要选择使用。

3.2 发送与接受数据
  在建立了套接字的连接后,就可以服务器端和客户端之间进行数据通信了。异步套接字用BeginSend和EndSend方法来负责数据的发送。注意在调用BeginSend方法前要确保双方都已经建立连接,否则会出异常。下面演示代码:

复制代码
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());
}
}
复制代码
  接收数据是通过BeginReceive和EndReceive方法:

复制代码
private static void Receive(Socket client)
{
try
{
// Create the state object.
StateObject state = new StateObject();
state.workSocket = client;
// Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
// Retrieve the state object and the client socket
// from the asynchronous state object.
StateObject state = (StateObject)ar.AsyncState;
Socket client = state.workSocket;
// Read data from the remote device.
int bytesRead = client.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));
// Get the rest of the data.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
}
else
{
// All the data has arrived; put it in response.
if (state.sb.Length > 1)
{
response = state.sb.ToString();
}
// Signal that all bytes have been received.
receiveDone.Set();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
复制代码
  上述代码的处理逻辑为:

(1)首先处理连接的回调函数里得到的通讯套接字client,接着开始接收数据;
(2)当数据发送到缓冲区中,BeginReceive方法试图从buffer数组中读取长度为buffer.length的数据块,并返回接收到的数据量bytesRead。最后接收并打印数据。

  

  除了上述方法外,还可以使用基于NetworkStream相关的异步发送和接收方法,下面是基于NetworkStream相关的异步发送和接收方法的使用介绍。
  NetworkStream使用BeginRead和EndRead方法进行读操作,使用BeginWreite和EndWrete方法进行写操作,下面看实例:

复制代码
static void DataHandle(TcpClient client)
{
TcpClient tcpClient = client;
//使用TcpClient的GetStream方法获取网络流
NetworkStream ns = tcpClient.GetStream();
//检查网络流是否可读
if(ns.CanRead)
{
//定义缓冲区
byte[] read = new byte[1024];
ns.BeginRead(read,0,read.Length,new AsyncCallback(myReadCallBack),ns);
}
else
{
Console.WriteLine("无法从网络中读取流数据");
}
}

public static void myReadCallBack(IAsyncResult iar)
{
NetworkStream ns = (NetworkStream)iar.AsyncState;
byte[] read = new byte[1024];
String data = "";
int recv;

recv = ns.EndRead(iar);
data = String.Concat(data, Encoding.ASCII.GetString(read, 0, recv));

//接收到的消息长度可能大于缓冲区总大小,反复循环直到读完为止
while (ns.DataAvailable)
{
ns.BeginRead(read, 0, read.Length, new AsyncCallback(myReadCallBack), ns);
}
//打印
Console.WriteLine("您收到的信息是" + data);
}
复制代码
3.3 程序阻塞与异步中的同步问题
  .Net里提供了EventWaitHandle类来表示一个线程的同步事件。EventWaitHandle即事件等待句柄,他允许线程通过操作系统互发信号和等待彼此的信号来达到线程同步的目的。这个类有2个子类,分别为AutoRestEevnt(自动重置)和ManualRestEvent(手动重置)。下面是线程同步的几个方法:
(1)Rset方法:将事件状态设为非终止状态,导致线程阻塞。这里的线程阻塞是指允许其他需要等待的线程进行阻塞即让含WaitOne()方法的线程阻塞;
(2)Set方法:将事件状态设为终止状态,允许一个或多个等待线程继续。该方法发送一个信号给操作系统,让处于等待的某个线程从阻塞状态转换为继续运行,即WaitOne方法的线程不在阻塞;
(3)WaitOne方法:阻塞当前线程,直到当前的等待句柄收到信号。此方法将一直使本线程处于阻塞状态直到收到信号为止,即当其他非阻塞进程调用set方法时可以继续执行。

复制代码
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 = ipHostInfo.AddressList[0];
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
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();
}
复制代码

  上述代码的逻辑为:

(1)试用了ManualRestEvent对象创建一个等待句柄,在调用BeginAccept方法前使用Rest方法允许其他线程阻塞;
(2)为了防止在连接完成之前对套接字进行读写操作,务必要在BeginAccept方法后调用WaitOne来让线程进入阻塞状态。

  当有连接接入后系统会自动调用会调用回调函数,所以当代码执行到回调函数时说明连接已经成功,并在函数的第一句就调用Set方法让处于等待的线程可以继续执行。

给个例子吧, 下面是从网页链接转载的源代码, 我看过, 可以供参考, 希望能帮到你

using System;

using System.Net;

using System.Net.Sockets;

using System.Threading;

using System.Text;

// State object for receiving data from remote device.

public class StateObject

{

// Client socket.

public Socket workSocket = null;

// Size of receive buffer.

public const int BufferSize = 256;

// Receive buffer.

public byte[] buffer = new byte[BufferSize];

// Received data string.

public StringBuilder sb = new StringBuilder();

}

public class AsynchronousClient

{

// The port number for the remote device.

private const int port = 11000;

// ManualResetEvent instances signal completion.

private static ManualResetEvent connectDone =

new ManualResetEvent(false);

private static ManualResetEvent sendDone =

new ManualResetEvent(false);

private static ManualResetEvent receiveDone =

new ManualResetEvent(false);

// The response from the remote device.

private static String response = String.Empty;

private static void StartClient()

{

// Connect to a remote device.

try

{

// Establish the remote endpoint for the socket.

// The name of the 

// remote device is "host.contoso.com".

IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com");

IPAddress ipAddress = ipHostInfo.AddressList[0];

IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

// Create a TCP/IP socket.

Socket client = new Socket(AddressFamily.InterNetwork,

SocketType.Stream, ProtocolType.Tcp);

// Connect to the remote endpoint.

client.BeginConnect(remoteEP,

new AsyncCallback(ConnectCallback), client);

connectDone.WaitOne();

// Send test data to the remote device.

Send(client, "This is a test<EOF>");

sendDone.WaitOne();

// Receive the response from the remote device.

Receive(client);

receiveDone.WaitOne();

// Write the response to the console.

Console.WriteLine("Response received : {0}", response);

// Release the socket.

client.Shutdown(SocketShutdown.Both);

client.Close();

}

catch (Exception e)

{

Console.WriteLine(e.ToString());

}

}

private static void ConnectCallback(IAsyncResult ar)

{

try

{

// Retrieve the socket from the state object.

Socket client = (Socket)ar.AsyncState;

// Complete the connection.

client.EndConnect(ar);

Console.WriteLine("Socket connected to {0}",

client.RemoteEndPoint.ToString());

// Signal that the connection has been made.

connectDone.Set();

}

catch (Exception e)

{

Console.WriteLine(e.ToString());

}

}

private static void Receive(Socket client)

{

try

{

// Create the state object.

StateObject state = new StateObject();

state.workSocket = client;

// Begin receiving the data from the remote device.

client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,

new AsyncCallback(ReceiveCallback), state);

}

catch (Exception e)

{

Console.WriteLine(e.ToString());

}

}

private static void ReceiveCallback(IAsyncResult ar)

{

try

{

// Retrieve the state object and the client socket 

// from the asynchronous state object.

StateObject state = (StateObject)ar.AsyncState;

Socket client = state.workSocket;

// Read data from the remote device.

int bytesRead = client.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));

// Get the rest of the data.

client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,

new AsyncCallback(ReceiveCallback), state);

}

else

{

// All the data has arrived; put it in response.

if (state.sb.Length > 1)

{

response = state.sb.ToString();

}

// Signal that all bytes have been received.

receiveDone.Set();

}

}

catch (Exception e)

{

Console.WriteLine(e.ToString());

}

}

private static void Send(Socket client, 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.

client.BeginSend(byteData, 0, byteData.Length, 0,

new AsyncCallback(SendCallback), client);

}

private static void SendCallback(IAsyncResult ar)

{

try

{

// Retrieve the socket from the state object.

Socket client = (Socket)ar.AsyncState;

// Complete sending the data to the remote device.

int bytesSent = client.EndSend(ar);

Console.WriteLine("Sent {0} bytes to server.", bytesSent);

// Signal that all bytes have been sent.

sendDone.Set();

}

catch (Exception e)

{

Console.WriteLine(e.ToString());

}

}

public static int Main(String[] args)

{

StartClient();

return 0;

}

}





事件 或者 action 做回调

  • c鐨勬纭彂闊
    绛旓細c鍦ㄨ嫳鏂囧瓧姣嶄腑璇讳綔鈥 [si:]鈥濄傗渃鈥濇槸鑻辫瀛楁瘝涓殑绗笁涓瓧姣嶏紝澶у啓涓衡C鈥濓紝灏忓啓涓衡渃鈥濄傚瓧姣嶇殑鍚箟锛1銆佸湪鏁板鍙婅绠楁満绉戝涓紝琛ㄧず鍗佸叚杩涘埗鐨12銆2銆佸湪鍖栧涓紝琛ㄧず纰崇殑鍖栧绗﹀彿銆3銆佸湪涔愮悊涓紝琛ㄧず闊抽樁涓殑C闊炽4銆佸湪鍥介檯鍗曚綅鍒剁殑鐢佃嵎涓〃绀哄簱浠戙5銆佸湪钀ュ吇瀛︿腑锛岃〃绀虹淮鐢熺礌C銆6銆佸湪鐢...
  • c鏄粈涔堟剰鎬濇暟瀛
    绛旓細C鏄暟瀛︿腑鐨勪竴绉嶅父鏁帮紝甯稿嚭鐜板湪鍚勭寮忓瓙涓傚叾浠h〃鐨勬槸涓涓浐瀹氱殑鏁板硷紝閫氬父鐢ㄦ潵琛ㄧず鏌愮鐗瑰畾鐨勭墿鐞嗛噺鎴栬呮暟瀛﹀父閲忋侰鐨勬剰涔夊湪涓嶅悓鐨勪笂涓嬫枃涓湁鎵涓嶅悓锛屾瘮濡侰鍙兘浠h〃鍏夐燂紝鍦嗗懆鐜囨垨鑰呭叾浠栨暟瀛︿笂鐨勫父閲忋傚湪鏁板涓婏紝C浠h〃浜嗛渶瑕佺敤浣滆绠楀熀纭鐨勪竴浜涢噸瑕佹暟鍊硷紝鍥犳涓嶅悓鐨凜鍊艰骞挎硾搴旂敤浜庡悇绉嶆暟瀛﹀垎鏀腑銆傚湪...
  • c鏄粈涔堟剰鎬?
    绛旓細c++鐨勫叿浣撳惈涔夊涓嬶細C++鏄C璇█鐨勭户鎵匡紝瀹冩棦鍙互杩涜C璇█鐨勮繃绋嬪寲绋嬪簭璁捐锛屽張鍙互杩涜浠ユ娊璞℃暟鎹被鍨嬩负鐗圭偣鐨勫熀浜庡璞$殑绋嬪簭璁捐锛岃繕鍙互杩涜浠ョ户鎵垮拰澶氭佷负鐗圭偣鐨勯潰鍚戝璞$殑绋嬪簭璁捐銆侰++鎿呴暱闈㈠悜瀵硅薄绋嬪簭璁捐鐨勫悓鏃讹紝杩樺彲浠ヨ繘琛屽熀浜庤繃绋嬬殑绋嬪簭璁捐锛屽洜鑰孋++灏遍傚簲鐨勯棶棰樿妯¤岃锛屽ぇ灏忕敱涔嬨侰++涓嶄粎鎷ユ湁璁...
  • c鏄粈涔堟剰鎬?
    绛旓細c++鐨勫叿浣撳惈涔夊涓嬶細C++鏄C璇█鐨勭户鎵匡紝瀹冩棦鍙互杩涜C璇█鐨勮繃绋嬪寲绋嬪簭璁捐锛屽張鍙互杩涜浠ユ娊璞℃暟鎹被鍨嬩负鐗圭偣鐨勫熀浜庡璞$殑绋嬪簭璁捐锛岃繕鍙互杩涜浠ョ户鎵垮拰澶氭佷负鐗圭偣鐨勯潰鍚戝璞$殑绋嬪簭璁捐銆侰++鎿呴暱闈㈠悜瀵硅薄绋嬪簭璁捐鐨勫悓鏃讹紝杩樺彲浠ヨ繘琛屽熀浜庤繃绋嬬殑绋嬪簭璁捐锛屽洜鑰孋++灏遍傚簲鐨勯棶棰樿妯¤岃锛屽ぇ灏忕敱涔嬨侰++涓嶄粎鎷ユ湁璁...
  • c鏄粈涔堟剰鎬
    绛旓細c++鐨勫叿浣撳惈涔夊涓嬶細C++鏄C璇█鐨勭户鎵匡紝瀹冩棦鍙互杩涜C璇█鐨勮繃绋嬪寲绋嬪簭璁捐锛屽張鍙互杩涜浠ユ娊璞℃暟鎹被鍨嬩负鐗圭偣鐨勫熀浜庡璞$殑绋嬪簭璁捐锛岃繕鍙互杩涜浠ョ户鎵垮拰澶氭佷负鐗圭偣鐨勯潰鍚戝璞$殑绋嬪簭璁捐銆侰++鎿呴暱闈㈠悜瀵硅薄绋嬪簭璁捐鐨勫悓鏃讹紝杩樺彲浠ヨ繘琛屽熀浜庤繃绋嬬殑绋嬪簭璁捐锛屽洜鑰孋++灏遍傚簲鐨勯棶棰樿妯¤岃锛屽ぇ灏忕敱涔嬨侰++涓嶄粎鎷ユ湁...
  • 缃戠粶涓C鍜宻c鏄粈涔堟剰鎬?
    绛旓細1銆佲C鈥滄槸鎷奸煶鈥渃hu鈥濈殑棣栧瓧姣嶏紝涓轰簡鏂逛究璧疯缂╁啓涓衡淐鈥濓紝鎸囩殑鏄鐢枫佸濂崇殑鎰忔濄2銆丼C鏄痵uperchat鐨勭缉鍐欙紝鏄粯璐圭暀瑷鍔熻兘銆傝繖涓姛鑳芥渶寮濮嬫槸YouTube2017骞翠笂绾跨殑锛孊绔欎篃鎺ㄥ嚭浜嗚繖涓姛鑳姐傝繖鏄竴绉嶅湪鐩存挱杩囩▼涓繛鎺ョ矇涓濅笌涓绘挱鐨勬柟寮忥紝鍚屾椂涔熷彲缁欎富鎾甫鏉ョ浉搴旂殑缁忔祹鏀剁泭銆傞氳繃杩欎竴鍔熻兘锛岀矇涓濆彲浠ヤ互鏄庝寒...
  • C鏄粈涔堟剰鎬?
    绛旓細nCk鏄竴涓暣浣擄紝鏄痭涓厓绱犱腑锛屽彇k涓厓绱犵殑鍙栨硶鐨勪釜鏁帮紝涔熷彨n涓厓绱犱腑锛屽彇k 涓猭缁勫悎鏁帮紝锛C浠h〃缁勫悎锛夛紝绠楁硶鏄細nCk锛漬!/k!锛坣-k锛!锛漬锛坣-1锛夆︹︼紙n-k+1锛/k!绛変簬浠巒寮濮嬭繛缁掑噺鐨刴涓嚜鐒舵暟鐨勭Н闄や互浠1寮濮嬭繛缁掑鐨刴涓嚜鐒舵暟鐨勭Н銆傝姒傜巼鍏紡鐨勬帹瀵艰繃绋嬶細鍦ㄨ繖涓瘉鏄庝腑锛岃〃绀簄娆...
  • c鐨勬剰鎬濇槸鎬庝箞鏍风殑?
    绛旓細c鐨勬剰鎬濓細1銆佸湪鍖栧涓紝琛ㄧず纰崇殑鍖栧绗﹀彿銆2銆佸湪涔愮悊涓紝琛ㄧず锛氶煶闃朵腑鐨C闊筹紝璋冨彿涓簬C闊冲紑濮嬬殑闊充箰鐨凜澶ц皟鍙奀灏忚皟锛屾媿瀛愯鍙蜂腑鐨4/4鎷嶅瓙銆3銆佸湪缃楅┈鏁板瓧涓紝琛ㄧず100銆4銆佸湪鍥介檯鍗曚綅鍒朵腑锛岃〃绀虹數鑽烽噺鐨勫崟浣嶁滃簱浠戔濄5銆佸湪璁$畻鏈虹瀛︿腑锛屾湁C璇█銆丆++銆丆#銆丱bjective-C绛夈6銆佸湪钀ュ吇瀛︿腑锛岃〃绀...
  • c鏄粈涔堟剰鎬濈殑缂╁啓?
    绛旓細c鏄粈涔堟剰鎬濈殑缂╁啓1 C鏄痗ell鑻辫鍚嶇О鐨勭畝绉帮紝鎰忔濇槸缁嗚優銆傝瘝姹囪В鏋愶細cell 鍩烘湰璇嶆眹 鑻 [sel] 缇 [sel]n. 鍗曚汉鐗㈡埧锛涘皬鎴块棿锛涚粏鑳烇紱铚傛埧鐨勫发瀹わ紱鐢垫睜锛涙斂娌诲皬缁勶紱(璁$畻鏈虹數瀛愯〃鏍肩殑)鍗曞厓鏍硷紱鎵嬫満 The prisoner was locked in a cell.閭e洑鐘鍏冲湪鍗曚汉鐗㈡埧鍐呫俤iseased cell 鍙戠敓鐥呭彉鐨勭粏鑳 d...
  • c鍦ㄥ寲瀛︿腑鎸囦粈涔堟剰鎬
    绛旓細C鍦ㄥ寲瀛︿腑鏈変袱绉嶅惈涔夛細1銆佽〃绀烘祿搴︼紝鍗曚綅涓簃ol/L锛岃绠楀紡涓猴細C=n/V. C=1000蟻蠅/M銆傚惈涔夛細浠1鍗囨憾娑蹭腑鎵鍚憾璐ㄧ殑鎽╁皵鏁拌〃绀虹殑娴撳害銆備互鍗曚綅浣撶Н閲屾墍鍚憾璐ㄧ殑鐗╄川鐨勯噺锛堟懇灏旀暟锛夋潵琛ㄧず婧舵恫缁勬垚鐨勭墿鐞嗛噺锛屽彨浣滆婧惰川鐨勬懇灏旀祿搴︼紝鍙堢О璇ユ憾璐ㄧ墿璐ㄧ殑閲忔祿搴︺傛憾璐ㄥ惈閲忚秺澶氾紝娴撳害瓒婂ぇ銆傛祿搴﹀彲浠ョ敤涓瀹...
  • 扩展阅读:c#的websocket服务端 ... c#tcp socket的编程 ... c#是什么语言 ... c和c#和c++区别 ... c# invoke ... c# tcp ... c#面试题 ... c#和java ... c#怎么读 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网