0

i have a problem comunicating with a java server socket with my client c# socket. The problem came from when i try to read the response. My code is this:

IPHostEntry IPHost = Dns.Resolve("IP_ADDRESS");
Console.WriteLine(IPHost.HostName);
string []aliases = IPHost.Aliases; 
IPAddress[] addr = IPHost.AddressList;
Console.WriteLine(addr[0]);
EndPoint ep = new IPEndPoint(addr[0],1024); 
Socket sock =
   new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
sock.Connect(ep);
if(sock.Connected)
   Console.WriteLine("OK");
Encoding ASCII = Encoding.ASCII;
string Get = "A";
Byte[] ByteGet = ASCII.GetBytes(Get);
Byte[] RecvBytes = new Byte[256];
sock.Send(ByteGet, ByteGet.Length, 0);
Int32 bytes = sock.Receive(RecvBytes, RecvBytes.Length, 0);
Console.WriteLine(bytes);
String strRetPage = null;
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
while( bytes > 0 ) {
   bytes = sock.Receive(RecvBytes, RecvBytes.Length, 0);
   strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
   Console.WriteLine(strRetPage );
}
sock.Close();

at the code line

Int32 bytes = sock.Receive(RecvBytes, RecvBytes.Length, 0);

my client hang. I have to stop the application. It seems that don't respond the socket server. The specific of the socket server is to send and receive a BitStream.

Alfabravo
  • 7,493
  • 6
  • 46
  • 82
Alle
  • 1
  • 1
  • A socket is by default _blocking_, so unless you send anything the `Receive` call will block (i.e. "hang"). – Some programmer dude Oct 22 '12 at 07:33
  • i send with the command sock.Send(ByteGet, ByteGet.Length, 0); – Alle Oct 22 '12 at 07:35
  • Yes you send _to_ the server, but does the server send _back_? I recommend using a tool such as [Wireshark](http://www.wireshark.org/) to see that data really travels back and forth as it should. – Some programmer dude Oct 22 '12 at 07:43
  • I used wireshark and the server return me back but it seems that the client socket in c# cannot read the data, i don't know if a compatibility problem or something i also supposed that the client cannot read the data because is too small ... – Alle Oct 22 '12 at 07:54

1 Answers1

1

Without the Java code of your server it's hard to tell, but it seems very likely your server is not sending the expected 256 bytes. Since Socket.Receive is blocking by default the client keeps waiting for data.

Tim Lamballais
  • 1,056
  • 5
  • 10
  • The `Receive` call will reead _up_ to the number of bytes requested, but may read less if more is not available. – Some programmer dude Oct 22 '12 at 08:07
  • `while (bytes > 0)` I guess, the condition is never met, so Tim is right: the client waits forever. `read` may read less than expected, yes. But there has to be at least *something* or the connection being closed to "unblock" the `read`, AFAIK. – Fildor Oct 22 '12 at 08:57
  • so ... i can use Byte[] RecvBytes = new Byte[1]; 1 Byte ? – Alle Oct 22 '12 at 10:30
  • How i can know the right size of byte to read ? can be possible that the socket server send me not a byte but some bit ? and with the c# socket client i will never able to read the response ? i'm afraid ... about this – Alle Oct 22 '12 at 13:12
  • I would start with checking if the server is sending anything at all, and whether the socket is being closed or not. – Tim Lamballais Oct 22 '12 at 13:17
  • nope ... i checked with wireshark, but when i try the method Receive the application hang ! anyone had the same problem ? – Alle Oct 27 '12 at 16:31