Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Threading TCPlistener.AcceptSocket();

Status
Not open for further replies.

StuGee

Technical User
Jan 12, 2004
2
GB
I am trying to creating a system that has the TCPListener running on a thread the problem is each time the thread is run it produces an error saying that the port is already being listened at, not sure how to resolve this, the code I am using for the server is:

try
{
// Step 1: create TcpListener
listener = new TcpListener( 3000 );
// Step 2: TcpListener waits for connection request
listener.Start();
// Step 3: establish connection upon client request
if (listener.Pending())
{
MessageBox.Show("Sorry, no connection requests have arrived");
}
else
{
while (Listening ==true )
{
server.Text = "Waiting for connection\r\n";
// accept an incoming connection
connection = listener.AcceptSocket();
// create NetworkStream object associated with socket
socketStream = new NetworkStream( connection );
// create objects for transferring data across stream
writer = new BinaryWriter( socketStream );
reader = new BinaryReader( socketStream );
server.Text += "Connection received.\r\n";

I error occurs with the connection = listener.AcceptSocket();
Any help with how this could be resolved would be excellent thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top