本文介绍了日期和时间服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Dateclient.java:
import java.net。*;
import java.io. *;
import java.util。*;
public class dateclient
{
public static void main(字符串跨度> ARGS [])
{ {
插座S =新的Socket(InetAddress的.getLocalHost(), 1000 );
BufferedReader br = new BufferedReader( new InputStreamReader(s.getInputStream()));
字符串输入;
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
while ((input = br.readLine())!= null)
{
System.out。的println(输入);
out.println( Date& Time received ....);
}
}
catch (例外e){
System.out.println( 已重置与服务器的连接);}
}
}
Dateserver.java
import java.net。*;
import java.io. *;
import java.util。*;
public class dateserver extends 主题
{
public static 空隙跨度>主( ARGS [])异常
{
ServerSocket ss = new ServerSocket( 1000 );
Socket s = ss.accept();
BufferedReader br = new BufferedReader( new InputStreamReader(s.getInputStream()));
PrintWriter out = new PrintWriter(s.getOutputStream(),true);
日期d =新日期();
尝试
{
while (true)
{
out.println( 服务器上的时间: + d.toString()) ;
System.out.println(br.readLine());
sleep( 1000 );
}
}
(IOException的发送){的System.out.println( ---重置与客户端的连接---);
}
}
}
他是javaEE的初学者。我遇到问题一旦服务器获得客户端的地址,从服务器到客户端的输入。
我没有收到客户端的收到日期和时间..输出。
PS:我的输出,只是它不打印线alone.Am试图太好了:d
解决方案
Dateclient.java:
import java.net.*; import java.io.*; import java.util.*; public class dateclient { public static void main(String args[]) {try{ Socket s=new Socket(InetAddress.getLocalHost(),1000); BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream())); String input; PrintWriter out=new PrintWriter(s.getOutputStream(),true); while((input=br.readLine())!=null) { System.out.println(input); out.println("Date & Time received...."); } } catch(Exception e){ System.out.println("The connection to the server has been reset");} } }
Dateserver.java
import java.net.*; import java.io.*; import java.util.*; public class dateserver extends Thread { public static void main(String args[]) throws Exception { ServerSocket ss=new ServerSocket(1000); Socket s=ss.accept(); BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter out=new PrintWriter(s.getOutputStream(),true); Date d=new Date(); try { while(true) { out.println("Time at server: "+d.toString()); System.out.println(br.readLine()); sleep(1000); } } catch(IOException e){System.out.println("---Connection to client is reset---"); } } }
Hey am a begineer to javaEE.I have a problem receiving an input from server to client once the server gets the client's address.
Am not getting the "Date and time received.." output at the client.
PS:My output is good except that it doesnt print that line alone.Am trying too :D
解决方案
这篇关于日期和时间服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!