1 importjava.io.IOException;2 importjava.io.InputStream;3 importjava.io.OutputStream;4 importjava.net.InetAddress;5 importjava.net.ServerSocket;6 importjava.net.Socket;7 importjava.util.Scanner;8
9 /**
10 * 聊天程序11 * TCP实现12 * 开一个线程监听一个端口,接收消息13 *@authorGXF14 *15 */
16 public classTcpChatWindow {17 private int destPort; //目的端口
18 private int listenPort; //监听端口
19 private Socket socketToSendMessage; //发送消息的socket
20
21 public TcpChatWindow(int destPort, intlistenPort){22 this.destPort =destPort;23 this.listenPort =listenPort;24
25 //init
26 init();27 }28
29 /**
30 * 接收消息线程31 *@authorGXF32 *33 */
34 class ReceiveMessageThread implementsRunnable{35 //接收消息socket
36 privateServerSocket receiveMessageSocket;37
38 @Override39 public voidrun() {40 //init
41 init();42 while(true){43 try{44 Socket clientSocket =receiveMessageSocket.accept();45 InputStream inputStreamOfClientSocket =clientSocket.getInputStream();46 byte buff[] = new byte[1024];47 int lengthOfMessage = -1;48 StringBuilder sb = newStringBuilder();49 //read message
50 while((lengthOfMessage = inputStreamOfClientSocket.read(buff)) > 0){51 sb.append(new String(buff, 0, lengthOfMessage));52 }//while53 //输出消息到控制台
54 System.out.println(sb.toString());55 //关闭socket
56 clientSocket.close();57 } catch(IOException e) {58 e.printStackTrace();59 }60 }61 }62
63 /**
64 * 对线程进行初始化65 */
66 public voidinit(){67 try{68 receiveMessageSocket = newServerSocket(listenPort);69 } catch(IOException e) {70 e.printStackTrace();71 }72 }73
74 }75
76 //初始化操作
77 private voidinit(){78
79 //start ReceiveMessageThread
80 Thread receiveMessageThread = new Thread(newReceiveMessageThread());81 receiveMessageThread.start();82 //input message and send message
83 while(true){84 Scanner scanner = newScanner(System.in);85 String messageToSend =scanner.nextLine();86 //getSocketToSendMessage();
87 sendMessage(messageToSend);88 scanner.close();89 }90
91
92 }93
94 /**
95 * 向对方发送消息96 *@parammessage97 */
98 public voidsendMessage(String message){99 try{100 Socket socketToSendMessage = new Socket(InetAddress.getByName("127.0.0.1"), destPort);101 OutputStream outputStreamOfClientSocket =socketToSendMessage.getOutputStream();102 byte bytesOfMessage[] =message.getBytes();103 outputStreamOfClientSocket.write(bytesOfMessage);104 socketToSendMessage.close();105 } catch(IOException e) {106 e.printStackTrace();107 }108 }109
110 //获取发送消息的socket
111 publicSocket getSocketToSendMessage(){112 if(socketToSendMessage == null){113 try{114 socketToSendMessage = new Socket(InetAddress.getByName("127.0.0.1"), destPort);115 } catch(IOException e) {116 //TODO Auto-generated catch block
117 e.printStackTrace();118 }119 }120 returnsocketToSendMessage;121 }122
123 public static voidmain(String args[]){124 if(args.length != 2){125 System.out.println("usage: java TcpChatWindow destport listenport");126 return;127 }128 int destPort = Integer.valueOf(args[0]);129 int listenPort = Integer.valueOf(args[1]);130
131 TcpChatWindow chatWindow = newTcpChatWindow(destPort, listenPort);132 }133 }