Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2010, 12:38 AM
Member
 
Join Date: Mar 2010
Posts: 10
Rep Power: 0
FallenBlade is on a distinguished road
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:

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?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-20-2010, 02:59 AM
sari's Avatar
Member
 
Join Date: Jan 2009
Posts: 26
Rep Power: 0
sari is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-20-2010, 11:07 AM
Senior Member
 
Join Date: Mar 2010
Posts: 605
Rep Power: 1
gcalvin is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-20-2010, 01:11 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
JosAH is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-20-2010, 02:34 PM
Member
 
Join Date: Mar 2010
Posts: 10
Rep Power: 0
FallenBlade is on a distinguished road
Default
Well I've changed the code to this:

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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-20-2010, 02:44 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
JosAH is on a distinguished road
Default
What happens when you flush the output streams (both on the client side and the server side).

kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 03-20-2010, 02:49 PM
Member
 
Join Date: Mar 2010
Posts: 10
Rep Power: 0
FallenBlade is on a distinguished road
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);
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 03-20-2010, 03:58 PM
Member
 
Join Date: Mar 2010
Posts: 10
Rep Power: 0
FallenBlade is on a distinguished road
Default
FIXED!

It was a combination of new line characters, and my client wait for a message infinitely before sending one back.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 03-20-2010, 04:14 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
JosAH is on a distinguished road
Default
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 03-20-2010, 04:50 PM
Member
 
Join Date: Mar 2010
Posts: 10
Rep Power: 0
FallenBlade is on a distinguished road
Default
Cheers, glad this is all sorted. Now to get the messages to have meaning and my programs working!
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Message Server/Client help sari Networking 3 03-20-2010 11:17 PM
Java Server Client with database cliff Networking 2 02-08-2010 11:17 PM
Java Server, Flash Client matbuckland New To Java 3 09-01-2009 09:58 AM
develop java standa alone client for ASP.Net web server manucng Advanced Java 4 08-25-2009 07:04 PM
java server-client comm Manas Das Networking 2 01-28-2009 04:58 PM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 09:16 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org