模拟QQ(Socket编程)

客户端代码:

package CrescentMoonpackage;

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.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

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

public class QQ extends JFrame{
	JLabel lbl;
	JTextField txt;
	JButton btnStart;
	JTextArea jtaContent;
	Socket s;
	Container c;
	JPanel pup;
	DataOutputStream out;
	public void init(){
		lbl=new JLabel("请输入");
		txt=new JTextField(20);
		btnStart=new JButton("发送");
		jtaContent=new JTextArea(10,20);
		c=this.getContentPane();
		pup=new JPanel();
		pup.setLayout(new FlowLayout());
		pup.add(lbl);pup.add(txt);pup.add(btnStart);
		c.add(pup,BorderLayout.NORTH);
		c.add(jtaContent,BorderLayout.CENTER);
		btnStart.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String temp=txt.getText();
				txt.setText("");
				try {
					out.writeUTF(temp);
					if(temp.equals("bye")){
						out.close();
						s.close();
					}
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				
				jtaContent.setText(jtaContent.getText()+"\n我说"+temp);
			}
		});
		try {
			s=new Socket("localhost",5000);
			out=new DataOutputStream(s.getOutputStream());
		} catch (UnknownHostException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(500,400);
		this.setVisible(true);
	}
	public QQ(){
		init();
		Receivethread rt=new Receivethread(s,jtaContent);
		rt.start();
	}
	public static void main(String[] args) {
		QQ qq=new QQ();
	}

}
class Receivethread extends Thread{
	Socket s;
	JTextArea jtaContent;
	DataInputStream in;
	public Receivethread(Socket s,JTextArea jtaContent){
		this.s=s;
		this.jtaContent=jtaContent;
	}
	public void run(){
				try {
					in=new DataInputStream(s.getInputStream());
					
				} catch (IOException e) {
					e.printStackTrace();
				}
				while(true){
					String temp;
					try {
						temp= in.readUTF();
						jtaContent.append("\n对方说:"+temp);
					if(temp.equals("bye")){
						in.close();
						s.close();
					}
					} catch (IOException e) {
						e.printStackTrace();
					}					
				}					
		}
}


 

 

服务器代码:

 

package CrescentMoonpackage;


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.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

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

public class QQ2 extends JFrame{
	JLabel lbl;
	JTextField txt;
	JButton btnStart;
	JTextArea jtaContent;
	Socket s;
	ServerSocket ss;
	Container c;
	JPanel pup;
	DataOutputStream out;
	public void init(){
		lbl=new JLabel("请输入");
		txt=new JTextField(20);
		btnStart=new JButton("发送");
		jtaContent=new JTextArea(10,20);
		c=this.getContentPane();
		pup=new JPanel();
		pup.setLayout(new FlowLayout());
		pup.add(lbl);pup.add(txt);pup.add(btnStart);
		c.add(pup,BorderLayout.NORTH);
		c.add(jtaContent,BorderLayout.CENTER);
		btnStart.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				String temp=txt.getText();
				txt.setText("");
				try {
					out.writeUTF(temp);
					if(temp.equals("bye")){
						out.close();
						s.close();
					}
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				
				jtaContent.setText(jtaContent.getText()+"\n我说"+temp);
			}
		});
		try {
			ss=new ServerSocket(5000);
			s=ss.accept();
			out=new DataOutputStream(s.getOutputStream());
		} catch (UnknownHostException e1) {
			System.out.println("服务器启动监听5000端口失败");
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(500,400);
		this.setVisible(true);
	}
	public QQ2(){
		init();
		Receivethread rt=new Receivethread(s,jtaContent);
		rt.start();
	}
	public static void main(String[] args) {
		QQ2 qq=new QQ2();
	}

}


 

效果图:

模拟QQ(Socket编程)_第1张图片

 

你可能感兴趣的:(java)