麻烦大神帮忙看看啊!!!!!!新手初学socket编程,写了服务器端和客户端测试代码。问题如题。在客户端使用本机实际ip 的时候出现的错误提示:No connection could be made because the target machine actively refused it代码如下服务器端:TcpListener listener = new TcpListener(IPAddress.Any,port);listener.Start();Console.Write("start listen\n");while(true){const int buffersize = 256;TcpClient client = listener.AcceptTcpClient();Console.Write("connect\n");NetworkStream clientStream = client.GetStream();byte[] buffer = new byte[buffersize];int readBytes = 0;readBytes = clientStream.Read(buffer, 0, buffersize);string request = Encoding.ASCII.GetString(buffer).Substring(0, readBytes);Console.Write(request);clientStream.Close();}客户端:TcpClient client = new TcpClient();IPAddress ip = IPAddress.Parse("127.0.0.1");client.Connect(ip, 748);NetworkStream stream = client.GetStream();string req = "test";byte[] requestBuffer = Encoding.ASCII.GetBytes(req);stream.Write(requestBuffer, 0, requestBuffer.Length);端口已打开,没有被占用,防火墙也关了。。
服务端用了路由器吧 做端口映射了吗 追问 我这个是在同一个局域网内的。并且我是在同一台电脑上测试啊。不用管路由器吧。。 追答 同一个局域网你使用公网ip就会过路由器 不信你tracert 用局域网ip就可以,公网ip就不行就是nat的毛病 在你的路由器里加上端口转发规则