TCP Sockets errors

S

Safiiru no Baka

Hi i'm running a simple test.

App 1 (sender), this is a thread:
===========================================================
ASCIIEncoding encoder = new ASCIIEncoding();

Byte[] hugeData = new Byte[5000];
for (int i = 0; i < hugeData.Length; i++)
{
hugeData = 88;
}

while (true)
{
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(new
IPEndPoint(IPAddress.Parse(listenTcpAddress), sendTcpPort));
tcpClient.GetStream().Write(hugeData, 0,
hugeData.Length);
tcpClient.Close();
}

===========================================================


App2 (listener) also in a thread:
===========================================================
TcpListener tcpListener = new TcpListener(new
IPEndPoint(IPAddress.Parse(listenTcpAddress), listenTcpPort));
tcpListener.Start();

while (runListener)
{
if (tcpListener.Pending())
{
TcpClient tcpClient =
tcpListener.AcceptTcpClient();
Stream s = tcpClient.GetStream();
Byte[] buffer = new Byte[5000];
int bytes = s.Read(buffer, 0, buffer.Length);
string data = Encoding.ASCII.GetString(buffer, 0,
bytes);

tcpClient.Close();
}
Thread.Sleep(0);
}

===========================================================

After running for a few seconds, the App 1 crashes with a "No
connection could be made because the target machine actively refused
it" exception on tcpClient.Connect.

I am not sure what to proceed with... any help would be greatly
appreciated. Thanks.
 
P

Paul G. Tobey [eMVP]

I don't think that this is a sensible test. Why would you want to build and
destroy a socket in a loop like that in the first program? You probably
have too many clients attempting to connect at the same time, overflowing
the listening queue for the TcpListener. When you try to connect another
socket while that queue is full, you'll get the error you're seeing.

If you can explain 'why', maybe we can suggest something else to do, but the
obvious change is delaying the creation of another TcpClient for a while
after closing the previous one.

Paul T.
 
S

Safiiru no Baka

Hi Paul,

Sorry about my test, I was trying to understand why it was crashing due
to me sending a lot of data. You could think of it as a "stress" test
for heavy loads?

What I am trying to accomplish is know how much data and how often I
can send safely and if my crashes are due to "logic" error or hardware
limitations.

Thanks!
 
S

Safiiru no Baka

I forgot, if I try to keep only the "tcpClient.GetStream().Write" in
the loop without making and destroying connections, I am not receiving
the data in App 2.
 
P

Paul G. Tobey [eMVP]

Then you're doing something very wrong when you rewrite your code that way.

Are you getting the *connection* in App 2?

Paul T.
 
S

Safiiru no Baka

Hi Paul,

what is wrong with the code? How would i send a lot of "writes"
without having to re-create the connections?

App2 is getting the connections, it displays the data fine but
eventually crashes.
 
P

Paul G. Tobey [eMVP]

You'd establish the connection, then do the writes in a loop. You described
it, but didn't show any code for that case. I can't predict what you might
have done wrong.

I'm not interested in what happens with the code you've already posted.
It's not usable for anything. Please refer to your code which opens the
connection in a correct way and uses it, closing it only when it really
should be closed and then tell me if App2 is getting the connection.

Paul T.
 
S

Safiiru no Baka

Opening and writing in a loop:
=========================================================
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(new
IPEndPoint(IPAddress.Parse(listenTcpAddress), sendTcpPort));

Byte[] hugeData = new Byte[5000];
for (int i = 0; i < hugeData.Length; i++)
{
hugeData = 88;
}

while(true)
{
tcpClient.GetStream().Write(hugeData, 0,
hugeData.Length);
}

tcpClient.Close();
=========================================================

This is also a thread. I'm trying to make it so App 1 sends data
continuously to App 2. This isnt working as it is.

Thanks again
 
P

Paul G. Tobey [eMVP]

And presumably you have modified the other application's code to do
something different, right? I'd need to see that, too, of course.

Paul T.
 
S

Safiiru no Baka

Oh, so if App2 was written like this:
=========================================================
TcpListener tcpListener = new TcpListener(new
IPEndPoint(IPAddress.Parse(listenTcpAddress), listenTcpPort));
tcpListener.Start();

while (true)
{
if (tcpListener.Pending())
{
TcpClient tcpClient =
tcpListener.AcceptTcpClient();
Stream s = tcpClient.GetStream();
Byte[] buffer = new Byte[10000];
int bytes = s.Read(buffer, 0, buffer.Length);
string data = Encoding.ASCII.GetString(buffer, 0,
bytes);

results = data;
this.Invoke(new EventHandler(showResults));
}
Thread.Sleep(0);
}
=========================================================

Would it work? Because I changed it and it only receives the first
iteration of data then stops...
 
P

Paul G. Tobey [eMVP]

Work? I'd say that that would be completely wrong, but, depending on what
you know about the data coming across the network, it could conceivably be
right. That's going to receive 10000 bytes, maximum, then close the
connection to the client and return to a state where it's waiting for
someone to try to connect.

Paul T.
 
S

Safiiru no Baka

Hi Paul,

So after the read it closes the connection? How can I keep the
connection open and constantly polling for the data that is being
written (by App1)?
 
S

Safiiru no Baka

You mean for App2? I am not calling tcpClient.Close anymore so why is
the connection closing?
 
P

Paul G. Tobey [eMVP]

Right you are. I overlooked that. However, the code doesn't ever get back
to read again unless there is another connection. If you actually *read*
your own code and think about what happens, you'll see that it's very broken
for the general case. When a connection is made, you want to take that
socket that responds to a made connection and do the right thing. Trying to
handle connections and data reception, etc. in the same thread is a
challenge. What happens, for example, if a *second* socket connects and
wants to send some data?!

Paul T.
 
S

Safiiru no Baka

Hi Paul I apprecite your responses.

For now I want to keep it simple so I'll deal with a second connection
later.

For now, from what I see here's whats happening:

1- App2: Listens for connections
2- App1: Connects to App2
3- App1: Starts sending data
4- App2: Reads data sent
5- Goto 3

Since both threads are infinite loops, I should just keep getting data,
but that is not the case. All I want (for this example) is to get a
constant stream of data flowing between the two apps.
 
P

Paul G. Tobey [eMVP]

No, that's not what the code does now. When the first Read call returns,
you do the Invoke call, but you then drop back into the outer loop and
tcpListener.Pending will *never* return until someone else tries to connect
to the socket.

Paul T.
 
S

Safiiru no Baka

Hi, wow thats a problem ... How would I "drop back" to the thread from
the Invoke call?

I have rewritten the code for App2:
=========================================================
while (runListener)
{
//if (tcpListener.Pending())
//{
TcpClient tcpClient =
tcpListener.AcceptTcpClient();
Stream s = tcpClient.GetStream();
Byte[] buffer = new Byte[10000];
int bytes = s.Read(buffer, 0, buffer.Length);
string data = Encoding.ASCII.GetString(buffer, 0,
bytes);

results = "-->" + data;
this.Invoke(new EventHandler(showResults));

//}
Thread.Sleep(0);
}
=========================================================

Same exact code but note that I commented out looking for
"tcpListener.Pending()" but I am still getting the same behaviour (one
iteration only).
 

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

Similar Threads


Top