none
TcpClient write error RRS feed

  • Question

  • Hi I am following article in Code Project. I am getting error at the point where client tries to send data to server (write() method). this is the server (TcpListener) code. I made change in port number from 8001 the author is using to some other number (44300) because 8001 did not work. When the Listener is started it shows

    The server is running at port 8001...
    The local End point is  :192.168.56.1:44300
    Waiting for a connection.....

    When the client is run it shows error after a couple of minutes:

    Connecting.....
    Connected
    Enter the string to be transmitted : hellow server
    Transmitting.....
    Error.....    at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offs
    et, Int32 size)
       at clnt.Main() in c:\users\user\Source\Repos\ConsoleApp2\ConsoleApp2\Program.
    cs:line 34
    Press any key to continue . . .

    I need help: any debugging ideas to find the cause of error? I tried to turn off Firewall but it did not help.

    using System;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;
    
    public class serv
    {
        public static void Main()
        {
            try
            {
                IPAddress ipAd = IPAddress.Parse("192.168.56.1"); //use local m/c IP address, and use the same in the client
    
                /* Initializes the Listener */
                TcpListener myList = new TcpListener(ipAd, 44300);
    
                /* 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);
            }
        }
    
    }
    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Net.Sockets;
    
    
    public class clnt
    {
    
        public static void Main()
        {
    
            try
            {
                TcpClient tcpclnt = new TcpClient();
                Console.WriteLine("Connecting.....");
    
                tcpclnt.Connect("192.168.56.1", 8001); // use the ipaddress as in the server program
    
                Console.WriteLine("Connected");
                Console.Write("Enter the string to be transmitted : ");
    
                String str = Console.ReadLine();
               NetworkStream stm = tcpclnt.GetStream();
    
                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] ba = asen.GetBytes(str);
                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);
            }
        }
    


    • Edited by kobosh3 Monday, October 1, 2018 12:04 AM
    Monday, October 1, 2018 12:03 AM

All replies

  • Use the same valid address and port in both of the programs.

    For local tests, you can use the IPAddress.Loopback address.

    Improve the error reporting:

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

    Show the error message.



    Monday, October 1, 2018 5:10 AM
  • Hi Enterprise Library msi not found,

    Thank you for posting here.

    I test the code. Based on my test, please use the server ip address and the same port. Every thing will be ok. Run the code on server first and then run the client to connect.

    Server:

    Client:

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 1, 2018 6:48 AM
    Moderator
  • أthanks Wendy I did not understand you. I changed the port for server as :

    TcpListener myList = new TcpListener(ipAd, 8001);

    It was: TcpListener myList = new TcpListener(ipAd, 44300);

    I get error  even before I run the client: 

    Error.....    at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, Soc
    ketAddress socketAddress)
       at System.Net.Sockets.Socket.Bind(EndPoint localEP)
       at System.Net.Sockets.TcpListener.Start(Int32 backlog)
       at System.Net.Sockets.TcpListener.Start()
       at serv.Main() in C:\Users\user\Source\Repos\ConsoleApp1\ConsoleApp1\Program.
    cs:line 18
    Press any key to continue . . .

    THIS is my problem. Using 8001 in the server does not work. 


    • Edited by kobosh3 Monday, October 1, 2018 10:37 AM
    Monday, October 1, 2018 10:35 AM
  • thanks Viorel

    same error. can you elaborate?

    Monday, October 1, 2018 10:42 AM
  • thanks this worked on my work laptop
    Monday, October 1, 2018 12:41 PM
  • Hi Enterprise Library msi not found,

    Use the same ip address and port number in your server side and client. Everything will be okay. This is what I test with your code in my computer.

    If your question has been solved, please make the solution as answer. This will make answer searching easier in the forum and be beneficial to community members.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Tuesday, October 9, 2018 3:21 AM
    Monday, October 1, 2018 12:47 PM
    Moderator