问题描述
具体功能室这样:用C#实现一个服务器功能,用VC6++实现一个客户端功能,服务器启动之后,等待客户端连接,然后接受客户端发过来的字符串。现在的问题是,客户端每次发送之前,都必须先连接一次,我想只需连接一次,然后多次不间断发送。请高手帮解答-------------------------------------服务器端代码(C#)namespaceSocketServer{publicdelegatevoidDelLink(stringstrTemp,stringstrTemp1);publicdelegatevoidTextShowHandeler(stringMessage);publicpartialclassFormServer:Form{privateAsyasyTemp=newAsy();privateDelLinklinkSocket;publicFormServer(){InitializeComponent();linkSocket=newDelLink(asyTemp.BeginAccept);asyTemp.tshMessage+=newTextShowHandeler(this.Appendtext);}privatevoidbuttonOk_Click(objectsender,EventArgse){stringIP=this.textBoxIp.Text;stringPort=this.textBoxPort.Text;this.buttonOk.Enabled=false;linkSocket.BeginInvoke(IP,Port,null,null);}privatevoidbuttonCancle_Click(objectsender,EventArgse){this.Close();}publicvoidAppendtext(stringMessage){if(this.textBoxReceive.InvokeRequired){//this.Invoke(asyTemp.tshMessage(Message));TextShowHandelerd=newTextShowHandeler(Appendtext);this.Invoke(d,newobject[]{Message});}else{this.textBoxReceive.AppendText(Message);if(Message.Length>30){this.toolStripStatusLabelSee.Text=Message.Remove(30)+"...";}else{this.toolStripStatusLabelSee.Text=Message;}}}}publicclassAsy{publicTextShowHandelertshMessage;publicvoidBeginAccept(stringIP,stringPort){intBUFFERSIZE=256;byte[]buffer=newbyte[BUFFERSIZE];Socketserver=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);IPEndPointiep=newIPEndPoint(IPAddress.Parse(IP),int.Parse(Port));server.Bind(iep);server.Listen(5);while(true){IAsyncResultresult=server.BeginAccept(newAsyncCallback(AcceptConn1),server);this.tshMessage("waitingforaconnection...n");result.AsyncWaitHandle.WaitOne();}}publicvoidAcceptConn1(IAsyncResultiar){this.tshMessage("asocketisconnected...n");Sockets=(Socket)iar.AsyncState;Sockethandler=s.EndAccept(iar);StateObjectstate=newStateObject();state.worksocket=handler;handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,newAsyncCallback(ReadCallBack),state);}publicvoidReadCallBack(IAsyncResultar){stringcontent=string.Empty;StateObjectstate=(StateObject)ar.AsyncState;Sockethandler=state.worksocket;intbytesRead=handler.EndReceive(ar);if(bytesRead>0){state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead));content=state.sb.ToString();this.tshMessage("Read"+content.Length+"bytesfromsocket.n");this.tshMessage("Data:"+content+"n");//Send(handler,content);}else{handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,newAsyncCallback(ReadCallBack),state);}}}publicclassStateObject{publicSocketworksocket=null;publicconstintBufferSize=1024;publicbyte[]buffer=newbyte[BufferSize];publicStringBuildersb=newStringBuilder();}}------------------------------客户端代码(VC++)voidCChatDlg::OnBtnSend(){//DWORDdwIP;CStringstrSend;WSABUFwsabuf;DWORDdwSend;intlen;CStringstrHostName;SOCKADDR_INaddrTo;addrTo.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");addrTo.sin_family=AF_INET;addrTo.sin_port=htons(8080);GetDlgItemText(IDC_EDIT_SEND,strSend);len=strSend.GetLength();wsabuf.buf=strSend.GetBuffer(len);wsabuf.len=len+1;if(SOCKET_ERROR==WSASendTo(m_socket,&wsabuf,1,&dwSend,0,(SOCKADDR*)&addrTo,sizeof(SOCKADDR),NULL,NULL)){inti=WSAGetLastError();MessageBox("发送数据失败!");closesocket(m_socket);WSACleanup();return;}else{MessageBox("发送数据成功!");}Display(strSend);}voidCChatDlg::OnConnect(){m_socket=WSASocket(AF_INET,SOCK_STREAM,0,NULL,0,WSA_FLAG_OVERLAPPED);if(INVALID_SOCKET==m_socket){MessageBox("创建套接字失败!");closesocket(m_socket);//returnFALSE;}SOCKADDR_INaddrTo;addrTo.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");addrTo.sin_family=AF_INET;addrTo.sin_port=htons(8080);intiResult=connect(m_socket,(SOCKADDR*)&addrTo,sizeof(addrTo));if(iResult==SOCKET_ERROR){inti=WSAGetLastError();MessageBox("连接失败!");closesocket(m_socket);WSACleanup();}else{MessageBox("连接成功!");}}