admin 管理员组文章数量: 887021
2023年12月19日发(作者:国家计算机二级c语言真题及答案)
private Socket serverSocket; private Socket clientSocket; private List
public AsyncServer() { clients = new List
public void ListenThread() { while (true) { (); ccept(newAsyncCallback(AcceptCallback), serverSocket); e(); } }
public void AcceptCallback(IAsyncResult ar) { Socket temp =((Socket)tate).EndAccept(ar);
private const int port =11000; private static ManualResetEvent connectDone= new ManualResetEvent(false); private static ManualResetEvent sendDone= new ManualResetEvent(false); private static ManualResetEvent receiveDone= new ManualResetEvent(false); private static String response= ;
private static void StartClient(){ try{ IPHostEntry ipHostInfo= e(""); IPAddress ipAddress= sList[0]; IPEndPoint remoteEP= new IPEndPoint(ipAddress,port); Socket client= new Socket(etwork, , );
onnect(remoteEP,new AsyncCallback(ConnectCallback), client); e(); Send(client, "This is atest
Receive(client); e(); ine("Responsereceived : {0}", response); wn(); ();} catch (Exception e){ ine(ng());} }
private static void ConnectCallback(IAsyncResult ar) { try{ Socket client = (Socket)tate; nect(ar);
ine("Socketconnected to {0}", ng()); (); } catch (Exception e){ ine(ng());} }
private static void Receive(Socket client) { try{ StateObject state= new StateObject(); cket= client; eceive(,0, Size, 0, new AsyncCallback(ReceiveCallback),state); } catch (Exception e){ ine(ng());} }
private static void ReceiveCallback(IAsyncResult ar) { try{ StateObject state = (StateObject)tate; Socket client =cket; int bytesRead= eive(ar); if (bytesRead> 0){ (ing(,0, bytesRead)); eceive(,0, Size, 0, new AsyncCallback(ReceiveCallback),state); } else{ if (> 1) {
response= ng(); } (); } } catch (Exception e){ ine(ng());} }
private static void Send(Socket client, String data) { byte[] byteData= es(data); end(byteData,0, , 0, new AsyncCallback(SendCallback),client); }
private static void SendCallback(IAsyncResult ar) { try{ Socket client = (Socket)tate; int bytesSent= d(ar); ine("Sent {0}bytes to server.", bytesSent); ();} catch (Exception e){ ine(ng());} }
public static int Main(String[] args) { StartClient(); return 0; }}
cket= handler; eceive(,0, Size, 0,newAsyncCallback(ReadCallback),state); (); } public static void ReadCallback(IAsyncResult ar) { String content= ; StateObject state = (StateObject)tate; Socket handler =cket; int bytesRead= eive(ar); if (bytesRead> 0) { (ing(,0, bytesRead)); content= ng(); if (f("
class Server{ private TcpListener tcpListener; private Thread listenThread; public Server() { tener = newTcpListener(, 3000); Thread = new Thread(newThreadStart(ListenForClients)); (); }
private voidListenForClients(){ (); while (true) { try{ TcpClient client =TcpClient(); Thread clientThread = new Thread(newParameterizedThreadStart(HandleClientComm)); (client);}catch(Exception e){break;}}();}
private voidHandleClientComm(object client){ TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream =eam(); byte[] message = new byte[4096]; int bytesRead; while (true)
{ bytesRead = 0; try { bytesRead =(message, 0, 4096); } catch { break; } if (bytesRead == 0) { break; } strings = ing(message, 0, bytesRead); ine(s); byte[] SendMessage = es("收到信息"); (SendMessage,0, ); ();}(); ();}}
客户端:TcpClientclient = new TcpClient();IPEndPointserverEndPoint = new IPEndPoint(("127.0.0.1"), 3000);t(serverEndPoint);NetworkStreamclientStream = eam();byte[] buffer= es("Hello Server!");(buffer,0 , );();
byte[] message= new byte[4096];
int bytesRead= (message, 0, 4096);string s =ing(message, 0, bytesRead);ine(s);();();
版权声明:本文标题:C#Socket编程详解(三)Socket编程 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1702983799h438429.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论