Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Client disconnecting

  1. #1
    Join Date
    Jan 2011
    Thanked 0 Times in 0 Posts

    Question Client disconnecting

    I am new to writing servers and clients so I'm not to sure what I am doing here.

    I have the server and the client working with the client connecting to the server and sending the message back and forth between the two like it should but my problem is after it sends the message once the client disconnects. I was able to fix it by lopping the Socket but I figured making it reconnect after each time is really inefficient so thats why I am here.

    Heres my code;
    import java.net.*;
    import java.io.*;
    public class Client {
    	static DataInputStream in;
    	static Socket socket;
    	static DataOutputStream out;
    	static String ip = "localHost";
    	static int port = 5555;
    	static String sent;
    	static String recived;
    	static BufferedReader stdin = new BufferedReader
        (new InputStreamReader(System.in));
    	public static void main(String[] args) throws IOException, UnknownHostException {
    		System.out.println("Connecting to server "+ip+" on port "+port);
    		socket = new Socket(ip, port);
    		while (true) {
    			out = new DataOutputStream(socket.getOutputStream());
    			in = new DataInputStream(socket.getInputStream());
    			System.out.println("Type your Message:");
    			sent = stdin.readLine();
    			recived = in.readUTF();

    Heres what I was doing:
    public void... {
         while (true) {
              System.out.println("Connecting to server "+ip+" on port "+port);
              socket = new Socket(ip, port);

    So basically it was reconnecting every time it preformed a function

    I'm wanting to know how to make it connect and stay connected until disconnected by the server (I can do the disconnecting part myself).

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 833 Times in 772 Posts
    Blog Entries

    Default Re: Client disconnecting

    Don't have all the code of the client or the requirements, but place the client IO into a loop and you can continually read the server messages:
    while ( ( line = br.readLine() ) != null ){
      ///deal with line
    ...assuming br is a BufferedReader wrapping the InputStream of the Socket, line is a String, and each message is separated from another by a new line. readLine will block until more data is sent by the server. Placing this in a Thread and the OutputStream writing in another threads adds more flexibility for the Server/Client communication.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Thanked 0 Times in 0 Posts

    Default Re: Client disconnecting

    why are you flushing System.out instead of out ... also why are you always re-initializing in and out???

Similar Threads

  1. http client
    By ilearn-computer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 07:47 AM
  2. IRC client
    By Brt93yoda in forum The Cafe
    Replies: 14
    Last Post: November 26th, 2010, 04:31 PM
  3. Java Client
    By newsomjk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2010, 11:39 PM
  4. [SOLVED] web client
    By 5723 in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: June 10th, 2009, 04:44 PM
  5. How to write switch statement inside if statement?
    By Rezz in forum Loops & Control Statements
    Replies: 6
    Last Post: June 11th, 2008, 11:27 AM