日期:2014-05-20 浏览次数:21228 次
import java.net.*;
import java.io.*;
public class Test07 {
public static void main(String[] args){
try{
ServerSocket ss = new ServerSocket(6666);
String str = null;
String zs = null;
while(true){
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
if((str=dis.readUTF())!=null){
System.out.println("客户端说:" + str);
}
while(!((zs=br.readLine()).equals("bye"))){
dos.writeUTF(zs);
dos.flush();
System.out.println("等待客户器端说话...");
if((str=dis.readUTF())!=null){
System.out.println("客户端说:" + str);
}
}
br.close();
dos.close();
dis.close();
s.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
import java.net.*;
import java.io.*;
public class Test08 {
public static void main(String[] args){
try{
String str = null;
String zs = null;
Socket s = new Socket("127.0.0.1",6666);
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(!((zs=br.readLine()).equals("bye"))){
dos.writeUTF(zs);
os.flush();
System.out.println("等待服务端说话...");
if((str=dis.readUTF())!=null){
System.out.println("服务端说:" + str);
}
}
s.close();
br.close();
dos.close();
dis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}