socket编程TCP实现简单聊天功能_第1页
socket编程TCP实现简单聊天功能_第2页
socket编程TCP实现简单聊天功能_第3页
socket编程TCP实现简单聊天功能_第4页
socket编程TCP实现简单聊天功能_第5页
免费预览已结束,剩余13页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、计算机网络报告socket编程TCP实现简单聊天功能java网络编程,通过TCP,Socket实现多对一的局域网聊天室可以实现多个客户端连接服务器,服务器接收到信息就会把信息广播到所有的客户端服务端源码: import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;

2、import .Socket;import java.util.List;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;/*这个类是服务器端的UI*/public class ServerUI extends JFrame public stat

3、ic void main(String args) ServerUI serverUI = new ServerUI();public JButton btStart;启动服务器public JButton btSend;发送信息按钮public JTextField tfSend;需要发送的文本信息public JTextArea taShow;川言息展示public Server server;/R3来监听客户端连接static List<Socket> clients;保存连接到服务器的客户端public ServerUI() super("服务器端");

4、btStart = new JButton("启动月艮务");btSend = new JButtonC 发送信息)tfSend = new JTextField(10);taShow = new JTextArea();btStart.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) server = new Server(ServerUI.this););btSend.addActionListener(new ActionListener() public

5、 void actionPerformed(ActionEvent e) server.sendMsg(tfSend.getText();tfSend.setText(""););this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) int a = JOptionPane.showConfirmDialog(null, " 确定关闭吗? ", "温馨提示 ",JOptionPane.YES_NO_OPTION);if (a

6、 = 1) server.closeServer();System.exit(0); / 关闭);JPanel top = new JPanel(new FlowLayout();top.add(tfSend);top.add(btSend);top.add(btStart);this.add(top, BorderLayout.SOUTH);final JScrollPane sp = new JScrollPane();sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_A LWAYS);sp.setViewportVi

7、ew(this.taShow);this.taShow.setEditable(false);this.add(sp, BorderLayout.CENTER);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setSize(400, 300);this.setLocation(100, 200);this.setVisible(true);import java.io.BufferedReader;import java.io.IOException;import java.io.PrintWriter;import .Ser

8、verSocket;import .Socket;import java.util.ArrayList;/*这个类是服务器端的等待客户端连接*/ public class Server extends Thread ServerUI ui;ServerSocket ss;BufferedReader reader;PrintWriter writer;public Server(ServerUI ui) this.ui = ui;this.start();public void run() try ss = new ServerSocket(1228);ui.clients=new Array

9、List<Socket>();println(" 启动服务器成功:端口1228");while (true) println(" 等待客户端");Socket client = ss.accept();ui.clients.add(client);println(" 连接成功" + client.toString();new ListenerClient(ui, client); catch (IOException e) println(" 启动服务器失败:端口1228");println(e.toS

10、tring();e.printStackTrace();public synchronized void sendMsg(String msg) try for (int i = 0; i < ui.clients.size(); i+) Socket client = ui.clients.get(i);writer = new PrintWriter(client.getOutputStream(),true);writer.println(msg); catch (Exception e) println(e.toString();public void println(Strin

11、g s) if (s != null) this.ui.taShow.setText(this.ui.taShow.getText() + s + "n"); System.out.println(s + "n");public void closeServer() try if (ss != null) ss.close();if (reader != null) reader.close();if (writer != null)writer.close(); catch (IOException e) / TODO Auto-generated c

12、atch block e.printStackTrace();import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import .Socket;/*这个类是服务器端的等待客户端发送信息*/public class ListenerClient extends Thread BufferedReader reader;PrintWriter writer;ServerUI ui;Socket client;publi

13、c ListenerClient(ServerUI ui, Socket client) this.ui = ui;this.client=client;this.start();/为每一个客户端创建线程等待接收信息,然后把信息广播出去public void run() String msg = ""while (true) try reader = new BufferedReader(new InputStreamReader(client.getInputStream();writer = new PrintWriter(client.getOutputStream(

14、), true);msg = reader.readLine();sendMsg(msg); catch (IOException e) println(e.toString();/ e.printStackTrace();break;if (msg != null && msg.trim() != "") println(">>" + msg);/把信息广播到所有用户public synchronized void sendMsg(String msg) try for (int i = 0; i < ui.clien

15、ts.size(); i+) Socket client = ui.clients.get(i);writer = new PrintWriter(client.getOutputStream(), true);writer.println(msg); catch (Exception e) println(e.toString();public void println(String s) if (s != null) this.ui.taShow.setText(this.ui.taShow.getText() + s + "n");System.out.println

16、(s + "n");客户端源码:import java.awt.BorderLayout;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JOpti

17、onPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;public class ClientUI extends JFrame public static void main(String args) ClientUI client = new ClientUI();public ClientUI() super("客户端)btStart = new JButton("启动连接&quo

18、t;);btSend = new JButtonC 发送信息)tfSend = new JTextField(10);tfIP = new JTextField(10);tfPost = new JTextField(5);taShow = new JTextArea();btStart.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) server = new ClientThread(ClientUI.this););btSend.addActionListener(new A

19、ctionListener() public void actionPerformed(ActionEvent e) server.sendMsg(tfSend.getText();tfSend.setText(""););this.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) int a = JOptionPane.showConfirmDialog(null, " 确定关闭吗? ", "温馨提示 ",JOptionPan

20、e.YES_NO_OPTION);if (a = 1) System.exit(0); / 关闭);JPanel top = new JPanel(new FlowLayout();top.add(tfSend);top.add(btSend);top.add(btStart);this.add(top, BorderLayout.SOUTH);final JScrollPane sp = new JScrollPane();sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_A LWAYS);sp.setViewportV

21、iew(this.taShow);this.taShow.setEditable(false);this.add(sp, BorderLayout.CENTER);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setSize(400, 300);this.setLocation(600, 200);this.setVisible(true);publicJButton btStart;publicJButton btSend;publicJTextField tfSend;publicJTextField tfIP;publi

22、cJTextField tfPost;publicJTextArea taShow;public ClientThread serverimport java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import .Socket;public class ClientThread extends Thread ClientUI ui;Socket client;BufferedReader reader;PrintWriter writer;public ClientThread(ClientUI ui) this.ui = ui;try client = new Socket("127.0.0.1", 1228);/这里设置连接服务器端的IP 的端口println(" 连接服务器成功:端口1228");reader = new Buffe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论