Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello : i want to build a simple client server chat (single server with single client)

but for recieving message and send message (concurrency) use timer to check input or output but timerTask not repeated! and waiting for user input, how can i change my code to build effective timer and my timer have a independant behaviour such as thread.

and next problem is my scanner variable hasNextLine() not working when data exist!
this is my code:

server side :

Java
import java.net.*;
import java.io.*;
import java.util.Scanner;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class ChatServer
{  private Socket          socket   = null;
   private ServerSocket    server   = null;
   private Scanner Recieving=null;
   private Scanner Sending=null;
   private PrintWriter out1=null;
   private boolean inManage=false;
   public ChatServer(int port)
   {  try
      {
	System.out.println("Binding to port " + port + ", please wait  ...");
         server = new ServerSocket(port);
         System.out.println("Server started: " + server);
         System.out.println("Waiting for a client ...");
         socket = server.accept();
         System.out.println("Client accepted: " + socket);
 		try
 		{
 			Recieving = new Scanner(socket.getInputStream());
 			Sending= new Scanner(System.in);
			out1 = new PrintWriter(socket.getOutputStream());

 		}
 		catch (Exception e)
 		{
 			e.printStackTrace();
		}         
      }
      catch(IOException ioe)
      {
		  System.out.println(ioe);
      }
   }
   public void close() throws IOException
   {  if (socket != null)    socket.close();
   }

   public void Manage()
   {
	  if(inManage==true)
	      return;
	  inManage=true;
	  System.out.println("Server In Manage");
	  if (Recieving.hasNextLine())
	  {
	  	String input = Recieving.nextLine();
	  	System.out.println("Client : " + input);
	  }
	  if (Sending.hasNextLine())
	  {
	  	String input = Sending.nextLine();
	  	out1.println((new Date()).toString()+": " + input);
	  }
	  inManage=false;
   }
   public static void main(String args[])
   {
      final ChatServer server = new ChatServer(8800);
      Timer timer=null;
      timer=new Timer();
	timer.scheduleAtFixedRate(new TimerTask() {
	@Override
	public void run() {
	            server.Manage();
	  	   	 }
         },0, 1000);
   }
}


clientSide

Java
import java.net.*;
import java.io.*;
import javax.swing.JOptionPane;
import java.util.Date;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;
public class ChatClient
{
   private Socket          socket   = null;
   private Scanner Recieving=null;
   private Scanner Sending=null;
   private boolean inManage=false;
   private PrintWriter out1=null;
   public ChatClient(String serverName, int serverPort)
   {
	  System.out.println("Establishing connection. Please wait ...");
      try
      {  socket = new Socket(serverName, serverPort);
         System.out.println("Connected: " + socket);
          Recieving = new Scanner(socket.getInputStream());
		  Sending= new Scanner(System.in);
		 out1 = new PrintWriter(socket.getOutputStream());
      }
      catch(UnknownHostException uhe)
      {  System.out.println("Host unknown: " + uhe.getMessage());
      }
      catch(IOException ioe)
      {  System.out.println("Unexpected exception: " + ioe.getMessage());
      }
   }
   public void stop()
   {  try
      {
         if (socket    != null)  socket.close();
      }
      catch(IOException ioe)
      {  System.out.println("Error closing ...");
      }
   }
   public void Manage()
   {
	   if(inManage==true)
	      return;
	   inManage=true;
       System.out.println("Client In Manage");
	if (Recieving.hasNextLine())
	{
		String input = Recieving.nextLine();
		System.out.println("Client : " + input);
	}
	if (Sending.hasNextLine())
	{
		String input = Sending.nextLine();
		out1.println((new Date()).toString()+": " + input);
	}
	  inManage=false;
   }
   public static void main(String args[])
   {
      String serverAddress = JOptionPane.showInputDialog(
          "Enter Server IP Address:");
      final ChatClient client = new ChatClient( serverAddress,8800);
      Timer timer=null;
      timer=new Timer();
      timer.schedule(new TimerTask() {
	@Override
	public void run() {
	             client.Manage();
		    }
         },0, 1000);
   }
}

Thanks Advance.
F1 F1 F1 F1 F1 , i'm waiting for your consideration, please......
Posted
Updated 1-Sep-13 11:36am
v4

1 solution

There are lots of issue. You server app is bulid to listen one client and exist.
You have put that code under while(true) loop. And one more thing its not a good choice to start with timer. Use thread to which is more effective in this scenario.


Example 1
[^]

A Java Chat Application[^]
 
Share this answer
 
Comments
Mojtaba Eng 2-Sep-13 10:54am    
thanks for your answer , in my code just exactly one client chat with server, why must i have a while loop?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900