`
leoizumi
  • 浏览: 92981 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

TalkToMe 0.3

    博客分类:
  • J2EE
阅读更多

客户端 :

 

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class TalkClient extends JFrame{

	private JPanel myPanel;
	private JLabel enterLabel;
	private JTextField enterField;
	private JTextArea displayArea;
	private DataOutputStream output;
	private Socket socket;
	
	
	
	public TalkClient() {
		super("Client Port");	
		Container container = getContentPane();	
		myPanel = new JPanel();
		myPanel.setLayout(new FlowLayout());
		enterLabel = new JLabel("输入:");
		enterField = new JTextField(20);
		enterField.setEditable(true);
		
		enterField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String message = enterField.getText();
				sendData(message);
				displayArea.setText(displayArea.getText() + message+'\n');
				enterField.setText("");
			}
		});
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				disconnect();
				System.exit(0);
			}
			
		});
		myPanel.add(enterLabel);
		myPanel.add(enterField);
		container.add(myPanel,BorderLayout.NORTH);
		displayArea = new JTextArea();
		container.add(new JScrollPane(displayArea),BorderLayout.CENTER);
		setSize(320, 180);
		setVisible(true);
		connect();
	}
	
	public void connect() {
		try {
			socket = new Socket("127.0.0.1",7777);
			output = new DataOutputStream(socket.getOutputStream());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void disconnect() {
		try {
			output.close();
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void sendData(String str) {
		
		//System.out.println(str);
		
		try {
			output.writeUTF(str);	
			output.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {

		new TalkClient();
	}

}

 

 

 

服务器端 :

 

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.server.SocketSecurityException;
import java.util.ArrayList;
import java.util.List;


public class TalkServer {
	ServerSocket ss = null;
    List<Client> clients = new ArrayList<Client>();
	
	public static void main(String[] args) {
	
		new TalkServer().start();

	}
	
	public void start() {
		
		try {
			ss = new ServerSocket(7777);
		
			while(true) {
				Socket socket = ss.accept();
				Client client = new Client(socket);
				System.out.println("连接上了");
				new Thread(client).start();
				clients.add(client);
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	class Client implements Runnable {

		private Socket socket;
		DataInputStream dis = null;
		DataOutputStream dos = null;
		
		public Client(Socket socket) {
			this.socket = socket;
			
			try {
				dis = new DataInputStream(socket.getInputStream());
				dos = new DataOutputStream(socket.getOutputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			
		}
		
		public void run() {
			try {
				String str = dis.readUTF();
				System.out.println(str);
				
				

			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics