【JAVA】多线程网络通信---聊天室

package Chat;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class chatClient implements ActionListener , Runnable{
	JTextArea showArea;
	JTextField msgText;
	JFrame mainJframe;
	JButton senBtn;
	JScrollPane JSPane;
	JPanel pane;
	Container con;
	Thread thread = null;
	Socket connectToServer;
	DataInputStream inFromserver;
	DataOutputStream outToServer;
	public chatClient(){
		mainJframe = new JFrame("聊天 -- 客户端");
		con = mainJframe.getContentPane();
		showArea = new JTextArea();
		showArea.setEditable(false);
		showArea.setLineWrap(true);
		JSPane = new JScrollPane(showArea);
		msgText  = new JTextField();
		msgText.setColumns(30);
		msgText.addActionListener(this);
		senBtn = new JButton("发送");
		senBtn.addActionListener(this);
		pane = new JPanel();
		pane.setLayout(new FlowLayout());
		pane.add(msgText);
		pane.add(senBtn);
		con.add(JSPane, BorderLayout.CENTER);
		con.add(pane, BorderLayout.SOUTH);
		mainJframe.setSize(500,400);
		mainJframe.setVisible(true);
		mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		try{
			connectToServer = new Socket("localhost",5500);
			inFromserver = new DataInputStream(connectToServer.getInputStream());
			outToServer = new DataOutputStream(connectToServer.getOutputStream());
			showArea.append("连接成功,请说话\n");
			thread = new Thread(this);
			thread.setPriority(Thread.MIN_PRIORITY);
			thread.start();
		}catch(IOException E){
			showArea.append("对不起,没能连接服务器\n");
			msgText.setEditable(false);
			msgText.setEnabled(false);
		}
	}
	public static void main(String[]args){
		new chatClient();
	}
	public void run(){
		try{
			while(true){
				showArea.append("赵冠文说: "+inFromserver.readUTF()+"\n");
				Thread.sleep(1000);
			}
		}catch(IOException e){
			
		}catch(InterruptedException E){}
		
	}
	public void actionPerformed(ActionEvent e){
		String s = msgText.getText();
		if(s.length()>0){
			try {
				outToServer.writeUTF(s);
				outToServer.flush();
				showArea.append("王阔说: "+msgText.getText()+"\n");
				msgText.setText(null);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
		showArea.append("你的消息"+msgText.getText()+"未能发送出去\n");
			}
			
		}
	}

}

package Chat;

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import javax.swing.*;

public  class chatServer implements ActionListener,Runnable {
	JTextArea showArea;
	JTextField msgText;
	JFrame mainJframe;
	JButton sentBtn;
	JScrollPane JSPane;
	JPanel pane;
	Container con;
	Thread thread = null;
	ServerSocket serverSocket;
	Socket connectToClient;
	DataInputStream inFromClient;
	DataOutputStream outToClient;
	public chatServer(){
		mainJframe = new JFrame("聊天 -- 服务器端");
		con=mainJframe.getContentPane();
		showArea = new JTextArea();
		showArea.setEditable(false);
		showArea.setLineWrap(true);
		JSPane = new JScrollPane(showArea);
		msgText = new JTextField();
		msgText.setColumns(30);
		msgText.addActionListener(this);
		sentBtn = new JButton("发送");
		sentBtn.addActionListener(this);
		pane = new JPanel();
		pane.setLayout(new FlowLayout());
		pane.add(msgText);
		pane.add(sentBtn);
		con.add(JSPane, BorderLayout.CENTER);
		con.add(pane, BorderLayout.SOUTH);
		mainJframe.setSize(500, 400);
		mainJframe.setVisible(true);
		mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		try{
			serverSocket = new ServerSocket(5500);
			showArea.append("正在等待对话请求\n");
			connectToClient = serverSocket.accept();
			inFromClient = new DataInputStream(connectToClient.getInputStream());
			outToClient = new DataOutputStream(connectToClient.getOutputStream());
			thread = new Thread(this);
			thread.setPriority(Thread.MIN_PRIORITY);
			thread.start();
		}catch(IOException E){
			showArea.append("对不起,不能创建服务器\n");
			msgText.setEditable(false);
			sentBtn.setEnabled(false);
			
		}
	}
	public static void main(String[]args){
		new chatServer();
	}
	public void actionPerformed(ActionEvent e){
		String s  = msgText.getText();
		if(s.length()>0){
			try{
				outToClient.writeUTF(s);
				outToClient.flush();
				showArea.append("赵冠文说:  "+msgText.getText()+"\n");
				msgText.setText(null);
			}catch(IOException E){
				showArea.append("你的消息"+msgText.getText()+"未能发送");
			}
		}
	}
	public void run(){
		try{
			while(true){
				showArea.append("王阔说: "+inFromClient.readUTF()+"\n");
				Thread.sleep(1000);
			}
		}catch(IOException E){}
		 catch(InterruptedException E){}
		
	}
	}
<img src="https://img-blog.csdn.net/20160913223715935?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" style="font-family: Arial, Helvetica, sans-serif; white-space: normal; background-color: rgb(255, 255, 255);" alt="" />


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值