Results 1 to 14 of 14
  1. #1
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default Java Server/C Client

    Hi I have a networking issue between my two programs. I believe the issue is in the Java Server.

    At the moment they connect but a message isn't sent between even though both programs seem to be working fine. They both wait for a reply. They both compile and don't crash, they both just hang waiting for each other. I think the problem is in my Java code. Here is the connection method and where it hangs:

    Java Code:
    public void connect() throws IOException
        {
            try
            {
                // Create server socket on specified port
                // to wait for client requests
                mySocket = new ServerSocket(4000);
            }
            catch (IOException e)
            {
                System.err.println("IO exception on port");
                System.exit(1);
            }
            System.out.println("Server Socket set up, waiting for call");
            try
            {
                // Wait here for the client request
                // Returns a normal socket
                clientSocket = mySocket.accept();
            }
            catch (IOException e)
            {
                System.err.println("Could not accept incoming call");
                System.exit(1);
            }
            System.out.println("Incoming Call Accepted");
    
            PrintWriter out = new PrintWriter(
            clientSocket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(
                new InputStreamReader(clientSocket.getInputStream()));
            String inputLine, outputLine;
    
            out.println("Welcome");
    
            System.out.println(in.readLine());
            
        }
    The Server waits on the last "System.out.println(in.readLine());", and the client is still waiting for the message from the Server.

    Can anyone see any mistakes?

  2. #2
    sari's Avatar
    sari is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    I have found that there are issues when an input stream and an output stream are open at the same time.
    It always freezes on me when I do it.
    If you don't try you can't succeed.

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Your mySocket variable is declared within your first try block, so it goes out of scope at the end of that try block. Declare it outside the try block and set it to null, and you should be fine.

    EDIT: Sorry, I didn't look closely enough -- it's not declared there, so it must be an instance variable in your class. In that case, it needs to be declared static -- is it? (I guess it is, or it wouldn't compile.) So I don't know what's wrong. I've duplicated your code, and it's working for me. (Tested with 'telnet localhost 4000'.)

    -Gary-
    Last edited by gcalvin; 03-20-2010 at 11:17 AM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Can it be an end of line issue? i.e. one party writes a single \n for an end of line while the other party waits for a \r\n\ pair to mark the end of a line.

    kind regards,

    Jos

  5. #5
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default

    Well I've changed the code to this:

    Java Code:
    public String connect() throws IOException
        {
            String message = null;
            try
            {
                // Create server socket on specified port
                // to wait for client requests
                mySocket = new ServerSocket(4000);
            }
            catch (IOException e)
            {
                System.err.println("IO exception on port");
                System.exit(1);
            }
            System.out.println("Have Server Socket set up, waiting for call");
            try
            {
                // Wait here for the client request
                // Returns a normal socket
                clientSocket = mySocket.accept();
            }
            catch (IOException e)
            {
                System.err.println("Could not accept incoming call");
                System.exit(1);
            }
            System.out.println("Incoming Call Accepted");
    
            PrintWriter out = new PrintWriter(
            clientSocket.getOutputStream(), true);
            
            String inputLine, outputLine;
    
            out.println("Welcome\r\n");
    
            out.close();
            
            BufferedReader in = new BufferedReader(
                new InputStreamReader(clientSocket.getInputStream()));
            System.out.println(in.readLine());
            
            return message;
        }
    Because I used "out.close()" it crashes the Java server, saying that the socket is closed. If I take "out.close()" out, it doesn't crash but it still just hangs waiting for the message.

    However the C client now gets the message of "Welcome" and sends it's response.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    What happens when you flush the output streams (both on the client side and the server side).

    kind regards,

    Jos

  7. #7
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default

    I can flush the java out with .flush() obviously. But I don't know how to flush in C. I'm not very experienced with C, all I know is it uses a "send" function and not a stream, that I'm aware of. Here's the code to send the message in C:

    send(socket_fd, "1/523/101/Sam/nighttime\r\n", 23, 0);

  8. #8
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default

    FIXED!

    It was a combination of new line characters, and my client wait for a message infinitely before sending one back.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by FallenBlade View Post
    FIXED!

    It was a combination of new line characters, and my client wait for a message infinitely before sending one back.
    Good; so it was a \n versus \r\n issue; in case it matters in the future: a socket normally uses Nagle's algorithm (it won't send anything until a certain amount of bytes are ready to be send). You can turn off this feature with the setsockopt( ... ) function with the TCP_NODELAY option switched on. Using Java you normally don't need it.

    kind regards,

    Jos

  10. #10
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default

    Cheers, glad this is all sorted. Now to get the messages to have meaning and my programs working!

  11. #11
    airride is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default

    OP is it possible for you to send me the complete programs; I am have a similar situation; need to write a C client and pass/receive data from Java server...

  12. #12
    sari's Avatar
    sari is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    lol grave dig
    If you don't try you can't succeed.

  13. #13
    FallenBlade is offline Member
    Join Date
    Mar 2010
    Posts
    31
    Rep Power
    0

    Default

    Quote Originally Posted by airride View Post
    OP is it possible for you to send me the complete programs; I am have a similar situation; need to write a C client and pass/receive data from Java server...
    As you "need" to write this as opposed to wanting to, I assume this is for some course work/homework...etc.

    So no I won't give you all my work to pass off as your own.

    If you make an attempt and have problems, I and others here would be happy to help.

  14. #14
    sari's Avatar
    sari is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Absolutely if you would like help with an assignment etc... I suggest opening a new thread on it and I'm sure you'll get some help.
    If you don't try you can't succeed.

Similar Threads

  1. Java Message Server/Client help
    By sari in forum Networking
    Replies: 3
    Last Post: 03-20-2010, 11:17 PM
  2. Java Server Client with database
    By cliff in forum Networking
    Replies: 2
    Last Post: 02-08-2010, 11:17 PM
  3. Java Server, Flash Client
    By matbuckland in forum New To Java
    Replies: 3
    Last Post: 09-01-2009, 09:58 AM
  4. Replies: 4
    Last Post: 08-25-2009, 07:04 PM
  5. java server-client comm
    By Manas Das in forum Networking
    Replies: 2
    Last Post: 01-28-2009, 04:58 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
  •