【问题标题】:C# TCP IP Connection [duplicate]C# TCP IP 连接 [重复]
【发布时间】:2014-05-25 16:14:30
【问题描述】:

我用过这个link。当我尝试连接两台不同的机器 192.168.1.4 和 192.168.1.9 时。我在服务器代码中更改了这部分

TcpListener myList = new TcpListener(IPAddress.Any, 27505);

而不是

IPAddress ipAd = IPAddress.Parse("192.168.1.9");
TcpListener myList = new TcpListener(ipAd, 27505);

我在客户端收到此错误

Unhandled Exception: System.Net.Sockets.SocketException: A connection attempt fa
iled because the connected party did not properly respond after a period of time
, or established connection failed because connected host has failed to respond
192.168.1.4:8001
   at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
   at Client.Client.send() in C:\Users\John\Documents\Visual Studio 2010\Project
s\Server Client\Client\Client.cs:line 111
   at Client.Client.Main(String[] args) in C:\Users\John\Documents\Visual Studio
 2010\Projects\Server Client\Client\Client.cs:line 147

我尝试在同一台机器上同时运行它们,并且成功了。 如何解决?

【问题讨论】:

  • 缺少错误。请修复,以便我们诊断问题。
  • 这不是错误,它只是错误附带的堆栈跟踪的一小部分。
  • 抱歉,David 我没注意到,我已经编辑过了
  • 我发现问题出在我的windows防火墙上,我不知道如何关闭这个问题。非常感谢大家

标签: c# tcp client-server


【解决方案1】:

您的侦听器地址必须是您自己的主机地址之一。 TcpListener 的第一个参数不是您要连接的地址。

我的服务器代码(可以使用我机器的 IP 或任何 IP)(我没有在我的 LAN 上的两台机器上尝试过):

    try {
        IPAddress ipAd = IPAddress.Parse("192.168.0.100");
         // use local m/c IP address, and 
         // use the same in the client

/* Initializes the Listener */
        // TcpListener myList=new TcpListener(IPAddress.Any,8001);
        TcpListener myList=new TcpListener(ipAd,8001);

/* Start Listeneting at the specified port */        
        myList.Start();

        Console.WriteLine("The server is running at port 8001...");    
        Console.WriteLine("The local End point is  :" + 
                          myList.LocalEndpoint );
        Console.WriteLine("Waiting for a connection.....");

        Socket s=myList.AcceptSocket();
        Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);

        byte[] b=new byte[100];
        int k=s.Receive(b);
        Console.WriteLine("Recieved...");
        for (int i=0;i<k;i++)
            Console.Write(Convert.ToChar(b[i]));

        ASCIIEncoding asen=new ASCIIEncoding();
        s.Send(asen.GetBytes("The string was recieved by the server."));
        Console.WriteLine("\nSent Acknowledgement");
/* clean up */            
        s.Close();
        myList.Stop();

    }
    catch (Exception e) {
        Console.WriteLine("Error..... " + e.StackTrace);
    }    

我的客户代码:

        try {
        TcpClient tcpclnt = new TcpClient();
        Console.WriteLine("Connecting.....");

        tcpclnt.Connect("192.168.0.100",8001);
        // use the ipaddress as in the server program

        Console.WriteLine("Connected");
        Console.Write("Enter the string to be transmitted : ");

        // String str=Console.ReadLine();
        Stream stm = tcpclnt.GetStream();

        ASCIIEncoding asen= new ASCIIEncoding();
        byte[] ba=asen.GetBytes("hello from client");
        Console.WriteLine("Transmitting.....");

        stm.Write(ba,0,ba.Length);

        byte[] bb=new byte[100];
        int k=stm.Read(bb,0,100);

        for (int i=0;i<k;i++)
            Console.Write(Convert.ToChar(bb[i]));

        tcpclnt.Close();
    }

    catch (Exception e) {
        Console.WriteLine("Error..... " + e.StackTrace);
    }

【讨论】:

  • 你的意思是服务器正在监听错误的地址。服务器不应该监听所有可能的地址吗?
  • 是的,这就是 IPAddress.Any 所做的。你指定的地址是192.168.1.9,这是执行代码的机器吗?
  • 192.168.1.9 是我尝试连接到 192.168.1.4(服务器)的客户端计算机。希望我回答了你的问题,我不太明白你的评论
  • 我可能需要查看一些代码。我可能误解了这个问题。
  • 完整代码在链接中。如果你想通过上面提到的方式修改后看到我的代码,我该如何发送给你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 2014-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多