java中java.util.Timer的问题 [英] problem with java.util.Timer in java

查看:52
本文介绍了java中java.util.Timer的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好:我想构建一个简单的客户端服务器聊天(单个服务器与单个客户端)



但是对于接收消息和发送消息(并发)使用定时器来检查输入或输出但timerTask不重复!并等待用户输入,如何更改我的代码以构建有效的计时器和我的计时器有一个独立的行为,如线程。



和下一个问题是我的扫描仪变量当数据存在时,hasNextLine()无效!

这是我的代码:



服务器端:



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 :

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





clientSide

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);
   }
}



谢谢提前。

F1 F1 F1 F1 F1,我在等你的考虑,请......


Thanks Advance.
F1 F1 F1 F1 F1 , i'm waiting for your consideration, please......

推荐答案

有很多问题。你的服务器应用程序很大,可以监听一个客户端并且存在。

您已将该代码置于while(true)循环下。还有一件事,从计时器开始不是一个好的选择。在这种情况下使用更有效的线程。





示例1
[ ^ ]



Java聊天应用程序 [ ^ ]
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[^]


这篇关于java中java.util.Timer的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆