import *;
import *;
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 *;
import *;
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){}
}
}
自己改一下