首页 > 代码库 > Java学习笔记_网络+多线程
Java学习笔记_网络+多线程
支持同时收发的客户端和服务器端
客户端
1 import javax.swing.*; 2 import java.awt.*; 3 import java.io.*; 4 import java.net.*; 5 import java.awt.event.*; 6 7 public class ChatClient { 8 private JTextArea incoming; 9 private JTextField outgoing; 10 private PrintWriter writer; 11 private BufferedReader reader; 12 13 public static void main(String[] args) { 14 ChatClient client = new ChatClient(); 15 client.go(); 16 } 17 18 private void go() { 19 build_gui(); 20 setup_net(); 21 } 22 23 private void build_gui() { 24 JFrame frame = new JFrame("Chat Client"); 25 JPanel panel = new JPanel(); 26 27 incoming = new JTextArea(15, 50); 28 incoming.setLineWrap(true); 29 incoming.setWrapStyleWord(true); 30 incoming.setEditable(false); 31 JScrollPane qScroller = new JScrollPane(incoming); 32 qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 33 qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 34 panel.add(incoming); 35 36 outgoing = new JTextField(20); 37 panel.add(outgoing); 38 39 JButton button = new JButton("Send"); 40 button.addActionListener(new SendButtonListener()); 41 panel.add(button); 42 43 frame.add(BorderLayout.CENTER, panel); 44 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 45 frame.setSize(600, 400); 46 frame.setVisible(true); 47 } 48 49 public class SendButtonListener implements ActionListener { 50 public void actionPerformed(ActionEvent a) { 51 String msg = outgoing.getText(); 52 try { 53 writer.println(msg); 54 writer.flush(); 55 } catch (Exception ex) { ex.printStackTrace(); } 56 outgoing.setText(""); 57 outgoing.requestFocus(); 58 } 59 } 60 61 class ReceiveThreadJob implements Runnable { 62 public void run() { 63 String msg; 64 try { 65 while ( (msg = reader.readLine()) != null ) { 66 incoming.append(msg + "\n"); 67 } 68 } catch (IOException ex) { System.out.println("server disconnected"); } 69 } 70 } 71 72 private void setup_net() { 73 try { 74 Socket s = new Socket("127.0.0.1", 4242); 75 writer = new PrintWriter(s.getOutputStream()); 76 InputStreamReader streamReader = new InputStreamReader( s.getInputStream() ); 77 reader = new BufferedReader( streamReader ); 78 79 Runnable receive_thread_job = new ReceiveThreadJob(); 80 Thread receive_thread = new Thread( receive_thread_job ); 81 receive_thread.start(); 82 83 } catch (IOException ex) { 84 ex.printStackTrace(); 85 } 86 } 87 }
服务器端
1 import java.io.*; 2 import java.net.*; 3 import java.util.ArrayList; 4 5 public class ChatServer { 6 BufferedReader reader; 7 ArrayList<PrintWriter> writer_array = new ArrayList<PrintWriter>(); 8 9 public static void main(String[] args) { 10 ChatServer server = new ChatServer(); 11 server.go(); 12 } 13 14 private void go() { 15 setup_net(); 16 } 17 18 private void setup_net() { 19 try { 20 ServerSocket serverSock = new ServerSocket(4242); 21 while (true) { 22 Socket s = serverSock.accept(); 23 System.out.println("client connected"); 24 InputStreamReader streamReader = new InputStreamReader( s.getInputStream() ); 25 reader = new BufferedReader( streamReader ); 26 PrintWriter writer = new PrintWriter(s.getOutputStream()); 27 writer_array.add(writer); 28 Runnable server_proc_thread_job = new ServerProcThreadJob(); 29 Thread server_proc = new Thread( server_proc_thread_job ); 30 server_proc.start(); 31 } 32 } catch (IOException ex) { 33 ex.printStackTrace(); 34 } 35 } 36 37 class ServerProcThreadJob implements Runnable { 38 public void run() { 39 String msg; 40 try { 41 while ( (msg = reader.readLine()) != null ) { 42 System.out.println(msg); 43 for ( PrintWriter writer: writer_array ) { 44 writer.println(msg); 45 writer.flush(); 46 } 47 } 48 } catch (IOException ex) { System.out.println("client disconnected"); } 49 } 50 } 51 }
Java学习笔记_网络+多线程
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。