问题描述
现在学写一个异步多线程的服务器端,遇到一个问题,怎么异步发送数据publicstaticvoidSend(Sockethandler,Stringdata)象这个函数,怎么去调用的?
解决方案
解决方案二:
new一个socketmySocket,然后调用Send(mySocket,myData);
解决方案三:
可这样是新建的SOCKET,无法判断连接的状况,任意发吗?
解决方案四:
Send(mySocket,myData)中,mySocket中包括连接信息,要么是用它接收到对方的信息,再用它发回一个信息;要么自己用它来连接!
解决方案五:
我的意思是说这个新建的SOCKET不会包括原来的监听那个SOCKET类的信息,怎么能把那个信息传过来
解决方案六:
publicclassStateObject//(异步)线程参数类{//Clientsocket.publicSocketworkSocket=null;//Sizeofreceivebuffer.publicconstintBufferSize=256;//Receivebuffer.publicbyte[]buffer=newbyte[BufferSize];//Receiveddatastring.publicStringBuildersb=newStringBuilder();}publicvoidStart(){Socketlistener=newSocket(...);//listener为侦听连接的套接字...listener.BeginAccept(newAsyncCallback(AcceptCallback),listener);}publicvoidAcceptCallback(IAsyncResultar){Socketlistener=(Socket)ar.AsyncState;handler=listener.EndAccept(ar);//handler为接收信息的StateObjectstate=newStateObject();state.workSocket=handler;handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,newAsyncCallback(ReadCallback),state);}publicvoidReadCallback(IAsyncResultar){StateObjectstate=(StateObject)ar.AsyncState;Sockethandler=state.workSocket;//Readdatafromtheclientsocket.intbytesRead=0;//if(handler.Connected)bytesRead=handler.EndReceive(ar);}
解决方案七:
xuexue