Results 1 to 6 of 6
  1. #1
    glauber is offline Member
    Join Date
    Feb 2011
    Location
    São José dos Campos, Brazil
    Posts
    6
    Rep Power
    0

    Exclamation 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

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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'.

  3. #3
    glauber is offline Member
    Join Date
    Feb 2011
    Location
    São José dos Campos, Brazil
    Posts
    6
    Rep Power
    0

    Default

    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. My
    Java Code:
    command = in.readLine;
    variable 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?

  4. #4
    glauber is offline Member
    Join Date
    Feb 2011
    Location
    São José dos Campos, Brazil
    Posts
    6
    Rep Power
    0

    Default

    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.
    Attached Thumbnails Attached Thumbnails Problem reading from server socket-capture2.jpg  

  5. #5
    firer is offline Member
    Join Date
    Feb 2011
    Posts
    1
    Rep Power
    0

    Default

    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.

  6. #6
    glauber is offline Member
    Join Date
    Feb 2011
    Location
    São José dos Campos, Brazil
    Posts
    6
    Rep Power
    0

    Default

    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
    end
    Last edited by glauber; 02-17-2011 at 12:19 PM. Reason: Thanks, firer

Similar Threads

  1. Java Socket server with C client problem
    By rnvrnv in forum Networking
    Replies: 6
    Last Post: 11-09-2010, 12:47 AM
  2. Problem on server side (Socket Programming)
    By ersachinjain in forum Networking
    Replies: 9
    Last Post: 05-06-2010, 04:21 PM
  3. Reading inputstream on socket
    By javanetworknew in forum Networking
    Replies: 1
    Last Post: 04-27-2010, 11:01 AM
  4. Problem reading from socket using read(bytes[])
    By sm123 in forum New To Java
    Replies: 1
    Last Post: 04-21-2010, 06:49 PM
  5. Client Server socket problem - help needed
    By kellaw in forum Threads and Synchronization
    Replies: 6
    Last Post: 10-03-2008, 06:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •