C#TCP IP连接

时间:2014-05-25 16:14:30

标签: c# tcp client-server

我已使用此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

我试过在同一台机器上运行它们并且它有效。 如何解决?

1 个答案:

答案 0 :(得分:1)

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

我的服务器代码(使用我的机器的IP或Any)(我没有在我的局域网上的两台机器上尝试过):

    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);
    }