import java.io.*;
import java.net.*;public class ClientDemo{
public static void main(String[] args)throws IOException{
Socket kkSocket=null;
PrintWriter out=null;
BufferedReader in=null;
boolean runable=true;
String toServer;

try{

kkSocket=new Socket("127.0.0.1",1111);
out=new PrintWriter(kkSocket.getOutputStream(),true);
in=new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
}catch(UnknownHostException e){
System.err.println("找不到服务器");
System.exit(1);
}catch(IOException e){
System.err.println("不能获取Socket的读入与写出气");
System.exit(1);
}

BufferedReader stdIn=new BufferedReader(new InputStreamReader(System.in));

out.println("新的用户登入");

ReadServerThread readServerThread=new ReadServerThread(in);
readServerThread.start();

while(runable){
toServer=stdIn.readLine();
out.println(toServer);

if(toServer.equals("Bye."))break;
runable=readServerThread.runable;
}

readServerThread.fromServer="欢迎下次再来";
readServerThread.runable=false;
out.close();
in.close();
stdIn.close();
kkSocket.close();
}
}class ReadServerThread extends Thread{
BufferedReader in=null;
String fromServer="";
boolean runable=true;

public ReadServerThread(BufferedReader in){
this.in=in;
}

public void run(){
while(runable){

try{fromServer=in.readLine();}catch(Exception e){runable=false;}

if(fromServer.equals("Bye.")){
System.out.print("服务器程序退出");
runable=false;
break;
}
System.out.println("服务器:"+fromServer);
}
}
}