首页 > 编程语言 > 详细

java实现TCP通信(带界面)

时间:2020-12-11 14:26:15      阅读:37      评论:0      收藏:0      [点我收藏+]

服务端:

package NetWork;

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

public class Server 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 Server() {
// 设置界面
mainJframe = new JFrame("TCP聊天服务端");
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.setLocation(300, 200);
mainJframe.setSize(500, 350);
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 Server();
}

@Override // 响应按钮事件,发送消息给对方
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 el) {
showArea.append("你的消息:“" + msgText.getText() + "”未能发出去!\n");
}
}
}

@Override
// 本线程负责将客户机传来的信息显示在对话区域
public void run() {
try {
while (true) {
showArea.append("客户端:" + inFromClient.readUTF() + "\n");
Thread.sleep(1000);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

客户端:

package NetWork;

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

public class Client implements ActionListener, Runnable {
JTextArea showArea;
JTextField msgText;
JFrame mainJframe;
JButton sentBtn;
JScrollPane JSPane;
JPanel pane;
Container con;
Thread thread = null;
Socket connectToServer;
DataInputStream inFromServer;
DataOutputStream outToServer;

public Client() {
mainJframe = new JFrame("TCP聊天客户端");
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.setLocation(700, 200);
mainJframe.setSize(500, 350);
mainJframe.setVisible(true);
mainJframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建套接字连接到服务器
try {
connectToServer = new Socket("localhost", 5500); // 本地IP
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 (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
showArea.append("抱歉,未能连接到服务器!\n");
msgText.setEditable(false);
sentBtn.setEnabled(false);
}
}

public static void main(String[] args) {
new Client();
}

@Override //
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) {
showArea.append("你的消息:“" + msgText.getText() + "”未能发送出去!\n");
}
}
}

// 本线程负责将服务器传来的消息显示在对话区域
public void run() {
try {
while (true) {
showArea.append("服务端 :" + inFromServer.readUTF() + "\n");
Thread.sleep(1000);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

结果展示:

技术分享图片

 

java实现TCP通信(带界面)

原文:https://www.cnblogs.com/beimo-ly/p/14119919.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!