Create a class to find out when the class is internal, external classes or anonymous classes
A preliminary understanding of the steps debugger.
In order to increase functionality:
Chat 0.1 version produces a window
Chat 0.2 version of the preliminary layout window
Chat 0.3 version adds the window closing event
Chat 0.4 version added TextField event processing
Chat 0.5 version adds ChatServer
Chat 0.6 version of Client connect to server
Chat 0.7 version of the client to the server sends a message
Chat 0.8 version fixes the previous version of the problem, the client may send multiple statements
Chat 0.9 release fixes a bug version
Chat 1.0 version even on multiple clients
Chat 1.1 version of the server-side forwarding
Chat 1.2 version of the client receives
The final version of the amendment Chat 1.3
Chat experience
To understand the importance of iterative version of the program, no one will be able to write any program is perfect to add a new feature in each iteration.
Save the code at any time, Ctrl + S
Do not throw an exception is encountered directly, not afraid of difficulties, thoughtful, abnormal abnormal resolved with try-catch statement, exceptions may be mined after each thrown.
Like how to write? Class is written inside or outside the class, or anonymous class? Specifically to see what this class do
The correct code is 100 lines, 300 lines of code perfect. `
Server-side code:
import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.EOFException;import java.io.IOException;import java.net.*;import java.util.*;publicclassChatServer{boolean started =false;// 成员变量
ServerSocket ss = null;
List<Client> clients =newArrayList<Client>();publicstaticvoidmain(String[] args){newChatServer().start();}publicvoidstart(){try{
ss =newServerSocket(8888);
started =true;}catch(BindException e){
System.out.println("端口使用中....");
System.out.println("请关掉相关程序,请重新运行服务器!");
System.exit(0);}catch(IOException e){
e.printStackTrace();}try{while(started){// 无限接受客户端连接进来boolean bConnected =false;
Socket s = ss.accept();
Client c =newClient(s);
System.out.println("a client connected!");newThread(c).start();
clients.add(c);// dis.close();}}catch(IOException e){
e.printStackTrace();}finally{try{
ss.close();}catch(IOException e){
e.printStackTrace();}}}classClientimplementsRunnable{private Socket s;private DataInputStream dis = null;private DataOutputStream dos = null;privateboolean bConnected =false;publicClient(Socket s){// 构造方法this.s = s;try{
dis =newDataInputStream(s.getInputStream());
dos =newDataOutputStream(s.getOutputStream());
bConnected =true;}catch(IOException e){
e.printStackTrace();}}publicvoidsend(String str){// 发送方法try{
dos.writeUTF(str);}catch(IOException e){
e.printStackTrace();}}publicvoidrun(){try{while(bConnected){// 接收到连接,也不断接受消息
String str = dis.readUTF();
System.out.println(str);for(int i =0; i < clients.size(); i++){
Client c = clients.get(i);
c.send(str);}}}catch(EOFException e){
System.out.println("client closed!!");}catch(IOException e){
e.printStackTrace();}finally{try{if(dis != null)
dis.close();if(dos != null)
dos.close();if(s != null){
s.close();
s = null;}}catch(IOException e1){
e1.printStackTrace();}}}}}