基于套接字Socket通信的编程
编写一个时间服务器/客户程序,当服务器收到客户端请求后,将当前的系统时间以一定的格式发送给客户端,客户端解析这个格式,并以可读的格式在标准输出设备上打印接收到的时间。谁会呀?我是初学者,大侠帮帮忙!
----------------解决方案--------------------------------------------------------
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[]args){
String s=null;
Socket socket;
DataInputStream in=null;
DataOutputStream out=null;
try{
socket=new Socket("localhost",4331);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
out.writeUTF("hello! It is Client");
while(true){
s=in.readUTF();
out.writeUTF(":"+Math.random());
System.out.println("client get****:"+s);
Thread.sleep(2000);
}
}catch(IOException e){
System.out.println("can't connect");
}catch(InterruptedException e){}
}
}
package awt;
import java.io.*;
import java.net.*;
public class Serever {
public static void main(String [] args){
ServerSocket server=null;
Socket socket=null;
String s=null;
DataOutputStream out=null;
DataInputStream in=null;
try{
server=new ServerSocket(4331);
}catch(IOException e1){
System.out.println("ERROR:"+e1);
}try{
socket=server.accept();
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
while(true){
s=in.readUTF();
out.writeUTF("It's Server:");
out.writeUTF("client sends:"+s);
Thread.sleep(2000);
}
}catch(IOException e){
System.out.println(""+e);
}catch(InterruptedException e){}
}
}
自己改一下
----------------解决方案--------------------------------------------------------