关于网络互连聊天

import java.io.*;

import java.net.*;

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

/**

 * 用户界面ClientUI

 */

public class ClientUI extends JFrame {

JTextArea mainArea;

 JTextArea sendArea;

 ChatClient client;

 JTextField ipArea;

 JButton btnLink;

 public void setClient(ChatClient client) {

this.client = client;

}

 public ClientUI() {

super("客户端");

Container contain = getContentPane();

contain.setLayout(new BorderLayout());

mainArea = new JTextArea();

JScrollPane mainAreaP = new JScrollPane(mainArea);// 为文本区添加滚动条

JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());

sendArea = new JTextArea(3, 8);

JButton sendBtn = new JButton("发送");

sendBtn.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

client.sendMsg(sendArea.getText());

mainArea.append("【客户端】" + sendArea.getText() + "\n");

sendArea.setText("");

}

});

JPanel ipPanel = new JPanel();

ipPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 10));

ipPanel.add(new JLabel("服务器:"));

ipArea = new JTextField(12);

ipArea.setText("219.218.21.179");

ipPanel.add(ipArea);

btnLink = new JButton("连接");

ipPanel.add(btnLink);

btnLink.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

client = new ChatClient(ipArea.getText(), 6666, ClientUI.this);// 创建socket对象

ClientUI.this.setClient(client);

}

});

panel.add(sendBtn, BorderLayout.EAST);

panel.add(sendArea, BorderLayout.CENTER);

contain.add(ipPanel, BorderLayout.NORTH);

contain.add(mainAreaP, BorderLayout.CENTER);

contain.add(panel, BorderLayout.SOUTH);

setSize(500, 300);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

 

public static void main(String[] args) {

ClientUI ui = new ClientUI();

}

}

 /**

 * 通讯类ChatClient负责守候数据到来

 */

class ChatClient extends Thread {

Socket sc;// 对象sc,用来处理与服务

BufferedReader in;// 声明输入流缓冲区,用于存储服务器发来的信息

 PrintWriter out;// 声明打印输出流,用于信息的发送

 ClientUI ui;

 public ChatClient(String ip, int port, ClientUI ui) {// 初始化ChatClient

this.ui = ui;

try {

sc = new Socket(ip, port); // 创建sc, 用服务器ip和端口作参数

System.out.println("已顺利联接到服务器。");

out = new PrintWriter(sc.getOutputStream(), true);

in = new BufferedReader(new InputStreamReader(sc.getInputStream()));

} catch (Exception e) {

System.out.println(e);

}

start();

}

 

public void run() { // 用于监听服务器端发送来的信息

String msg = "";

while (true) {

try {

msg = in.readLine();// 从缓冲区读入一行字符存于msg

} catch (SocketException ex) {

System.out.println(ex);

break;

} catch (Exception ex) {

System.out.println(ex);

}

if (msg != null && msg.trim() != "") {// msg信息不为空

System.out.println(">>" + msg);

ui.mainArea.append(msg + "\n");// msg信息添加到客户端的文本区域内

}

}

}

 

public void sendMsg(String msg) {// 用于发送信息

try {

out.println("【客户端】" + msg);

} catch (Exception e) {

System.out.println(e);

}

}

}

 

import java.io.*;

import java.net.*;

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

 

/**

 * 服务端界面ServerUI

 */

public class ServerUI 

    extends JFrame 

    {

    JTextArea mainArea;

 JTextArea sendArea;

 JTextField indexArea;

  SvrCom server;

 public void setServer(SvrCom server) 

    {

     this.server = server;

    }

   public ServerUI() 

    {

    super("服务器端");

    Container contain = getContentPane();

    contain.setLayout(new BorderLayout());

    mainArea = new JTextArea();

    JScrollPane mainAreaP = new JScrollPane(mainArea);

    JPanel panel = new JPanel();

    panel.setLayout(new BorderLayout());

    sendArea = new JTextArea(3, 8);

    JButton sendBtn = new JButton("发送");

    sendBtn.addActionListener(new ActionListener()// 注册动作监听器

{

public void actionPerformed(ActionEvent ae) 

{

server.sendMsg(sendArea.getText());// 把信息传递到客户端

mainArea.append("【服务器】" + sendArea.getText() + "\n");// 把信息显示在服务器的聊天记录区域

sendArea.setText("");

}

});

JPanel tmpPanel = new JPanel();

indexArea = new JTextField(2);

indexArea.setText("0");

tmpPanel.add(sendBtn);

tmpPanel.add(indexArea);

panel.add(tmpPanel, BorderLayout.EAST);

panel.add(sendArea, BorderLayout.CENTER);

contain.add(mainAreaP, BorderLayout.CENTER);

contain.add(panel, BorderLayout.SOUTH);

setSize(500, 300);

setVisible(true);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

 public static void main(String[] args)

{

ServerUI ui = new ServerUI();

SvrCom server = new SvrCom(ui);// 创建并启动网络通讯线程,准备接受客户端数据包

}

}

/**

 * 通讯类SvrCom负责守候数据到来

 */

class SvrCom extends Thread// 网络通讯类

{

Socket client;

 ServerSocket soc;

 BufferedReader in;

 PrintWriter out;

ServerUI ui;

 // ChatServer server;

 public SvrCom(ServerUI ui) { // 初始化SvrCom

this.ui = ui;

ui.setServer(this);

try {

soc = new ServerSocket(6666); // 开设服务器端口6666

System.out.println("启动服务器成功,等待端口号:6666");

client = soc.accept();// 当客户机请求连接时,创建一条链接

System.out.println("连接成功!来自" + client.toString());

in = new BufferedReader(new InputStreamReader(client

.getInputStream()));

out = new PrintWriter(client.getOutputStream(), true);

} catch (Exception ex) {

System.out.println(ex);

}

start();

}

 public void run() {// 用于监听客户端发送来的信息

String msg = "";

while (true) {

try {

msg = in.readLine();// in对象上读数据信息

} catch (SocketException ex) {

System.out.println(ex);

break;

} catch (Exception ex) {

System.out.println(ex);

}

if (msg != null && msg.trim() != "") {

System.out.println(">>" + msg);

ui.mainArea.append(msg + "\n");

}

}

}

public void sendMsg(String msg) {// 用于发送信息

try {

out.println("【服务器】" + msg);// 把信息写入输出流

} catch (Exception e) {

System.out.println(e);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值