Results 1 to 2 of 2
  1. #1
    munish is offline Member
    Join Date
    Jul 2009
    Posts
    37
    Rep Power
    0

    Exclamation Simple Client Server Application: Continuous message passing problem

    Hi,

    Once i send message from server code stops how can i change it to continuously keep on sending messages:
    Server code:
    Java Code:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    public class SocketServer {
    	/**
    	 * Creates a SocketServer object and starts the server.
    	 *
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// Setting a default port number.
    		ServerSocket serverSocket;
    		Socket client ;
    		BufferedWriter writer;
    		BufferedReader in;
    		try {
    				serverSocket = new ServerSocket(9993);
    				client = serverSocket.accept();
    			
    				writer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
    				in = new BufferedReader(new InputStreamReader(System.in));
    				String t=in.readLine();
    				writer.write(t);
    				writer.flush();
    		  
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    Client code:

    Java Code:
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class SocketClient {
    
    
    	public static void main(String arg[]){
    		//Creating a SocketClient object
    		Socket socketClient;
    		BufferedReader stdIn ;
    		String userInput;
    				
    		try {
    			socketClient = new Socket("127.0.01",9993);
    			
    				stdIn = new BufferedReader(new InputStreamReader(socketClient.getInputStream()));
    				while ((userInput = stdIn.readLine()) != null) {
    					System.out.println(userInput);
    				}
    			
    		} catch (UnknownHostException e) {
    			System.err.println("Host unknown. Cannot establish connection");
    		} catch (IOException e) {
    			System.err.println("Cannot establish connection. Server may not be up."+e.getMessage());
    		}
    	}
    }
    i also tried to add while loop in both but it doesn't work properly.
    Last edited by munish; 12-22-2013 at 04:27 AM.

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Simple Client Server Application: Continuous message passing problem

    Java Code:
    String t = "";
                while ((t = in.readLine()) != null) {
                    writer.write(t);
                    writer.flush();
                }

Similar Threads

  1. Replies: 0
    Last Post: 11-27-2011, 02:41 PM
  2. Replies: 7
    Last Post: 11-26-2011, 11:03 AM
  3. message passing in Client/Server Chat problem
    By pappyj77 in forum Threads and Synchronization
    Replies: 19
    Last Post: 09-13-2011, 02:54 PM
  4. passing info between server/client problem
    By DarkBlaze in forum New To Java
    Replies: 13
    Last Post: 07-24-2008, 03:14 AM
  5. Simple example Client Server Application
    By ferosh in forum Networking
    Replies: 1
    Last Post: 04-01-2007, 10:36 AM

Tags for this Thread

Posting Permissions

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