日期和时间服务器 [英] Date and time server
本文介绍了日期和时间服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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 空隙跨度>主(<跨度类= 代码sdkkeyword >字符串跨度> 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(<跨度类=代码-string> ---重置与客户端的连接---);
}
}
}
他是javaEE的初学者。我遇到问题一旦服务器获得客户端的地址,从服务器到客户端的输入。
我没有收到客户端的收到日期和时间..输出。
PS:我的输出,只是它不打印线alone.Am试图太好了:d
解决方案
好吧,我复制你的代码并运行按原样。
首先启动服务器然后启动客户端,工作正常。我会在服务器中输入一个输出来指示它正在等待连接:
System.out.println( 等待连接);
Socket s = ss.accept();
System.out.println( 收到连接);
此外,日期对象将保留在您只实例化一次的同时。更好的方法是在while循环中移动该行
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
解决方案
Okay, I copied your code and ran "as is".
Start the server first and then the client, works fine. I would put into the server an output to indicate that it's waiting for the connection:
System.out.println("Waiting for connection"); Socket s=ss.accept(); System.out.println("Connection received");
Also the date object will remain at the same time as you only instantiate it once. Better would be to move that line inside the while loop
这篇关于日期和时间服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文