I'm starting to dip my toes in the world of network programming and I've recently come across a fairly old sample from a lecturers tutorial (Or so I'm told).
We tried it on the university computers but it wouldn't work so the lecturer assumed it was down to a security setting either by Windows 7 or by the university computer systems.
Eager to find the cause I decided to run the same code on my own computer at home and to no surprise it didn't work.
There are two projects in a solution, one acts as the client and another the server. Once the server and client are both connected to each other the client sends a simple string to the server which is printed to the console. After this the appropriate connections are closed and the application gracefully exits.
The applications work as far as the server confirming that it has connected to the client yet it throws an exception (That is caught and handled) with the text:
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. - (System.IO.IOException)
Now as I'm just starting out with network programming with C# I'm not exactly sure where to look, my lecturer said that he would find out for next week's lecture with the cause and solution to the problem but I wanted to take some initiative and find out myself.
I've added the Client.cs and the Server.cs classes just in case they are helpful, the debugger suggests the cause lies at line 27 in Server.cs, the call to streamReader.Readline();
Note: This isn't in any way homework, I'm simply curious as to why it doesn't work. Call it a learning experience =]
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()) ;
}
}
}
}