问题描述
packagetest05;publicclassGameSize{publicstaticfinalintGAME_X=200;publicstaticfinalintGAME_Y=300;publicstaticfinalintGAME_WIDTH=400;publicstaticfinalintGAME_HIGH=530;}packagetest05;importjava.net.*;importjava.io.DataInputStream;importjava.io.EOFException;importjava.io.IOException;importjava.net.ServerSocket;importjava.net.Socket;/***1.主线程负责打开服务器以及接受客户端的链接*2.每接受一个客户端的链接就开辟一个线程***/publicclassChatServer{/**主线程*/publicstaticvoidmain(String[]args){ServerSocketss=null;ChatServercs=newChatServer();try{ss=newServerSocket(8888);cs.accept(ss);}catch(BindExceptione){System.out.println("端口已被占用,请关闭端口");}catch(IOExceptione){e.printStackTrace();}}/**接受客户端的连接*/publicvoidaccept(ServerSocketss)throwsIOException{Socketsocket=null;booleanbAccept=true;try{while(bAccept){socket=ss.accept();newThread(newClientThread(ss,socket)).start();}}catch(IOExceptione){e.printStackTrace();}finally{if(socket!=null){socket.close();socket=null;}}}classClientThreadimplementsRunnable{booleanflag=true;ServerSocketss=null;Socketsocket=null;DataInputStreamdis=null;publicClientThread(ServerSocketss,Socketsocket){this.ss=ss;this.socket=socket;}publicvoidrun(){try{dis=newDataInputStream(socket.getInputStream());while(flag){System.out.println(dis.readUTF());}}catch(EOFExceptione){try{socket.close();System.out.println("客户端已关闭");}catch(IOExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}}catch(IOExceptione){e.printStackTrace();}finally{try{if(socket!=null){socket.close();socket=null;}}catch(IOExceptione){}}}}}packagetest05;importjava.awt.*;/***1.窗口的设计*2.按钮的添加**/importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.io.*;importjava.net.*;publicclassChatClientextendsFrame{TextFieldtf=null;TextAreata=null;Buttonbutton=null;Socketsocket=null;DataOutputStreamdos=null;/***主方法:加载窗口**/publicstaticvoidmain(String[]args){newChatClient().launchFrame();}/**窗口的属性:*1.加载窗口时与服务器建立连接*/publicvoidlaunchFrame(){setBounds(GameSize.GAME_X,GameSize.GAME_Y,GameSize.GAME_WIDTH,GameSize.GAME_HIGH);setVisible(true);setTitle("我的聊天");this.setResizable(false);this.setLayout(null);addConpomnent();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){CloseAll(socket,dos);System.exit(0);}});connection();}/**向服务器端申请链接*初始化dos*/publicvoidconnection(){try{socket=newSocket("localhost",8888);dos=newDataOutputStream(socket.getOutputStream());System.out.println("一个客户端请求链接");}catch(UnknownHostExceptione){CloseAll(socket,dos);System.out.println("未找到主机");}catch(IOExceptione){CloseAll(socket,dos);System.out.println("IO有错误");}}/**流的关闭*/publicvoidCloseAll(Closeable...io){for(Closeabletemp:io){try{if(temp!=null){temp.close();temp=null;}}catch(IOExceptione){e.printStackTrace();}}}/**向服务器端发送信息*/publicvoidsendMessage(Stringstr){try{dos.writeUTF(str);dos.flush();}catch(IOExceptione){CloseAll(socket,dos);e.printStackTrace();}}/**添加conpoment*/publicvoidaddConpomnent(){tf=newTextField();tf.setBounds(10,GameSize.GAME_HIGH*4/5+30,GameSize.GAME_WIDTH*4/5,GameSize.GAME_HIGH-GameSize.GAME_HIGH*4/5);tf.addActionListener(newtfAction());add(tf);ta=newTextArea();ta.setBounds(10,30,GameSize.GAME_WIDTH,GameSize.GAME_HIGH*4/5);add(ta);button=newButton("发送");button.setBounds(GameSize.GAME_WIDTH*4/5,GameSize.GAME_HIGH*4/5+30,GameSize.GAME_WIDTH-GameSize.GAME_WIDTH*4/5,GameSize.GAME_HIGH-GameSize.GAME_HIGH*4/5);button.addActionListener(newbuttonAction());add(button);}/**textArea的监听*/classtfActionimplementsActionListener{@OverridepublicvoidactionPerformed(ActionEvente){tf=(TextField)e.getSource();ta.setText(ta.getText()+tf.getText()+'n');sendMessage(tf.getText());tf.setText(null);}}/**button的监听*/classbuttonActionimplementsActionListener{@OverridepublicvoidactionPerformed(ActionEvente){Buttonb=(Button)e.getSource();ta.setText(ta.getText()+tf.getText()+'n');sendMessage(tf.getText());tf.setText(null);}}}
解决方案
解决方案二:
楼主是想表达什么啊~~~~~~