admin 管理员组

文章数量: 887021


2023年12月19日发(作者:国家计算机二级c语言真题及答案)

private Socket serverSocket; private Socket clientSocket; private List clients; private ManualResetEvent AcceptDone;

public AsyncServer() { clients = new List(); ipEndPoint = new IPEndPoint(,8001); serverSocket = newSocket(etwork, , ); (ipEndPoint); (10); AcceptDone = new ManualResetEvent(false); }//启动函数 public void Start() { Thread startT = new Thread(ListenThread); (); }

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"); e();

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("") > -1){ ine("Read {0}bytes from socket. n Data : {1}", ,content); Send(handler, "Serverreturn :" + content); } else{ eceive(,0, Size, 0, new AsyncCallback(ReadCallback),state); } } } private static void Send(Socket handler, String data){ byte[] byteData= es(data); end(byteData,0, , 0, new AsyncCallback(SendCallback),handler); } private static void SendCallback(IAsyncResult ar) { try{ Socket handler =(Socket)tate;

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);();();


本文标签: 语言 计算机 启动 收到 国家