LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-18-2012, 07:41 PM   #1
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Rep: Reputation: Disabled
java synchronous chat question


I have a client and server in Java and want to loop the data handle until either the client or server closes out.

here is what i have so far

SERVER:
Code:
import java.util.*;
import java.net.*;
import java.io.*;

class ServerTest {
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static BufferedReader bufferedReader;
private static PrintWriter printWriter;
private static String inputLine;

  public static void main(String[] args) {
	try {
	serverSocket = new ServerSocket(63400);
	clientSocket = serverSocket.accept();

	bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

	  while((inputLine = bufferedReader.readLine()) != null)
	  System.out.println("SERVER: " + inputLine);
	}

		catch(IOException e) {
		System.out.println(e);
		}
  }
}
CLIENT:
Code:
import java.util.*;
import java.net.*;
import java.io.*;

class ClientTest {
private static Socket socket;
private static PrintWriter printWriter;

  public static void main(String[] args) {
        try {
        socket = new Socket("localhost",63400);
        printWriter = new PrintWriter(socket.getOutputStream(),true);
	String send_to;
	Scanner user_input = new Scanner(System.in);
	System.out.print("Message: ");
	send_to = user_input.next();
        printWriter.println(send_to);
        System.out.println("MESSAGE sent successfully.");
        }

		catch(Exception e) {
		System.out.println(e);
		}
    }
}
Instead of them both exiting after the server receives the clients message, how could I continue the chat?
 
Old 09-18-2012, 08:20 PM   #2
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
Solved my issue.

Last edited by amboxer21; 09-18-2012 at 10:57 PM.
 
Old 09-18-2012, 11:01 PM   #3
amboxer21
Member
 
Registered: Mar 2012
Location: New Jersey
Distribution: Gentoo
Posts: 291

Original Poster
Rep: Reputation: Disabled
So, I solved my issue. maybe I posted to early but here is the final code in case anyone else is having the same problem

CLIENT:
Code:
import java.util.*;
import java.net.*;
import java.io.*;

class Client {
private static String recv;
final static String quit = "quit";

  public static void main(String[] args) {

        try {
	String sendTo;
	Socket socket = new Socket("localhost",62400);
	  while(true) {
          PrintWriter bytesWritten = new PrintWriter(socket.getOutputStream(),true);
	  System.out.print("Message: ");
	  Scanner user_input = new Scanner(System.in);
	  sendTo = user_input.next();

	    if(sendTo.equals(quit)) {
	    bytesWritten.println("quit");
	    System.out.print("Quiting now.\n");
	    Thread.sleep(1000);
	    System.exit(0);
	    }
	    	else {
		bytesWritten.println(sendTo);
		}

	  BufferedReader bytesRead = new BufferedReader(new InputStreamReader(socket.getInputStream()));
	  recv = bytesRead.readLine();
	  System.out.println("SERVER: " + recv);
          }
	}

		catch(Exception e) {
		System.out.println(e);
		}
  }
}
SERVER:
Code:
import java.util.*;
import java.net.*;
import java.io.*;

class Server {
private static String recv;
final static String quit = "quit";

  public static void main(String[] args) {

	try {
	ServerSocket socket = new ServerSocket(62400);
	Socket clientSocket = socket.accept();
	  while(true) {
	  BufferedReader bytesRead = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
	    recv = bytesRead.readLine();
	      if(recv.equals(quit)) {
	      System.out.print("Client manually closed connection.\n");
	      System.exit(0);
	      }
	      	   else {
	           System.out.println("CLIENT: " + recv);
	           }

	  String sendTo;
          PrintWriter bytesWritten = new PrintWriter(clientSocket.getOutputStream(),true);
	  System.out.print("Message: ");
	  Scanner user_input = new Scanner(System.in);
	  sendTo = user_input.next();
	  bytesWritten.println(sendTo);
	  }
	}

		catch(IOException e) {
		System.out.println(e);
		}
  }
}
If you look at the code in my first post. I was using a while loop with the readline data handle. That was causing the issues. I built on it and added a while loop. Chat runs fine until you manually exit. Thanks

Last edited by amboxer21; 09-19-2012 at 01:33 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
IN JAVA, how to do a multi chat client in java with the option of private message ? nicolasd Programming 5 09-16-2009 08:53 PM
synchronous encryption algorithm vs. non-synchronous whysyn Linux - Security 2 02-22-2007 10:42 AM
no java yahoo-chat on freebsd5.4 dfowensby *BSD 6 05-14-2005 07:54 PM
Yahoo Java Chat And Linux webterractive General 1 07-25-2004 07:14 AM
Yahoo Java Chat glock19 Linux - General 2 02-23-2002 04:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration