Results 1 to 6 of 6
- 02-15-2011, 03:40 PM #1
Member
- Join Date
- Feb 2011
- Location
- São José dos Campos, Brazil
- Posts
- 6
- Rep Power
- 0
Problem reading from server socket
Hey guys,
I've implemented a simple TCP server using the ServerSocket class (code below) to communicate with a client. They should just exchange strings. The client comes off-the-shelf with a sensor I need to use and, although I don't have access to its source code, I know all the commands I can R/W from it. The server starts listening and as soon as the client connects to it, the client is supposed to send a string with 3 initialization commands of the type "command 1\r\n command 2\r\n command 3\r\n". \r\n denotes the end of a command.
The problem is, my server can't seem to read this initial string and then the client has a timeout because the server doesn't reply appropriately. I know the initial string packet is being sent because I can see it with a network sniffer (Wireshark). Does anyone have an idea why my server hasn't been able to read it?
Thanks,
Glauber
Java Code:function main import java.net.ServerSocket import java.io.* port = 16544; server = []; s = []; command = []; try % Creates the server socket if port is available fprintf(1, ['Waiting for the client to connect...\n']); server = ServerSocket(port); % Listens on the specified port server.setSoTimeout(0); % Wait forever s = server.accept; % Start listening fprintf(1, 'Server connected\n'); % Socket options s.setKeepAlive(true) % Heartbeat s.setTcpNoDelay(true) % Nable's algorithm off % R/W streams in = BufferedReader(InputStreamReader(s.getInputStream)); out = DataOutputStream(s.getOutputStream); while true % read data command = in.readLine; if ~isempty(command) command end command = char(command); % write data CRLF = [char(13),char(10)]; switch command case 'command 1' out.writeBytes(['reply 1',CRLF]); out.flush; case 'command 2' out.writeBytes(['reply 2',CRLF]); out.flush; end end % clean up server.close s.close return catch exception if ~isempty(server) server.close end if ~isempty(s) s.close end throw(exception) end end
- 02-16-2011, 04:14 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
You don't have a default case in your switch. My suspicion is that command is neither 'command 1' nor 'command 2', but is instead 'command 1\r' and 'command 2\r'.
- 02-16-2011, 11:00 AM #3
Member
- Join Date
- Feb 2011
- Location
- São José dos Campos, Brazil
- Posts
- 6
- Rep Power
- 0
My bad - I should have mentioned that the switch is there just as an example of how I intend to implement the writing part. It also allows me to see if the server can write correctly when I debug it through telnet. I have a list of 40+ commands the switch will deal with but I didn't want to spend too much time on it before making sure my server can read them.
The problem is at the read portion. Myvariable returns empty, although I can see the packets being sent by the client with Wireshark. Can it be a timing issue? Like, if the client sends the packet right after connecting, could it be that the server is not yet ready to read? If so, what route can I take to solve it?Java Code:command = in.readLine;
- 02-16-2011, 04:59 PM #4
Member
- Join Date
- Feb 2011
- Location
- São José dos Campos, Brazil
- Posts
- 6
- Rep Power
- 0
If it helps, here's a screenshot of the network capture during the conversation. 170.170.6.5 is the client and .50 is the server.
- 02-16-2011, 08:56 PM #5
Member
- Join Date
- Feb 2011
- Posts
- 1
- Rep Power
- 0
I don't see a timing problem here; readLine should block until there's enough data in the buffer. If you're using Wireshark, I think it could be useful if you could confirm that the incoming stream is exactly as you're thinking. I find it strange that the client is sending a FIN+ACK packet right after the connection is established. This is probably closing the connection before anything is sent, but I don't remember all the details of the TCP protocol to confirm you this.
EDIT: You can filter the specific TCP conversation using ContextMenu -> Apply as Filter -> Selected, and can see the TCP data using Context Menu -> Follow TCP Conversation.Last edited by firer; 02-16-2011 at 09:01 PM.
- 02-17-2011, 12:11 PM #6
Member
- Join Date
- Feb 2011
- Location
- São José dos Campos, Brazil
- Posts
- 6
- Rep Power
- 0
firer,
Thx for the reply. The incoming strings are arriving as expected. The problem was elsewhere. While the ServerSocket needs to be opened only once, the socket resulting from the serversocket.accept listener should be renewed continuously to deal with incoming client requests. The client tried to send data after the FIN+ACK package but by that time connection through the first listener socket was already closed. Putting serversocket.accept in a loop solved it. So the correct code should look something like this:
Java Code:function main import java.net.ServerSocket import java.io.* port = 16544; server = []; s = []; command = []; try % Creates the server socket if port is available fprintf(1, ['Waiting for the client to connect...\n']); server = ServerSocket(port); % Listens on the specified port server.setSoTimeout(0); % Wait forever s = server.accept; % Start listening fprintf(1, 'Client connected\n'); % R/W streams in = BufferedReader(InputStreamReader(s.getInputStream)); out = DataOutputStream(s.getOutputStream); while true % read data command = in.readLine; if ~isempty(command) & command~=-1 command end command = char(command); % write data CRLF = [char(13),char(10)]; switch command case 'command 1' out.writeBytes(['reply 1',CRLF]); out.flush; case 'command 2' out.writeBytes(['reply 2',CRLF]); out.flush; end % Repeatedly answer client requests (reopens listen socket) if isempty(command) s.close s = server.accept; % Start listening % R/W streams in = BufferedReader(InputStreamReader(s.getInputStream)); out = DataOutputStream(s.getOutputStream); end end % clean up s.close server.close return catch exception if ~isempty(server) server.close end if ~isempty(s) s.close end throw(exception) end endLast edited by glauber; 02-17-2011 at 12:19 PM. Reason: Thanks, firer
Similar Threads
-
Java Socket server with C client problem
By rnvrnv in forum NetworkingReplies: 6Last Post: 11-09-2010, 12:47 AM -
Problem on server side (Socket Programming)
By ersachinjain in forum NetworkingReplies: 9Last Post: 05-06-2010, 04:21 PM -
Reading inputstream on socket
By javanetworknew in forum NetworkingReplies: 1Last Post: 04-27-2010, 11:01 AM -
Problem reading from socket using read(bytes[])
By sm123 in forum New To JavaReplies: 1Last Post: 04-21-2010, 06:49 PM -
Client Server socket problem - help needed
By kellaw in forum Threads and SynchronizationReplies: 6Last Post: 10-03-2008, 06:49 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks