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

TalkToMe 0.1

    博客分类:
  • 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.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 ObjectOutputStream 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) {
				sendData(e.getActionCommand());
				enterField.setText("");
			}
		});
		
		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);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void sendData(String str) {
		
		try {
			output.writeObject("客户端>>> "+str);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {

		new TalkClient();
	}

}

 

 

 

服务器端 :

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.rmi.server.SocketSecurityException;


public class TalkServer {

	private ObjectInputStream input;
	
	public static void main(String[] args) {
		
		try {
			ServerSocket ss = new ServerSocket(7777);
		
			while(true) {
				Socket socket = ss.accept();
				System.out.println("连接上了");
				
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics