问题描述
今天遇到一个问题,多个服务器,都知道IP,只有一个上位机,要做成服务器listening,等待上位机来connect,请问这样的程序应该怎么写?总不能一个线程connect(IP1);在一个线程connect(IP2);........
解决方案
解决方案二:
觉得你可能还不了解Tcp/Ip在.net中的实现,去看看Socket相关的类,做一个简单的例子
解决方案三:
这个微软有例子的
解决方案四:
能不能说得具体点?
解决方案五:
微软的例子不好虽然能运行但不好上网上找别的例子去
解决方案六:
privatestringReceiveMessage(){LogWritelogWrite=null;NetworkStreamWrapnetworkStreamWrap=null;try{byte[]arrbyteReceive=newByte[10];intiReceiveTimeOut=this.m_updateServerParas.ReceiveTimeOut;intiSendTimeOut=this.m_updateServerParas.SendTimeOut;this.m_socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReceiveTimeout,iReceiveTimeOut*1000);this.m_socket.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.SendTimeout,iSendTimeOut*1000);networkStreamWrap=newNetworkStreamWrap(this.m_socket,LOGDIR);networkStreamWrap.ReadX(arrbyteReceive,0,arrbyteReceive.Length,LOGDIR);intiMessageLen=GetMessageLen(arrbyteReceive);logWrite=newLogWrite("Update","Info","要接收的报文长度为"+iMessageLen.ToString(),"");arrbyteReceive=newByte[iMessageLen];networkStreamWrap.ReadX(arrbyteReceive,0,arrbyteReceive.Length,LOGDIR);return(Encoding.Default.GetString(arrbyteReceive));}catch(System.Exceptionex){if((exisSocketException)||(exisObjectDisposedException)){logWrite=newLogWrite("Update",ex,"接收客户端报文时出错。");thrownewCommunicationException("网络通讯错误:接收客户端报文时出错。");}else{logWrite=newLogWrite("Update",ex,"接收客户端报文时出错。");throw;}}finally{if(networkStreamWrap!=null){networkStreamWrap.Close();}}}
解决方案七:
privatevoidDealRequest(){LogWritelogWrite=null;try{stringsClientIp=((IPEndPoint)this.m_socket.RemoteEndPoint).Address.ToString();stringsConnectTime=DateTime.Now.ToString("yyyy-MM-ddHH:mm:ss");stringsMessage=ReceiveMessage();logWrite=newLogWrite("Update","Info","接收到客户端:"+sClientIp+"发来的报文:["+sMessage+"]","");stringsRequestType=sMessage.Substring(0,2);switch(sRequestType){case"00":stringsUpdateFilesList="";try{//sUpdateFilesList+=GetUpdateFilesList(Application.StartupPath);sUpdateFilesList+=GetUpdateFilesList();if(sUpdateFilesList!=""){sUpdateFilesList=sUpdateFilesList.Substring(0,sUpdateFilesList.Length-1);}}catch(GetUpdateFilesListExceptiongetEx){logWrite=newLogWrite("Update",getEx,"在获得更新文件列表时出错。");sUpdateFilesList+="Error";}logWrite=newLogWrite("Update","Info","要发送给客户端:"+sClientIp+"的报文为:["+sUpdateFilesList+"]");SendMessage("00"+sUpdateFilesList);break;case"01":stringsFileName=sMessage.Substring(2);FileStreamWrapfileStreamWrap=null;BinaryReaderbReader=null;NetworkStreamWrapnetworkStreamWrap=null;try{fileStreamWrap=newFileStreamWrap(Application.StartupPath+"\"+sFileName,FileMode.Open,FileAccess.Read,FileShare.Read,LOGDIR);bReader=newBinaryReader(fileStreamWrap.fileStream);System.Int64iFileLen=bReader.BaseStream.Length;StringsMsgLen=((Int64)(iFileLen+19+2+1)).ToString();DateTimetimeFile=FileWrap.GetLastWriteTime(Application.StartupPath+"\"+sFileName,LOGDIR);StringsSend=sMsgLen.PadLeft(10,'0')+"01"+timeFile.ToString("yyyy-MM-ddHH:mm:ss")+"|";logWrite=newLogWrite("Update","Info","发送给客户端:"+sClientIp+"的文件内容报文中,前段的报文内容:["+sSend+"]");byte[]arrbyteSend=Encoding.Default.GetBytes(sSend);networkStreamWrap=newNetworkStreamWrap(this.m_socket,LOGDIR);networkStreamWrap.Write(arrbyteSend,0,arrbyteSend.Length,LOGDIR);logWrite=newLogWrite("Update","Info","成功将文件内容报文中前段的报文内容发送给客户端:"+sClientIp);logWrite=newLogWrite("Update","Info","开始将文件:"+sFileName+"的内容发送给客户端:"+sClientIp);arrbyteSend=newByte[iFileLen];bReader.Read(arrbyteSend,0,arrbyteSend.Length);networkStreamWrap.Write(arrbyteSend,0,arrbyteSend.Length,LOGDIR);logWrite=newLogWrite("Update","Info","成功将文件:"+sFileName+"的内容发送给客户端:"+sClientIp);}catch(FileAccessExceptionfileAccessEx){logWrite=newLogWrite("Update",fileAccessEx,"文件访问错误,因此将空报文返回给客户端。");SendMessage("01");}catch(StreamAccessExceptionstreamAccessEx){logWrite=newLogWrite("Update",streamAccessEx,"文件访问错误,因此将空报文返回给客户端。");SendMessage("01");}finally{if(bReader!=null){bReader.Close();}if(fileStreamWrap!=null){fileStreamWrap.Close();}if(networkStreamWrap!=null){networkStreamWrap.Close();}}break;
看得懂不?
解决方案八:
protectedoverridevoidOnStart(string[]args){try{LogInfologInfo=null;LogOperatelogOperate=newLogOperate(LOGDIR);LogWritelogWrite=null;try{logInfo=newLogInfo("开始启动自动更新服务","");logOperate.Write(logInfo);this.m_serverParas=newServerParas(this.m_sServerConfigFile);ThreadWrapthreadListenWrap=newThreadWrap(newThreadStart(Listen),LOGDIR);threadListenWrap.thread.IsBackground=true;threadListenWrap.Start(LOGDIR);logInfo=newLogInfo("成功启动自动更新服务","");logOperate.Write(logInfo);}catch(System.Exceptionex){logWrite=newLogWrite("Update",ex,"在启动自动更新服务时出错。");throwex;}}catch{}}protectedoverridevoidOnStop(){try{LogOperatelogOperate=null;LogInfologInfo=newLogInfo("停止自动更新服务","");logOperate=newLogOperate("Update");;logOperate.Write(logInfo);}catch{}}privatevoidListen(){try{LogWritelogWrite=null;try{TcpListenerWraptcpListenerWrap=newTcpListenerWrap(int.Parse(this.m_serverParas.updateServerParas.ListenPort),LOGDIR);tcpListenerWrap.Start(LOGDIR);while(true){SocketsocketAccept=tcpListenerWrap.AcceptSocket(LOGDIR);UpdateUserConnectionuserConnection=newUpdateUserConnection(socketAccept,this.m_serverParas.updateServerParas);userConnection.Start();}}catch(System.Exceptionex){logWrite=newLogWrite("Update",ex,"自动更新服务在监听客户端连接时出错。");throwex;}}catch{}}
在给你补个
解决方案九:
你这个是上位机还是服务器,我是想说得是上位机是客户端,我们现在要求是有很多个装置,每个装置都有个固定的IP,上位机是TcpClient。
解决方案十:
该回复于2012-01-19 13:52:39被版主删除