问题描述
为何只能进行单方通信?无法进行双方通信?????求大神解救!!!!!!!!!!packageswing;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Container;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintStream;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;importjavax.swing.JFrame;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.JTextField;importjavax.swing.border.BevelBorder;publicclassTest1903extendsJFrame{privateBufferedReaderreader;privateServerSocketserver;privateSocketsocket;privatePrintWriterwriter;//Socketsocket;privateJTextAreata=newJTextArea();privateJTextFieldtb=newJTextField();Containerc;publicTest1903(Stringtitle){super(title);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);c=this.getContentPane();finalJScrollPanescrollpane=newJScrollPane();scrollpane.setBorder(newBevelBorder(BevelBorder.RAISED));scrollpane.setBackground(Color.GREEN);getContentPane().add(scrollpane,BorderLayout.CENTER);scrollpane.setViewportView(ta);tb.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){writer.println(tb.getText());ta.append(tb.getText()+'n');ta.setSelectionEnd(ta.getText().length());tb.setText("");}});c.add(tb,"South");}publicvoidgetserver(){try{server=newServerSocket(8998);ta.append("服务器套接字已经创建成功n");for(;;){ta.append("等待客户机的连接n");this.socket=this.server.accept();this.reader=newBufferedReader(newInputStreamReader(this.socket.getInputStream()));getClientMessage();}}catch(Exceptione){e.printStackTrace();}}privatevoidconnect(){ta.append("tryconnectn");try{//Thread.sleep(4000);socket=newSocket("127.0.0.1",8999);writer=newPrintWriter(socket.getOutputStream(),true);ta.append("Succeedinconnectingn");}catch(Exceptione){e.printStackTrace();}}privatevoidgetClientMessage(){try{for(;;){if(this.reader.ready()){ta.append("客户机:"+this.reader.readLine());}}}catch(Exceptione){e.printStackTrace();try{if(this.reader!=null){this.reader.close();}if(this.socket!=null){this.socket.close();}}catch(IOExceptione1){e1.printStackTrace();}}}publicstaticvoidmain(String[]args){Test1903tcp=newTest1903("jahfb");tcp.setSize(500,500);tcp.setVisible(true);tcp.connect();tcp.getserver();}}packageswing;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Container;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintStream;importjava.io.PrintWriter;importjava.net.ServerSocket;importjava.net.Socket;importjavax.swing.JFrame;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.JTextField;importjavax.swing.border.BevelBorder;publicclassTest190301extendsJFrame{privateBufferedReaderreader;privateServerSocketserver;privateSocketsocket1;privatePrintWriterwriter;//Socketsocket;privateJTextAreata=newJTextArea();privateJTextFieldtb=newJTextField();Containerc;publicTest190301(Stringtitle){super(title);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);c=this.getContentPane();finalJScrollPanescrollpane=newJScrollPane();scrollpane.setBorder(newBevelBorder(BevelBorder.RAISED));scrollpane.setBackground(Color.GREEN);getContentPane().add(scrollpane,BorderLayout.CENTER);scrollpane.setViewportView(ta);tb.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){writer.println(tb.getText());ta.append(tb.getText()+'n');ta.setSelectionEnd(ta.getText().length());tb.setText("");}});c.add(tb,"South");}publicvoidgetserver(){try{server=newServerSocket(8999);ta.append("服务器套接字已经创建成功n");for(;;){ta.append("等待客户机的连接n");this.socket1=this.server.accept();this.reader=newBufferedReader(newInputStreamReader(this.socket1.getInputStream()));getClientMessage();}}catch(Exceptione){e.printStackTrace();}}privatevoidconnect(){ta.append("tryconnectn");try{//Thread.sleep(1000);socket1=newSocket("127.0.0.1",8998);writer=newPrintWriter(socket1.getOutputStream(),true);ta.append("Succeedinconnectingn");}catch(Exceptione){e.printStackTrace();}}privatevoidgetClientMessage(){try{for(;;){if(this.reader.ready()){ta.append("客户机:"+this.reader.readLine());}}}catch(Exceptione){e.printStackTrace();try{if(this.reader!=null){this.reader.close();}if(this.socket1!=null){this.socket1.close();}}catch(IOExceptione1){e1.printStackTrace();}}}publicstaticvoidmain(String[]args){Test190301tcp=newTest190301("asfaunf");tcp.setSize(500,500);tcp.setVisible(true);tcp.connect();tcp.getserver();}}