TCP Socket not working - Logic issue?

  • Thread starter Thread starter Gravity
  • Start date Start date
G

Gravity

Hi,

I have a TCP server running on my desktop. While another TCP client running
on my laptop. Both written in raw C/C++ and they work perfectly fine.

Now I am developing socket application using C# on the same laptop as TCP
client.

It basically connect to the server, send a packet and recv the data. But
what happen is that there is completely no activities going on when I trigge
the function using button. My desktop network activities also complete IDLE
despice several clicks from my C# clients.

Here is my basic logic of codes. I guess there must be some silly mistakes.
Hope you can pin point to me.

Thanks for reading.


MemoryStream f_MemoryStream = new MemoryStream(20);


TcpClient f_Client = new TcpClient();


Byte[] f_DataSend = new Byte[50000];

Byte[] f_DataRecv = new Byte[50000];

f_Client.Connect("192.168.1.2", 8000);

NetworkStream f_Stream = f_Client.GetStream();


f_Stream.Write(f_DataSend, 0, f_DataSend.Length);




Int32 f_LengthRecv = f_Stream.Read(f_DataRecv, 0, f_DataRecv.Length);

f_Client.Close();

f_Stream.Close();

f_MemoryStream.Close();
 
Hi,

This should work... strange...
maybe when you put nothing in the array it won't send a packet???
Try this:
MemoryStream f_MemoryStream = new MemoryStream(20);


TcpClient f_Client = new TcpClient();


Byte[] f_DataSend = new Byte[50000];

Byte[] f_DataRecv = new Byte[50000];

f_Client.Connect("192.168.1.2", 8000);

NetworkStream f_Stream = f_Client.GetStream();

f_DataSend[0] = 128;
f_DataSend[1] = 128;
f_Stream.Write(f_DataSend, 0, f_DataSend.Length);




Int32 f_LengthRecv = f_Stream.Read(f_DataRecv, 0, f_DataRecv.Length);

f_Client.Close();

f_Stream.Close();

f_MemoryStream.Close();

You can also use the underlying socket and don't use streams... (I do that
most of the time)

TcpClient f_Client = new TcpClient();
Byte[] f_DataSend = new Byte[50000];
Byte[] f_DataRecv = new Byte[50000];
f_Client.Connect("192.168.1.2", 8000);
f_Stream.TcpClient.Send(f_DataSend);
...

But I would put something inside that array first.

Best Regards,

Jeroen Vandezande
 
TcpClient f_Client = new TcpClient();
Byte[] f_DataSend = new Byte[50000];
Byte[] f_DataRecv = new Byte[50000];
f_Client.Connect("192.168.1.2", 8000);
f_Stream.TcpClient.Send(f_DataSend);


whoops...

that should be:

TcpClient f_Client = new TcpClient();
Byte[] f_DataSend = new Byte[50000];
Byte[] f_DataRecv = new Byte[50000];
f_Client.Connect("192.168.1.2", 8000);
f_Client.TcpClient.Send(f_DataSend);
 
Hi,

I only see the last line to be different.

However, in my "f_Client", there is no "TcpClient" member function in
compact .net.

Things getting more confussing now..... problem is that it seem right, but
no activities. : (

Jeroen Vandezande said:
TcpClient f_Client = new TcpClient();
Byte[] f_DataSend = new Byte[50000];
Byte[] f_DataRecv = new Byte[50000];
f_Client.Connect("192.168.1.2", 8000);
f_Stream.TcpClient.Send(f_DataSend);


whoops...

that should be:

TcpClient f_Client = new TcpClient();
Byte[] f_DataSend = new Byte[50000];
Byte[] f_DataRecv = new Byte[50000];
f_Client.Connect("192.168.1.2", 8000);
f_Client.TcpClient.Send(f_DataSend);
 
In fact, in my actual codes, I do have something for it to send... Just no
response.

By the way, I notice that there is no return value from either functions to
indicate the results? Or its simply did not documented?

Jeroen Vandezande said:
Hi,

This should work... strange...
maybe when you put nothing in the array it won't send a packet???
Try this:
MemoryStream f_MemoryStream = new MemoryStream(20);


TcpClient f_Client = new TcpClient();


Byte[] f_DataSend = new Byte[50000];

Byte[] f_DataRecv = new Byte[50000];

f_Client.Connect("192.168.1.2", 8000);

NetworkStream f_Stream = f_Client.GetStream();

f_DataSend[0] = 128;
f_DataSend[1] = 128;
f_Stream.Write(f_DataSend, 0, f_DataSend.Length);




Int32 f_LengthRecv = f_Stream.Read(f_DataRecv, 0, f_DataRecv.Length);

f_Client.Close();

f_Stream.Close();

f_MemoryStream.Close();

You can also use the underlying socket and don't use streams... (I do that
most of the time)

TcpClient f_Client = new TcpClient();
Byte[] f_DataSend = new Byte[50000];
Byte[] f_DataRecv = new Byte[50000];
f_Client.Connect("192.168.1.2", 8000);
f_Stream.TcpClient.Send(f_DataSend);
...

But I would put something inside that array first.

Best Regards,

Jeroen Vandezande
 
Instead of using the TcpClient have you tried just using the Socket class?
Also try doing a flush after your write. Maybe it's buffering it up for some
reason.

Gravity said:
In fact, in my actual codes, I do have something for it to send... Just no
response.

By the way, I notice that there is no return value from either functions to
indicate the results? Or its simply did not documented?

Jeroen Vandezande said:
Hi,

This should work... strange...
maybe when you put nothing in the array it won't send a packet???
Try this:
MemoryStream f_MemoryStream = new MemoryStream(20);


TcpClient f_Client = new TcpClient();


Byte[] f_DataSend = new Byte[50000];

Byte[] f_DataRecv = new Byte[50000];

f_Client.Connect("192.168.1.2", 8000);

NetworkStream f_Stream = f_Client.GetStream();

f_DataSend[0] = 128;
f_DataSend[1] = 128;
f_Stream.Write(f_DataSend, 0, f_DataSend.Length);




Int32 f_LengthRecv = f_Stream.Read(f_DataRecv, 0, f_DataRecv.Length);

f_Client.Close();

f_Stream.Close();

f_MemoryStream.Close();

You can also use the underlying socket and don't use streams... (I do that
most of the time)

TcpClient f_Client = new TcpClient();
Byte[] f_DataSend = new Byte[50000];
Byte[] f_DataRecv = new Byte[50000];
f_Client.Connect("192.168.1.2", 8000);
f_Stream.TcpClient.Send(f_DataSend);
...

But I would put something inside that array first.

Best Regards,

Jeroen Vandezande
 
By the way, I notice that there is no return value from either functions
to indicate the results? Or its simply did not documented?

If you don't get an exception it worked fine...
where you catch the exception you can look at the errorcodes.

Best Regards,

Jeroen Vandezande
 
I was having exception in other codes.

I am currently troubleshooting the codes.

It is the C# nature that do not practise return value checking for each
function call?
 
Gravity said:
I was having exception in other codes.

I am currently troubleshooting the codes.

It is the C# nature that do not practise return value checking for each
function call?

Hi,

You mean that every function call returns 0 when it worked properly and
another code when something went wrong?
Indeed in C# or any .net language this behaviour is handled by exceptions.
note that exceptions are NOT program flow modifiers... so it is only for
functions that normally don't return anything except when an errorcondition
occurs.

Best Regards,

Jeroen Vandezande.
 
I aware of that now. Need to get used to it.


Jeroen Vandezande said:
Hi,

You mean that every function call returns 0 when it worked properly and
another code when something went wrong?
Indeed in C# or any .net language this behaviour is handled by exceptions.
note that exceptions are NOT program flow modifiers... so it is only for
functions that normally don't return anything except when an
errorcondition occurs.

Best Regards,

Jeroen Vandezande.
 
Back
Top