模拟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();
	}

}


 

效果图:

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值