客户端:
package com.gxc;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class socket {
public static void main(String para[]){
Socket mysocket=null;
String str=null;
DataInputStream in=null;;
DataOutputStream out=null;
try {
mysocket=new Socket("localhost",8000);
in=new DataInputStream(mysocket.getInputStream());
out=new DataOutputStream(mysocket.getOutputStream());
out.writeUTF("nihao");
while(true){
str=in.readUTF();
if(str!=null){
System.out.println(str);
break;
}
}
mysocket.close();
} catch (UnknownHostException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
服务端:
package com.gxc;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class server {
public static void main(String para[]){
ServerSocket server=null;
Socket socket=null;
DataInputStream in=null;;
DataOutputStream out=null;
String str=null;
try {
server=new ServerSocket(8000);
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
try {
socket=server.accept();
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
while(true){
str=in.readUTF();
if(str!=null){
System.out.println(str);
break;
}
}
out.writeUTF("213423");
socket.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
posted on 2007-07-16 12:41
lcgxc 阅读(309)
评论(0) 编辑 收藏 所属分类:
java