By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,673 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,673 IT Pros & Developers. It's quick & easy.

SocketException

P: n/a
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.
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Amit Yadav <am*****@hotmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #2

P: n/a
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.

-----Original Message-----
Amit Yadav <am*****@hotmail.com> wrote:
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 youtry to connect with the client? Please post a short but complete pairof programs which demonstrate the problem. See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
.

Nov 13 '05 #3

P: n/a
Amit <am*****@hotmail.com> wrote:
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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #4

P: n/a
Thanks jon, That was the stupdist mistake i could ever
make.

Amit.
-----Original Message-----
Amit <am*****@hotmail.com> wrote:
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 toput all the using statements in etc having seen this) it looks like theproblem 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 :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
.

Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.