5 Replies - 1275 Views - Last Post: 23 August 2012 - 01:16 PM Rate Topic: -----

#1 StuckInJava  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 143
  • Joined: 11-November 10

Client stalls in TCP Client Server

Posted 23 August 2012 - 11:36 AM

Hi I am messing around with client server stuff in Java. Basically I want a client to connect to the server, when it sends any message the server responds by sending 0, then 1 and the client is then ready to send again. Only one client and one server so no need for threads.

This works, the client can send something and the server returns the numbers but the client does not know the server has stopped sending numbers and waits where as it should go back to asking the user to input something. Any help?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;


public class ElClient {
	public static void main(String[] args) throws IOException {

		Socket echoSocket = null;
		PrintWriter out = null;
		BufferedReader in = null;

		try {
			echoSocket = new Socket("localhost", 4444);
			out = new PrintWriter(echoSocket.getOutputStream(), true);
			in = new BufferedReader(new InputStreamReader(
					echoSocket.getInputStream()));
		} catch (UnknownHostException e) {
			System.err.println("error blah blah...");
			System.exit(1);
		} catch (IOException e) {
			System.exit(1);
		}

		BufferedReader stdIn = new BufferedReader(
				new InputStreamReader(System.in));
		String userInput, serverOutput;

		System.out.print("Give something to server : ");
		while ((userInput = stdIn.readLine()) != null) {
			out.println(userInput);
			while((serverOutput = in.readLine())!= null){
				System.out.println(serverOutput);
			}
			System.out.print("Give something to server :");
		}

		out.close();
		in.close();
		stdIn.close();
		echoSocket.close();
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


public class LeServer {
 
    public static void main(String[] args)
    {
   
        ServerSocket serverSocket = null;
        try {
           serverSocket = new ServerSocket(4444);
          
        } catch (IOException se) {

           System.exit(-1);
        }
 
     
 
        // Accept and handle client connections
        while (true) {
           try {
               Socket clientSocket = serverSocket.accept();
               PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
               BufferedReader in = new BufferedReader(
                       new InputStreamReader(
                       clientSocket.getInputStream()));
               String inputLine, outputLine;
         
        
               while ((inputLine = in.readLine()) != null) {
               for(int i = 0; i < 2; i++){
            	   out.println(i);
               }
               }
           } catch (IOException ioe) {
             
           }
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Client stalls in TCP Client Server

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Client stalls in TCP Client Server

Posted 23 August 2012 - 11:46 AM

This will never be null
while ((userInput = stdIn.readLine()) != null) {  


you will simply stall here until the user enters something

Same thing here
             while((serverOutput = in.readLine())!= null){  


you will simply stay stock on this statement until the server sends you something
Was This Post Helpful? 0
  • +
  • -

#3 StuckInJava  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 143
  • Joined: 11-November 10

Re: Client stalls in TCP Client Server

Posted 23 August 2012 - 11:58 AM

Yeh I knew that was the problem but how do I go about fixing it? I have tried everything I can think of.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Client stalls in TCP Client Server

Posted 23 August 2012 - 12:32 PM

Basically you can have two types of Client/server applications

One that is synchronous, which basically mean, the client sends a message, the server answers by a known number of messages (or a message that says how many messages follow)

The second case is asynchronous, you do not know who will talk to whom first and how many messages will be exchanged.

In both cases, the Server needs a thread to listen to each client.

If asynchronous, the Clients also needs a thread to listen to the possible messages from the server.

Have a look at

http://www.dreaminco...r-gui-optional/

This post has been edited by pbl: 23 August 2012 - 12:40 PM
Reason for edit:: Lots of typo

Was This Post Helpful? 1
  • +
  • -

#5 StuckInJava  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 143
  • Joined: 11-November 10

Re: Client stalls in TCP Client Server

Posted 23 August 2012 - 12:42 PM

How would the known number of messages or message declaring how many messages work?

Thanks for you help.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Client stalls in TCP Client Server

Posted 23 August 2012 - 01:16 PM

That is part of your application protocol.
You can have the client sending a request
The server sending back first the number of messages then these messages
or the Server sending back the messages ending them by a special message: "End of messages"
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1