"Unable to read data from transport connection" - C #

I am starting to plunge into the world of network programming, and recently I came across a rather old sample from a textbook of lecturers (or, as I was told).

We tried it on university computers, but it didn’t work, so the lecturer suggested that this was due to the security settings of either Windows 7 or university computer systems.

Wanting to find the reason, I decided to run the same code on my own computer at home, and it is not surprising that this did not work.

There are two projects in the solution: one acts as a client and the other server. Once the server and client are both connected to each other, the client sends a simple string to the server, which prints to the console. After that, the corresponding connections are closed, and the application exits gracefully.

Applications work while the server confirms that it is connected to the client, but it throws an exception (which is caught and processed) with the text:

Unable to read data from the transport connection: the existing connection was forcibly closed by the remote host. - (System.IO.IOException)

Now, when I just start with network programming with C #, I'm not quite sure where to look, my lecturer said that he would learn next week a lecture with the cause and solution of the problem, but I wanted to take the initiative and find out about yourself.

Client.cs Server.cs , , , 27 Server.cs, streamReader.Readline();

. , , . =]

Client.cs

using System;
using System.Net.Sockets;
using System.IO;

namespace NetworkProgrammingTutorial1Client
{
    class Client
    {
        static void Main(string[] args)
        {
            TcpClient myclient;

            try
            {
                // Create a TcpClient to talk to the local host.
                myclient = new TcpClient("localhost", 1234);
            }
            catch
            {
                Console.WriteLine("Failed to connect to the server.");
                return;
            }

            // get a Network stream from the server
            NetworkStream networkStream = myclient.GetStream();
            StreamWriter streamWriter = new StreamWriter(networkStream);

            streamWriter.WriteLine("Have a message.");
            streamWriter.Flush();
        }
    }
}

Server.cs

using System;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace NetworkProgrammingTutorial1Server
{
    class Server
    {
        static void Main(string[] args)
        {
            // TcpListener is listening on the given port...                                                 {
            TcpListener tcpListener = new TcpListener(1234);
            tcpListener.Start();     
            Console.WriteLine("Server Started") ;                  
            // Accepts a new connection...
            Socket socketForClient = tcpListener.AcceptSocket();                   
            try
            {
                if (socketForClient.Connected)
                {
                    while (true)
                    {
                        Console.WriteLine("Client connected");
                        NetworkStream networkStream = new NetworkStream(socketForClient);
                        StreamReader streamReader = new StreamReader(networkStream);
                        string line = streamReader.ReadLine();
                        if (line == null)
                            break;
                        Console.WriteLine(line);
                    }
                }

                socketForClient.Close();               
                Console.WriteLine("Exiting...");
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString()) ;
            }
        }
    }
}
+5
1

:

TcpListener listener = new TcpListener(IPAddress.Any, 1234);
listener.Start();     

using (TcpClient client = listener.AcceptTcpClient())
using (StreamReader reader = new StreamReader(client.GetStream()))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}

listener.Stop();

1234, , StreamReader , , . .

, StreamReader ( ), TcpClient, StreamReader using, .


. :

using (TcpClient client = new TcpClient("localhost", 1234))
using (StreamWriter writer = new StreamWriter(client.GetStream()))
{
    writer.WriteLine("Have a message.");
    writer.Flush();

    writer.WriteLine("Have another message.");
    writer.Flush();
}
+1

All Articles