Java Socket Server and Client Socket c# issue

696 views Asked by At

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.

1

There are 1 answers

6
Tim Lamballais On

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.