socket tcp 통신, 여러 클라이언트 연결 서버 간단한 예

3479 단어
서버 측
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);
	}
}

좋은 웹페이지 즐겨찾기