after opening socket, sending data then closing socket 3000 times i get "Only one usage of each sock

D

Daniel

after opening socket, sending data then closing socket 3000 times i get
"Only one usage of each socket address"

what am i doing wrong? is there some thing else i need to do to free up the
socket after i send data into it?

I simply want to open socket, send data, close socket and have the server
just handle one client thread to recieve connection, recieve data, and close
socket


here is my client:

for(int i=0;i<10000;i++)

{

TcpClient myclient;

myclient = new TcpClient("localhost",8888);

NetworkStream networkStream ;

networkStream = myclient.GetStream();

StreamWriter streamWriter ;

streamWriter = new StreamWriter(networkStream);

string strData = "";

strData += "0\0";

streamWriter.WriteLine(strData);

streamWriter.Flush();

streamWriter.Close() ;

networkStream.Close();

myclient.Close();

int i23 = 23+ 23;

}


here is my server:
using System;

using System.Net.Sockets;

using System.IO ;

public class Echoserver

{

public static void Main()

{

TcpListener tcpListener = new TcpListener(8888);

tcpListener.Start();

Console.WriteLine("Server Started") ;

while(true)

{

Socket socketForClient = tcpListener.AcceptSocket();

try

{

if(socketForClient.Connected)

{

Console.WriteLine("Client connected");

NetworkStream networkStream = new NetworkStream(socketForClient);

StreamReader streamReader = new StreamReader(networkStream);

string line = streamReader.ReadLine();

Console.WriteLine("Read:" +line);

}

socketForClient.Close();

Console.WriteLine("Client disconnected");

}

catch(Exception e)

{

Console.WriteLine(e.ToString()) ;

}

}

}

}
 
C

Chris Botha

I would say that the problem is because you are in a tight loop. Put a timer
on a form and execute the code every time it expires.
 
D

Daniel

i wrote a c version of this using c sockets and i did not have this issue.
it seems that after the socket is closed it is not realy closed. i looked in
a tcp viewer and it shows the sockets staying open for about 60 seconds
after i close them, after 3000 it starts to error because they are all in
use when they should disapear right after i close socket unless im missing a
step? or is this a bug w/ TcpClient? any work around appreciated.
 
M

Mike Blake-Knox

i wrote a c version of this using c sockets and i did not have this issue.
it seems that after the socket is closed it is not realy closed. i looked in
a tcp viewer and it shows the sockets staying open for about 60 seconds
after i close them, after 3000 it starts to error because they are all in
use when they should disapear right after i close socket unless im missing a
step? or is this a bug w/ TcpClient? any work around appreciated.

TCP sockets don't close instantly. When you execute a close(), the closing is
only started (but not completed) before control is returned to your code. If
you were to use the netstat command you can see the state of the sockets on
your computer.

Take a look at http://tangentsoft.net/wskfaq/articles/debugging-tcp.html for
moe details.

Mike
 

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