????(2). ???????????
import java.io.*;
import java.net.*;
import java.applet.Applet;
public class TalkServer{
public static void main(String args[]) {
try{
ServerSocket server=null;
try{
server=new ServerSocket(4700);
//???????ServerSocket????4700???????????
}catch(Exception e) {
System.out.println("can not listen to:"+e);
//???????????????
}
Socket socket=null;
try{
socket=server.accept();
//???accept()???????????????п??
//??????????????Socket?????????????
}catch(Exception e) {
System.out.println("Error."+e);
//???????????????
}
String line;
BufferedReader is=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//??Socket?????????????????????????BufferedReader????
PrintWriter os=newPrintWriter(socket.getOutputStream());
//??Socket???????????????????PrintWriter????
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
//????????????豸????BufferedReader????
System.out.println("Client:"+is.readLine());
//??????????????????????????
line=sin.readLine();
//?????????????????
while(!line.equals("bye")){
//??????????? "bye"?????????
os.println(line);
//????????????????
os.flush();
//???????????Client??????????????
System.out.println("Server:"+line);
//????????????????????????
System.out.println("Client:"+is.readLine());
//??Client???????????????????????????
line=sin.readLine();
//????????????????????
} ??//???????
os.close(); //???Socket?????
is.close(); //???Socket??????
socket.close(); //???Socket
server.close(); //???ServerSocket
}catch(Exception e){
System.out.println("Error:"+e);
//???????????????
}
}
}
????5??????????client/server????
????????Client/Server??????????Server?????????????????????? ?У????????????????????????????????????????????????????????????????????????????????????????????????????????????? ????????и??????????????????????????????????????????????????п?????????????????????????????????????????????????? ??????????????????????????????????????????????????????????????????????