用TCP实现双向聊天系统
程序员文章站
2022-03-15 20:35:23
...
服务器端代码:
package chat1;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.net.*;
import javax.swing.*;
public class Server extends JFrame implements ActionListener,Runnable {
private JTextArea taMsg=new JTextArea("以下是聊天记录\n");
private JTextField tfMsg=new JTextField("请您输入信息\n");
private JButton btSend= new JButton("发送");
private Socket s=null;
public Server(){
this.setTitle("服务器端");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(taMsg,BorderLayout.CENTER);
tfMsg.setBackground(Color.yellow);
this.add(tfMsg,BorderLayout.NORTH);
this.add(btSend,BorderLayout.SOUTH);
btSend.addActionListener(this);
this.setSize(200,300);
this.setVisible(true);
try {
ServerSocket ss=new ServerSocket(9999);
s=ss.accept();
new Thread(this).start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void run(){
try {
while(true){
InputStream is=s.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String str=br.readLine();
taMsg.append(str+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
try {
OutputStream os=s.getOutputStream();
PrintStream ps=new PrintStream(os);
ps.println("服务器说:"+tfMsg.getText());
} catch (Exception e2) {
e2.printStackTrace();
}
}
public static void main(String[] args) {
Server server1=new Server();
}
}
客户端代码:
package chat1;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.sound.midi.Transmitter;
import javax.swing.*;
public class Client extends JFrame implements ActionListener,Runnable{
private JTextArea taMsg=new JTextArea("以下是聊天记录\n");
private JTextField tfMsg=new JTextField("请您输入信息");
private JButton btSend=new JButton("发送");
private Socket s=null;
public Client(){
this.setTitle("客户端");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(taMsg,BorderLayout.CENTER);
tfMsg.setBackground(Color.yellow);
this.add(tfMsg,BorderLayout.NORTH);
this.add(btSend,BorderLayout.SOUTH);
btSend.addActionListener(this);
this.setSize(200,300);
this.setVisible(true);
try {
s=new Socket("127.0.0.1", 9999);
new Thread(this).start();
} catch (Exception e) {
e.printStackTrace();
}
}
public void run(){
try {
while(true){
InputStream is=s.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String str=br.readLine();
taMsg.append(str+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent e){
try {
OutputStream os=s.getOutputStream();
PrintStream ps=new PrintStream(os);
ps.println("客户端说:"+tfMsg.getText());
} catch (Exception e2) {
e2.printStackTrace();
}
}
public static void main(String[] args) {
Client client1=new Client();
}
}
上一篇: 剑指leetcode—旋转图像
下一篇: Java Servlet基本方法介绍
推荐阅读
-
用PHP实现一个双向队列_PHP教程
-
java学习之利用TCP实现的简单聊天示例代码
-
详解iOS开发 - 用AFNetworking实现https单向验证,双向验证
-
java学习之利用TCP实现的简单聊天示例代码
-
也谈用JSP实现新郎、sohu新闻系统的技术。
-
c#用Treeview实现FolderBrowerDialog 和动态获取系统图标(运用了Win32 dll类库)
-
Java编程实现基于TCP协议的Socket聊天室示例
-
c#用Treeview实现FolderBrowerDialog 和动态获取系统图标(运用了Win32 dll类库)
-
不到40行代码用Python实现一个简单的推荐系统
-
Python基于TCP实现会聊天的小机器人功能示例