问题描述
运行以下代码正常,但点BTN开启SOCKET服务候提示“在其上下文中,该请求的地址无效,System”,请问这是什么问题?谢谢!ImportsSystem.NetImportsSystem.Net.SocketsImportsSystem.ThreadingImportsSystem.TextPublicClassForm1PrivateobjSocketServerAsSocketServerPublicSubNew()objSocketServer=NewSocketServer()'此调用是Windows窗体设计器所必需的。InitializeComponent()'在InitializeComponent()调用之后添加任何初始化。EndSubPrivateSubBTN_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesBTN.ClickobjSocketServer.BindIP=txtIP.Text.Trim()objSocketServer.BindPort=Integer.Parse(txtPort.Text)objSocketServer.Run()EndSubPrivateSubBTNStop_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesBTNStop.ClickobjSocketServer.StopRun()EndSubEndClassPublicClassSocketServerPrivatem_BindIpAsStringPrivatem_BindPortAsStringPrivateRunStateAsBooleanPrivatearyClientListAsArrayListPrivateSckServerAsSocketPrivateDoneAsManualResetEventPublicSubNew()m_BindIp=""m_BindPort=0RunState=FalsearyClientList=NewArrayList()Done=NewManualResetEvent(False)EndSubPublicPropertyBindIP()AsStringGetReturnm_BindIpEndGetSet(ByValvalueAsString)m_BindIp=valueEndSetEndPropertyPublicPropertyBindPort()AsIntegerGetReturnm_BindIpEndGetSet(ByValvalueAsInteger)m_BindIp=valueEndSetEndPropertyPublicSubStopRun()RunState=FalseSckServer.Close()EndSubPublicSubRun()DimipLocalAsIPAddressDimintListenPortAsIntegerDimipepServerAsIPEndPointipLocal=IPAddress.Parse(m_BindIp.Trim())intListenPort=m_BindPortipepServer=NewIPEndPoint(ipLocal,intListenPort)SckServer=NewSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp)TrySckServer.Bind(ipepServer)SckServer.Listen(100)DimthdAcceptAsNewThread(AddressOfThreadBeginAccept)thdAccept.Start()CatchexAsExceptionMessageBox.Show("错误描述"&ex.Message&vbCrLf&"错误源"&ex.Source,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error)ExitSubEndTryEndSubPrivateSubThreadBeginAccept()RunState=TrueDoDone.Reset()SckServer.BeginAccept(NewAsyncCallback(AddressOfOnConnectionRequest),SckServer)Done.WaitOne()LoopWhileRunState=TrueEndSubPrivateSubOnConnectionRequest(ByValarAsIAsyncResult)Done.Set()DimListenerAsSocket=CType(ar.AsyncState,Socket)NewConnection(Listener.EndAccept(ar))Listener.BeginAccept(NewAsyncCallback(AddressOfOnConnectionRequest),Listener)EndSubPrivateSubNewConnection(ByValsckClientAsSocket)DimobjSocketClientAsNewSocketClient(sckClient)Debug.Print(objSocketClient.Sock.RemoteEndPoint.ToString()&"已经连接"&vbCrLf)aryClientList.Add(objSocketClient)objSocketClient.ReceiveCallback(Me)EndSubPublicSubOnReceiveData(ByValarAsIAsyncResult)DimobjSocketClientAsSocketClientobjSocketClient=CType(ar.AsyncState,SocketClient)DimbytReceiveData()AsByte=objSocketClient.GetReceiveData(ar)'判断远程客户端是否断开连接IfbytReceiveData.Length<1ThenDebug.Print(objSocketClient.Sock.RemoteEndPoint.ToString()&"已经断开"&vbCrLf)objSocketClient.Sock.Close()aryClientList.Remove(objSocketClient)ExitSubEndIfDimxAsString=Encoding.ASCII.GetString(bytReceiveData).Trim()Debug.Print(vbCrLf&"接受数据"&x&vbCrLf)IfLeft(x,x.Length)="123456"ThenDimSendBuff()AsByte=Encoding.ASCII.GetBytes("567890".ToCharArray())objSocketClient.Sock.Send(SendBuff,SendBuff.Length,SocketFlags.None)EndIfobjSocketClient.ReceiveCallback(Me)EndSubEndClassPublicClassSocketClientPrivatem_sockAsSocketPrivateRecvBuff(256)AsBytePublicSubNew(ByValsckSockAsSocket)m_sock=sckSockEndSubPublicReadOnlyPropertySock()AsSocketGetReturnm_sockEndGetEndPropertyPublicSubReceiveCallback(ByValobjSocketServerAsSocketServer)DimReceiveDataAsAsyncCallbackTryReceiveData=NewAsyncCallback(AddressOfobjSocketServer.OnReceiveData)m_sock.BeginReceive(RecvBuff,0,RecvBuff.Length,SocketFlags.None,ReceiveData,Me)CatchexAsExceptionEndTryEndSubPublicFunctionGetReceiveData(ByValarAsIAsyncResult)AsByte()DimintByteLenAsIntegerDimbytReturnAsByte()intByteLen=0TryintByteLen=m_sock.EndReceive(ar)CatchexAsExceptionEndTryReDimbytReturn(intByteLen-1)Array.Copy(RecvBuff,bytReturn,intByteLen)ReturnbytReturnEndFunctionEndClass
解决方案
解决方案二:
ding
解决方案三:
Mark