I am using the following code to connect to a server using a socket.
I have three arguments being sent to the code, the server ip, port and the message.
The code works fine and creates a connection to the server on the specified ip and port but they are not seeing the proper message string.
Since I am using the writeUTF method the string is being converted to Unicode format before the server receives it and it is not able to parse it
properly. Is there a way to see what exactly is being sent throug the socket (out.writeUTF(message)).
Also what would I be using to send the string as it is via the socket. Help much appreciated..
// File Name CallClient.java
This is the string I am passing as an argument,
"00491111206001379TU4I01111111111111 1528Y 02103943PSWD1NEU01Law Office C. SoffarCLVN01ICEware 50169 VIO CD015 TEST SH011NM011ABATE RALPH PI018506690111974082235 AD0124395 COLLECTION LN23 FANTASY ISLAND IL60750 3PN01016232065555 RP0107000N IOD010201001 ENDS011"
The client code you posted works fine on my side. The problem must be on the server side. Just make sure you didn't use something other than DataInputStream.readUTF(), because unlike writeBytes(), writeUTF() adds two extra bytes at the beginning of your data that specify the number of bytes of your data.
Here is the minimal server I used:
As for checking the contents of the data, you can use a free sniffer such as Wireshark and examine the data field of the right TCP segment. Be advised though that capturing packets on the loopback interface under Windows does not work with this tool. But there is a simple workaround that requires adding only a temporary entry in the routing table of your machine: http://forums.whirlpool.net.au/forum-replies-archive.cfm/1037087.html
Joined: Mar 23, 2005
I will try using writeBytes... And as you said the problem is at the server side and I dont have control on the server.
Also as per my knowledge they are not using Java to read the text being sent through the socket. That is why I needed help to modify my client code so as to send plain ascii text instead of UTF.
I hope writeBytes should do it.
Joined: Mar 23, 2005
With help from Steve I was able to send and receive Data using the following code,
Now my problem is that I get the data back from the server only after it reaches the timeout period. ie after 5 minutes.. The server is sending a single line of output which is seen after 5 min.
Is there a way I can close the socket and print the output from the server before it reaches the timeout period.
Is there a alternative function which I can use instead of readLine().
Joined: May 19, 2010
Your call to in.readLine() will block until the server actually sends a string terminated with a newline character. There might be several reasons why the server doesn't send this newline character, but it is quite plausible that it is waiting for extra input from your client, or that it is confused because it didn't receive the data it was expecting and in the correct order. It may as well be that the server doesn't even intend to send a newline character to indicate the end of the data it sends you.
I may be wrong, but I think the main issue here is that you and the server are not "speaking the same language". You have to figure out exactly what the server is expecting from your client and in what format. For instance, perhaps the server is expecting that you insert some kind of delimiters between the different data elements you are sending? Or that you have to include the size of the data you are sending? Otherwise, it can be hard for the server to determine if all the necessary data has arrived and that it should start interpreting it.
Hopefully I am pointing you in the right direction...
Joined: Mar 23, 2005
Yes the server and the client are not speaking the same language. But the server is able to interpret the string I am sending and is also sending back a fixed length string.
Also the string which is sent by the server does not contain a newline terminator, so my code(readLine()) is waiting for it and timeouts.
I am modifying my code and trying not to use readLine. The string sent by the server contains "END OF REPORT" in the end and I will be looping through the input till I get that string.