3 Replies - 916 Views - Last Post: 03 May 2013 - 06:48 PM Rate Topic: -----

#1 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Client wait for server message

Posted 02 May 2013 - 01:54 PM

How i can make the client part of my class to wait if input from server is null? i tried to check if is null but i failed..problem is in the getInputFromServer() method.





import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.Scanner;


/**
 *
 * @author chris
 */
public class AnotherClient implements Runnable {
    
    public static void main(String args[]) throws IOException, InterruptedException {
        
        Thread t1 = new Thread(new AnotherClient("1"));
        Thread t2 = new Thread(new AnotherClient("2"));
        
        t1.start();
        t2.start();    
    }
    
    private Socket       socket;
    private Scanner      scanner;
    private InputStream  input;
    private PrintWriter  printWriter;
    private String       id;
    private ServerSocket serverSocket;
    private Socket       connection;
    private PrintWriter  pw;
    private InputStream  in;
    private OutputStream out;
    
    public AnotherClient(String id) {
        
        this.id = id;      
    }
    

    public void runServer() throws IOException {
        
        startServer();
        sendTime();
        closeConnection();
        
    }
    
    public void startServer() throws IOException {
        
        try
        {
            serverSocket = new ServerSocket(3300);
            while(true)
            {
                connection = serverSocket.accept();
            }      
        }
        catch(Exception e)
        {
            System.out.println("Error");
        }    
    }
    
    public void sendTime() {
        
        try 
        { 
            pw = new PrintWriter(connection.getOutputStream(),true);
            pw.println(new Date().toString());
        } 
        catch (IOException ex) 
        {
            System.out.println("Couldnt write");   
        }   
  }
        
    
    
    public void closeServerConnection() {
        try 
        {
            serverSocket.close();
            connection.close();
        } 
        catch (IOException ex) 
        { 
            System.out.println("Error");
        }
    }
    
    public void runClient() throws IOException, InterruptedException {   
        
        connectToServer();
        getInputFromServer();
        closeConnection();    
    }
    
    public void connectToServer() throws InterruptedException {
        
        try
        {
            socket = new Socket("LocalHost",9090); 
        }
        catch(IOException ex)
        {
            System.out.println("Connection failed");
        }
    }
    
    public void getInputFromServer() throws IOException, InterruptedException {
        
        try 
        {
            input = socket.getInputStream();
            scanner = new Scanner(input);
            
            String time = scanner.nextLine();
            System.out.println("Time received from server: "+time);   
        } 
        catch (IOException ex) 
        {     
            System.out.println("Receive of message failed");
        }
        input.close();
        scanner.close(); 
    }
    
    public void closeConnection() {
        
        try 
        {
            socket.close();
        } 
        catch (IOException ex) 
        {   
            System.out.println("Failed to close the connection");
        } 
        System.exit(0);
    }
    
    public String getId() {
  
        return id;
    }
    
    @Override
    public void run() {
        
        try
        {
            if(getId().equals("1"))
            {
                runClient();
            }
            else if ( getId().equals("2"))
            {
                System.out.println("Im running now");
                runServer();    
            }  
        }
        catch(IOException | InterruptedException e)
        {
            System.out.println("Error xxxx"); 
        }         
}
}
    










Is This A Good Question/Topic? 0
  • +

Replies To: Client wait for server message

#2 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Client wait for server message

Posted 02 May 2013 - 06:17 PM

There are 2 possible client/server communication protocol

The synchronous ones:
- client sends a message
- server sends an answer
- client reads answer
- client waits for user input

- client sends a message
- server sends the number of answers it will send
- clients read that number
- cliends read thos N messages
- clients waits for user input

The asynchronous scenario

client has a thread waiting for message from Server
client wait for user input and sens message to Server
Was This Post Helpful? 1
  • +
  • -

#3 psh  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 65
  • Joined: 25-March 13

Re: Client wait for server message

Posted 03 May 2013 - 12:42 AM

View Postpbl, on 03 May 2013 - 01:17 AM, said:

There are 2 possible client/server communication protocol

The synchronous ones:
- client sends a message
- server sends an answer
- client reads answer
- client waits for user input

- client sends a message
- server sends the number of answers it will send
- clients read that number
- cliends read thos N messages
- clients waits for user input

The asynchronous scenario

client has a thread waiting for message from Server
client wait for user input and sens message to Server



For example i have 4 classes A,B,C,D( each one has an open port and also tries to connect to another port of another class ??)..in what order communication may happen between my 4 classes?Could you please give me an example ?(im not asking for code..something like A connects to B while C is connected to D ..something like that )

All i want is to sychronize their time..but i should not use a central server.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Client wait for server message

Posted 03 May 2013 - 06:48 PM

View Postpsh, on 03 May 2013 - 03:42 AM, said:

For example i have 4 classes A,B,C,D( each one has an open port and also tries to connect to another port of another class ??)..in what order communication may happen between my 4 classes?Could you please give me an example ?(im not asking for code..something like A connects to B while C is connected to D ..something like that )

You will have to call Apple apple and Orange orange :)
Classes do not communicate to other classes

Classes can be server or client
Server listen for a connection on a particular port
Client connect to that port
Was This Post Helpful? -1
  • +
  • -

Page 1 of 1