C# TCP IP Connection

本文关键字:Connection IP TCP | 更新日期: 2023-09-27 18:05:18

我已经使用了这个链接。当我尝试连接两台不同的机器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

我试着在同一台机器上运行它们,它工作了。

C# TCP IP Connection

监听器的地址需要是您自己的主机地址之一。TcpListener的第一个参数不是您想要连接的地址。

我的服务器代码(使用我的机器的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);
    }