java网络编程
客户端:
1 package talktest; 2 import java.io.*; 3 import java.net.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 public class SingleTalkClient implements ActionListener 7 { 8 Frame f=new Frame("QQ"); 9 Button b=new Button("sending"); 10 TextArea show=new TextArea("",10,50,TextArea.SCROLLBARS_VERTICAL_ONLY); 11 TextArea send=new TextArea("",10,50,TextArea.SCROLLBARS_VERTICAL_ONLY); 12 String fromServer, fromUser; 13 boolean sbye = false; 14 boolean ubye = false; 15 Socket client = null; 16 PrintWriter out = null; 17 BufferedReader in = null; 18 String record; 19 public void actionPerformed(ActionEvent e){ 20 if(e.getSource()==b){ 21 if(ubye==false){ 22 fromUser=send.getText(); 23 fromUser=fromUser.replace("Client input:",""); 24 record=record+"from Client:"+fromUser+"\n";//将发送语句中的Client input:去掉 25 show.setText(record); 26 out.println(fromUser); 27 //out.flush(); 28 if(fromUser.equals("Bye.")){ 29 ubye=true; 30 } 31 } 32 } 33 } 34 public void fun()throws IOException{ 35 36 try { 37 client = new Socket("127.0.0.1", 4444); 38 out = new PrintWriter(client.getOutputStream(), true); //auto flush 39 in = new BufferedReader(new InputStreamReader(client.getInputStream())); 40 } catch (UnknownHostException e) { 41 System.err.println("Don't know about host: 127.0.0.1."); 42 System.exit(1); 43 } catch (IOException e) { 44 System.err.println("Couldn't get I/O for the connection to: 127.0.0.1."); 45 System.exit(1); 46 } 47 48 f.setSize(500,500); 49 f.add(show); 50 f.add(send); 51 f.add(b); 52 f.setBackground(Color.GRAY); 53 f.setLayout(new FlowLayout(FlowLayout.CENTER)); 54 f.addWindowListener(new WindowAdapter(){ 55 public void windowClosing(WindowEvent e){ 56 System.exit(0); 57 } 58 }); 59 f.setVisible(true); 60 b.addActionListener(this); 61 62 63 64 65 //从标准输入流(键盘)中获取信息 66 //BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); 67 68 record=""; 69 send.setText("Client input:"); 70 /* 71 if(fromServer.equals("Bye.")){ 72 sbye=true; 73 } 74 record=record+"from Server:"+fromServer+"\n"; 75 show.setText(record); 76 if(sbye){ 77 ubye=true; 78 out.println("Bye."); 79 out.flush(); 80 } 81 else{ 82 send.setText("Client input:"); 83 */ 84 //System.out.print("Client input:"); 85 //fromUser = stdIn.readLine(); 86 while( true ) 87 { 88 //send.setText("Client input:"); 89 if( sbye == false ) 90 { 91 fromServer=in.readLine(); 92 record=record+"from Server:"+fromServer+"\n"; 93 show.setText(record); 94 //fromServer = in.readLine(); 95 //System.out.println("from Server: " + fromServer); 96 if (fromServer.equals("Bye.")) 97 sbye = true; 98 } 99 if( ubye == false ) 100 { 101 //out.println(fromUser); 102 //out.flush(); 103 //System.out.println("Client: " + fromUser); 104 //if (fromUser.equals("Bye.")) 105 //ubye = true; 106 if(sbye==true){ 107 out.println("Bye."); 108 out.flush(); 109 ubye=true; 110 } 111 else 112 send.setText("Client input:"); 113 } 114 115 116 //if( ubye == false ) 117 //{ 118 // System.out.print("Client input:"); 119 // fromUser = stdIn.readLine(); 120 //} 121 122 if( ubye == true && sbye == true ) 123 break; 124 } 125 126 out.close(); 127 in.close(); 128 //stdIn.close(); 129 client.close(); 130 } 131 public static void main(String[] args) throws IOException 132 { 133 new SingleTalkClient().fun(); 134 135 } 136 }
服务器端:
1 package talktest; 2 import java.net.*; 3 import java.io.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 public class SingleTalkServer implements ActionListener 7 { 8 9 Frame f=new Frame("QQ"); 10 Button b=new Button("sending"); 11 TextArea show=new TextArea("",10,50,TextArea.SCROLLBARS_VERTICAL_ONLY); 12 TextArea send=new TextArea("",10,50,TextArea.SCROLLBARS_VERTICAL_ONLY); 13 //String fromServer, fromUser; 14 //boolean sbye = false; 15 //boolean ubye = false; 16 boolean sinbye = false; 17 boolean inbye = false; 18 String sinputLine, inputLine; 19 ServerSocket serverSocket = null; 20 Socket clientSocket = null; 21 PrintWriter out = null; 22 BufferedReader in = null; 23 String record; 24 public void actionPerformed(ActionEvent e){ 25 if(e.getSource()==b){ 26 if(sinbye==false){ 27 sinputLine=send.getText(); 28 sinputLine=sinputLine.replace("Server input:",""); 29 record=record+"from Server:"+sinputLine+"\n";//将发送语句中的Client input:去掉 30 show.setText(record); 31 out.println(sinputLine); 32 //out.flush(); 33 if(sinputLine.equals("Bye.")){ 34 sinbye=true; 35 } 36 } 37 } 38 } 39 public void fun()throws IOException{ 40 41 try { 42 serverSocket = new ServerSocket(4444); 43 } catch (IOException e) { 44 System.err.println("Could not listen on port: 4444."); 45 System.exit(1); 46 } 47 48 49 try { 50 clientSocket = serverSocket.accept(); //程序将在此等候客户端的连接 51 } catch (IOException e) { 52 System.err.println("Accept failed."); 53 System.exit(1); 54 } 55 show.setText("Accept OK!"+"\n"); 56 57 //打开输入/输出流 58 out = new PrintWriter(clientSocket.getOutputStream(), true); //auto flush 59 in = new BufferedReader( 60 new InputStreamReader( 61 clientSocket.getInputStream())); 62 63 f.setSize(500,500); 64 f.add(show); 65 f.add(send); 66 f.add(b); 67 f.setBackground(Color.GRAY); 68 f.setLayout(new FlowLayout(FlowLayout.CENTER)); 69 f.addWindowListener(new WindowAdapter(){ 70 public void windowClosing(WindowEvent e){ 71 System.exit(0); 72 } 73 }); 74 f.setVisible(true); 75 b.addActionListener(this); 76 77 78 79 //从标准输入流(键盘)中获取信息 80 //BufferedReader sin = new BufferedReader( new InputStreamReader( System.in ) ); 81 82 83 record=""; 84 //send.setText("Server input:"); 85 inputLine = in.readLine(); 86 //System.out.println( "from Client: " + inputLine ); 87 if(inputLine.equals("Bye.")){ 88 inbye=true; 89 } 90 record=record+"from Client:"+inputLine+"\n"; 91 show.setText(record); 92 if(inbye){ 93 sinbye=true; 94 out.println("Bye."); 95 out.flush(); 96 } 97 else{ 98 send.setText("Server input:"); 99 } 100 //System.out.print("Server input:"); 101 //sinputLine = sin.readLine(); 102 103 while( true ) 104 { 105 //send.setText("Server input:"); 106 if( inbye == false ) 107 { 108 inputLine = in.readLine(); 109 //System.out.println( "from Client: " + inputLine ); 110 record=record+"from Client:"+inputLine+"\n"; 111 show.setText(record); 112 if (inputLine.equals("Bye.")) 113 inbye = true; 114 } 115 if( sinbye == false ) 116 { 117 //out.println(sinputLine); 118 //out.flush(); 119 //System.out.println("Server: " + sinputLine); 120 121 //if (sinputLine.equals("Bye.")) 122 //sinbye = true; 123 if(inbye==true){ 124 out.println("Bye."); 125 out.flush(); 126 sinbye=true; 127 } 128 else 129 send.setText("Server input:"); 130 } 131 //if( sinbye == false ) 132 //{ 133 //System.out.print("Server input:"); 134 //sinputLine = sin.readLine(); 135 //} 136 137 if( sinbye == true && inbye == true ) 138 break; 139 } 140 141 out.close(); 142 in.close(); 143 //sin.close(); 144 clientSocket.close(); 145 serverSocket.close(); 146 } 147 public static void main(String[] args) throws IOException 148 { 149 new SingleTalkServer().fun(); 150 } 151 }

浙公网安备 33010602011771号