SocketException

A

Amit Yadav

Hi,

I am trying to write a simple TCP Client Server program
which exchange some text messages.

Both of my Client and Server are running on the same
machine and hence i am using "localhost" when connecting
from the client side.

However i am getting the following Exception:

System.Net.Sockets.SocketException: No connection could
be made because the target machine actively refused it
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at System.Net.Sockets.TcpClient.Connect(IPEndPoint
remoteEP)
at System.Net.Sockets.TcpClient.Connect(IPAddress
address, Int32 port)
at System.Net.Sockets.TcpClient.Connect(String
hostname, Int32 port)
at System.Net.Sockets.TcpClient..ctor(String hostname,
Int32 port)

The Code is :

TcpClient client = new TcpClient("localhost",11000);


Can anybody explain what's going on and how do i correct
this ?

Thanks.

Amit.
 
J

Jon Skeet

Amit Yadav said:
I am trying to write a simple TCP Client Server program
which exchange some text messages.

Both of my Client and Server are running on the same
machine and hence i am using "localhost" when connecting
from the client side.

However i am getting the following Exception:

System.Net.Sockets.SocketException: No connection could
be made because the target machine actively refused it

Are you absolutely sure that the server has started running before you
try to connect with the client? Please post a short but complete pair
of programs which demonstrate the problem. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean.
 
A

Amit

Hi jon,

Here are the Server side and client side short code (i
have removed all the unneccery things ..)

public class Server : System.Windows.Forms.Form
{
private Socket connection;
private Thread readThread;
public Server()
{
InitializeComponent();
readThread = new Thread(new ThreadStart( RunServer ));
readThread.Start();
readThread.Name = "Server";
}

[STAThread]
static void Main()
{
Application.Run(new Server());
}

public void RunServer()
{
TcpListener listener;
try
{
listener = new TcpListener( 5001 );
listener.Start();
while(true)
{
connection = listener.AcceptSocket();
/* Here there is a loop which communicates with the
client*/
/* When user Terminates or Client Terminates we reach to
the following statment*/
connection.Close();
}
}
catch( Exception error )
{
MessageBox.Show(error.ToString(),"Error in the
Server");
}
}
}

/*-----------Client Side Code -------- */

public class Client : System.Windows.Forms.Form
{
private Thread readThread;

public Client()
{
InitializeComponent();
readThread = new Thread(new ThreadStart(
RunClient ));
readThread.Name = "Client" ;
readThread.Start();
}
[STAThread]
static void Main()
{
Application.Run(new Client());
}

private void Client_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
System.Environment.Exit( System.Environment.ExitCode );
}

public void RunClient()
{
TcpClient client;
try
{
client = new TcpClient("localhost",11000);
do
{
/* Client communication goes here ...
}while(...).
client.Close();
Application.Exit();
}
catch( Exception error)
{
MessageBox.Show(error.ToString(), "Error
in Client");
}
}
}
}


Again i Have removed all the variables from this shorter
version. My exception is getting caught at the last catch
section above.

Thanks for replying...

Amit.
 
J

Jon Skeet

Amit said:
Here are the Server side and client side short code (i
have removed all the unneccery things ..)

Well, most of them. You don't actually need it to be a GUI app at all,
as far as I can see. Thanks for getting rid of what you did though :)

Anyway, without actually running the code (I couldn't be bothered to
put all the using statements in etc having seen this) it looks like the
problem is:
listener = new TcpListener( 5001 );

Here you're listening on port 5001.
client = new TcpClient("localhost",11000);

Here you're trying to connect to port 11000.

Try connecting to the same port you're listening on :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top