0

I have used this link. and when i try to connect two different machines 192.168.1.4 and 192.168.1.9. I changed this part in the server code

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

instead of

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

I get this error in the client

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

I have tried to run them both on the same machine and it worked. How to fix it ?

DavidG
  • 113,891
  • 12
  • 217
  • 223
john-salib
  • 724
  • 1
  • 12
  • 27

1 Answers1

1

The address of your listener needs to be one of your own host addresses. The first parameter for TcpListener is not the address you wish to connect to.

My server code (works with using either my machine's IP or Any) (I have not tried it on two machines on my 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);
    }    

My client code:

        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);
    }
tim
  • 1,371
  • 3
  • 19
  • 30
  • you mean that the server is listening on wrong address. shouldn't the server be listening to all possible addresses ? – john-salib May 25 '14 at 16:39
  • Yes, that is what IPAddress.Any does. You specified the address 192.168.1.9, is this the machine on which the code executed? – tim May 25 '14 at 16:48
  • 192.168.1.9 is the client machine i try to make connect to 192.168.1.4 (server). hope i answered your question, i didn't quite understand your comment – john-salib May 25 '14 at 16:51
  • I might have to see some code. I may be misunderstanding the issue. – tim May 25 '14 at 17:03
  • the full code is in the link. if you want to see my code after modification which by the way mentioned above, how can i send it to you. – john-salib May 25 '14 at 17:19
  • tim I appreciate your help. but i found that it was a problem of windows firewall. really thankful for your time – john-salib May 26 '14 at 12:03
  • @john-aziz57, glad you found the issue. – tim May 26 '14 at 13:29