package itat.s11_5;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ServerSocket server=null;
Socket you=null;
String s=null;
DataOutputStream out=null;
DataInputStream in=null;
try {
server=new ServerSocket(4444);
System.out.println("等待客户呼叫");
you=server.accept();
out=new DataOutputStream(you.getOutputStream());
in=new DataInputStream(you.getInputStream());
while(true){
s=in.readUTF();
int m=Integer.parseInt(s);
out.writeUTF("你好:我是服务器");
out.writeUTF("你说的数乘2后是:"+2*m);
System.out.println("服务器收到:"+s);
Thread.sleep(1000);
}
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("客户已断开");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("客户已断开");
}
}
}
package itat.s11_5;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String s=null;
Socket mysocket;
DataInputStream in=null;
DataOutputStream out=null;
try {
mysocket=new Socket("127.0.0.1",4444);
in=new DataInputStream(mysocket.getInputStream());
out=new DataOutputStream(mysocket.getOutputStream());
for(int k=1;k<100;k+=2){
out.writeUTF(""+k);
s=in.readUTF();
System.out.println("客户收到:"+s);
Thread.sleep(1000);
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
System.out.println("服务器以断开"+e);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("服务器以断开"+e);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("服务器以断开"+e);
}
}
}