473,378 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

SocketException

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
4 11035
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Daktar | last post by:
Dunno if this is correct place to post but: I've made a servlet for tomcat that sends binary data, since the files that I send takes up some it's likely that some people want to cancel it but...
3
by: J C | last post by:
Hi, I'm using UDPClient to make a simple DNS server. I notice that intermittently and unpredictibly I get: Unhandled Exception: System.Net.Sockets.SocketException: An existing connection...
9
by: Henrik | last post by:
I'm developing an application that uses two threads to communicate with two different devices on my network. One of the devices is reached through an API from the supplier of that device. The API...
0
by: oferns | last post by:
Hi, apologies for the cluncky title.... Using WinXpSP2 & VS2005 Express editions..... I am writing an asynchronous socket client and I am getting a SocketException error on creating the...
4
by: sd1978 | last post by:
Hi, I have placed a webservice in the webserver. When I access it from a webpage, default.aspx on a click of a button i get the following error: No connection could be made because the target...
5
by: PJ6 | last post by:
I can't figure this out. I have several references to style sheets and javascript files in my page header, the contents of which are served dynamically by my httphandler that serves *.res requests....
1
by: wesomon99 | last post by:
Hello, I have a JBoss web application (v4.04GA), run on an Apache Tomcat HTTP server (v5.5.17). It connects with a MySQL database (v4.1.10). My web application takes an XML file, creates a CSV file...
5
by: mc | last post by:
I have a function (shown below) that I call to check if a server is present. when a server is present, everything is ok. however when it fails. I get an SocketException fired. On my development...
2
by: XenReborn | last post by:
Hi, I wrote a simple socket application, which connects to a simple socket server application I also wrote, the server opens a socket, listens, accepts connections, sends back a string of...
4
by: Rainer Queck | last post by:
Hello NG, in my application I get a SocketException in my receive callback method, if the remote socket closes. What would be the right way to treat this exception? I want to keep my socket...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.