java socket tcp通讯,多个客户端连接服务器简单例子详解编程语言

服务器端

import java.net.*;   
import java.io.*;   
   
public class TCPServer {   
	 
	public TCPServer() throws Exception{ 
		ServerSocket ss = new ServerSocket(6666); 
        while (true) {   
            Socket s = ss.accept();   
            new ServerThread(s).start(); 
             
        } 
	} 
    public static void main(String[] args) throws Exception {   
       new TCPServer(); 
   
    }   
     
    protected class ServerThread extends Thread{ 
    	 Socket s; 
    	public ServerThread(Socket s1){ 
    		s = s1; 
    	} 
    	public void run(){ 
    		 System.out.println("a client connect!");   
    		try{ 
                DataInputStream dis = new DataInputStream(s.getInputStream());//服务器通过输入管道接收数据流   
                String str; 
          		while((str = dis.readUTF()).length() > 0){ 
                    System.err.println(str);//输出读入的数据   
          		} 
 
    		} 
    		catch(Exception ex){ 
    			ex.printStackTrace(); 
    		} 
 
    	} 
    } 
}

客户端

import java.awt.EventQueue; 
 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.io.DataOutputStream; 
import java.io.OutputStream; 
import java.net.Socket; 
import javax.swing.JTextField; 
 
public class TCPClientFrame extends JFrame { 
	Socket s; 
	DataOutputStream dos; 
	private JPanel contentPane; 
	private JTextField textField; 
	private JButton button; 
 
	/** 
	 * Launch the application. 
	 */ 
	public static void main(String[] args) { 
		EventQueue.invokeLater(new Runnable() { 
			public void run() { 
				try { 
					TCPClientFrame frame = new TCPClientFrame(); 
					frame.setVisible(true); 
				} catch (Exception e) { 
					e.printStackTrace(); 
				} 
			} 
		}); 
	} 
 
	/** 
	 * Create the frame. 
	 */ 
	public TCPClientFrame() { 
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
		setBounds(100, 100, 450, 300); 
		contentPane = new JPanel(); 
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
		setContentPane(contentPane); 
		contentPane.setLayout(null); 
		 
		JButton btnNewButton = new JButton("连接"); 
		btnNewButton.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) { 
				try{ 
					s = new Socket("127.0.0.1", 6666);   
			        OutputStream os = s.getOutputStream();  //得到输出管道   
			        dos = new DataOutputStream(os);//创建数据传输流   
				} 
				catch(Exception ex){ 
					ex.printStackTrace(); 
				} 
			} 
		}); 
		btnNewButton.setBounds(10, 10, 93, 23); 
		contentPane.add(btnNewButton); 
		 
		JButton btnNewButton_1 = new JButton("发消息"); 
		btnNewButton_1.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) { 
				try{ 
					 dos.writeUTF(textField.getText());//客户端传输数据   
				     dos.flush();   
				} 
				catch(Exception ex){ 
					ex.printStackTrace(); 
				} 
			} 
		}); 
		btnNewButton_1.setBounds(141, 10, 93, 23); 
		contentPane.add(btnNewButton_1); 
		 
		textField = new JTextField(); 
		textField.setBounds(10, 56, 66, 21); 
		contentPane.add(textField); 
		textField.setColumns(10); 
		 
		button = new JButton("自动发消息"); 
		button.addActionListener(new ActionListener() { 
			public void actionPerformed(ActionEvent e) { 
				try{ 
					while(true){ 
						 dos.writeUTF("======自动发的消息");//客户端传输数据   
					     dos.flush();   
					} 
					 
				} 
				catch(Exception ex){ 
					ex.printStackTrace(); 
				} 
			} 
		}); 
		button.setBounds(314, 10, 93, 23); 
		contentPane.add(button); 
	} 
}

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/10584.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论