问题描述
我是像这样写的,但是遇到一个问题,就是我有一台电脑一方面要监听局域网的客户机,一方面还要连接公网(外网连接成功的),监听主要代码:privatevoidAcceptConnect(){intServerPoint=58888;IPAddress[]ip=Dns.GetHostAddresses(Dns.GetHostName());listener=newTcpListener(ip[0],ServerPoint);listener.Start();while(isExit==false){try{//让其他需要等待的线程阻塞//将事件的状态设为非终止allDone.Reset();//TcpClienttc=listener.AcceptTcpClient();//接受一个连接请求并返回TcpClient对象//NetworkStreamstm=tc.GetStream();//用GetStream方法创建NetworkStream对象//引用在异步操作完成时调用的回调方法AsyncCallbackcallback=newAsyncCallback(AcceptTcpClientCallback);//调用回调函数AcceptTcpClientCallback//listBoxStatus.Invoke(setListBoxCallback,"开始等待客户连接");//开始一个异步操作接受传入的连接尝试listener.BeginAcceptTcpClient(callback,listener);//接受一个传入连接allDone.WaitOne();//阻塞当前线程,直到收到信号}catch(Exceptionerr){//listBoxStatus.Invoke(setListBoxCallback,err.Message);break;}finally{}}}privatevoidAcceptTcpClientCallback(IAsyncResultar){try{//将事件状态设为终止状态,允许一个或多个等待线程继续//发送信号让阻塞的线程继续执行allDone.Set();//还原原始的TcpListener对象TcpListenermylistener=(TcpListener)ar.AsyncState;//异步接收传入的连接,并创建新的TcpClient对象处理远程主机通信TcpClientclient=mylistener.EndAcceptTcpClient(ar);//listBoxStatus.Invoke(setListBoxCallback,"已接受客户连接"+client.Client.RemoteEndPoint);//listBoxStatus.Items.Add("已接受客户连接"+client.Client.RemoteEndPoint);comboBox1.Invoke(setComboxItemsCallback,client.Client.RemoteEndPoint.ToString());//comboBox1.Items.Add(client.Client.RemoteEndPoint.ToString());IPEndPointclie=(IPEndPoint)client.Client.RemoteEndPoint;stringipc=clie.Address.ToString();//获取计算机名称computername=System.Net.Dns.Resolve(ipc).HostName;cbcomputer.Invoke(setComputerItemsCallback,computername);clientListcomputer.Add(computername);ReadWriteObjectreadWriteObject=newReadWriteObject(client);clientList.Add(readWriteObject);readWriteObject.netStream.BeginRead(readWriteObject.readByes,0,readWriteObject.readByes.Length,ReadCallback,readWriteObject);}catch(Exceptionerr){return;}finally{}}privatevoidReadCallback(IAsyncResultar){ReadWriteObjectreadWriteObject=(ReadWriteObject)ar.AsyncState;try{intcount=readWriteObject.netStream.EndRead(ar);richTextBoxReceive.Invoke(setRichTextBoxCallback,System.Text.Encoding.UTF8.GetString(readWriteObject.readByes,0,count));if(isExit==false){readWriteObject.InitReadArray();readWriteObject.netStream.BeginRead(readWriteObject.readByes,0,readWriteObject.readByes.Length,ReadCallback,readWriteObject);}}catch(Exceptionerr){comboBox1.Invoke(removeComboxItemCallback,readWriteObject);}}连接外网的主要代码:publicvoidconnecth(){if(Conn.State==ConnectionState.Open){clienthotel=newTcpClient(AddressFamily.InterNetwork);AsyncCallbackrequestCallbackh=newAsyncCallback(RequestCallbackh);allDone.Reset();clienthotel.BeginConnect("外网IP",51888,requestCallbackh,clienthotel);allDone.WaitOne();Conn.Close();}Conn.Close();}privatevoidRequestCallbackh(IAsyncResultar){allDone.Set();try{clienthotel=(TcpClient)ar.AsyncState;clienthotel.EndConnect(ar);//listBoxStatus.Invoke(setListBoxCallback,string.Format("与服务器{0}连接成功",client.Client.RemoteEndPoint));networkStreamh=clienthotel.GetStream();ReadObjectreadObject=newReadObject(networkStreamh,clienthotel.ReceiveBufferSize);networkStreamh.BeginRead(readObject.bytes,0,readObject.bytes.Length,ReadCallbackh,readObject);}catch(Exceptionerr){return;}finally{}}privatevoidReadCallbackh(IAsyncResultar){try{ReadObjectreadObject=(ReadObject)ar.AsyncState;intcount=readObject.netStream.EndRead(ar);richTextBoxReceiveh.Invoke(setRichTextBoxCallbackh,System.Text.Encoding.UTF8.GetString(readObject.bytes,0,count));if(isExit==false){readObject=newReadObject(networkStreamh,clienthotel.ReceiveBufferSize);networkStreamh.BeginRead(readObject.bytes,0,readObject.bytes.Length,ReadCallbackh,readObject);}}catch(Exceptionerr){}finally{}}这时候局域网的客户机就连接不到这台监听的机器了。但如果是没有连接到外网的时候,局域网的客户机可以连接到这台监听的机器???有人可以告诉我为什么吗?我用的是C#编写的。我想是连接和监听共同占用某个东西,可是我不知道是什么?希望大家知道的帮帮忙,O(∩_∩)O谢谢
解决方案
解决方案二:
解决方案三:
监听和连接不要用一个socket,把两者的端口号区分开