加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java socket tcp通讯,多个客户端连接服务器简单例子

发布时间:2020-12-15 03:21:40 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.net.*; import java.io.*; public class TCPServer { public TCPServer() throws Exception{ServerSocket ss = new ServerSocket(6666);

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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));
		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,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,23);
		contentPane.add(button);
	}
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读