首页 > 编程 > Java > 正文

java基于C/S模式实现聊天程序(客户端)

2019-11-26 09:21:40
字体:
来源:转载
供稿:网友

经过这几天对java的学习,用java做了这个计算机网络的课程设计,基于C/S模式的简单聊天程序,此篇文章介绍一些客户端的一些东西。

先讲一讲此聊天程序的基本原理,客户端发送消息至服务器,服务器收到消息之后将其转发给连接服务器的所有客户端,来自客户端的消息中包含发件人的名字。

客户端的主要功能是发送消息和接收消息,客户端设置好了端口和服务器地址,并创立客户端自己的套接字,用作和服务器通信的一个标识。布局就不多说了,主要说说监视器和两个重要的线程:发送和接收。

监视器中,登录按钮触发的功能是设置用户名,并且建立和服务器的连接,同时还要创建接收线程,并使其开始运行。

下面说一说,发送和接收的线程:发送线程是建立数据输出流,将想要文本输入区中的消息以UTF字符串的形式写入到数据流中,并且在发送成功后清空输入框。并且该线程由“发送”按钮触发。

接收线程是在登录之后就建立的,线程中建立输入流,并且读出流中的UTF字符串,将其显示到文本展示区,就完成了信息的接收。

客户端大致的功能和组成就是这些了,下一篇我将讲一下有关服务器的东西。

界面展示:


package client;import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;import java.util.*;import javax.swing.*;public class Client extends JFrame { Socket clientsocket = null; DataInputStream in = null; DataOutputStream out = null; JTextArea inputText; String SerAddress = "192.168.1.100"; int SendPort = 8888; JTextField NickName; JTextArea textShow; JButton button, setbutton; public Client() { // 构造函数,创建一个布局并初始化 init(); setVisible(true); setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); setBounds(480, 160, 340, 460); setTitle("好好学习天天向上聊天室"); setResizable(false); } void init() { // 初始化函数,设置布局并且设置监视器 inputText = new JTextArea(4, 29); button = new JButton("  发送  "); JLabel label = new JLabel("昵称"); setbutton = new JButton("  登录  "); textShow = new JTextArea(15, 29); textShow.setEditable(false); NickName = new JTextField(10); inputText.setBackground(new Color(45, 210, 209)); setLayout(new FlowLayout()); getContentPane().setBackground(new Color(20, 85, 237)); add(new JScrollPane(textShow)); textShow.setBackground(new Color(45, 210, 209)); setbutton.setBackground(new Color(236, 134, 21)); button.setBackground(new Color(236, 134, 21)); NickName.setBackground(new Color(45, 210, 209)); label.setForeground(new Color(243, 243, 14)); add(label); add(NickName); add(setbutton); add(new JScrollPane(inputText)); add(button); setbutton.addActionListener(new ActionListener() {  //添加监视器  public void actionPerformed(ActionEvent e) {  Thread readData;  Read read = null;  try {   clientsocket = new Socket();   read = new Read();   readData = new Thread(read);   if (clientsocket.isConnected()) {   } else {   InetAddress address = InetAddress.getByName(SerAddress);   InetSocketAddress socketAddress = new InetSocketAddress(    address, SendPort);   clientsocket.connect(socketAddress);   textShow.append(new java.text.SimpleDateFormat(    "yy-MM-dd HH:mm:ss").format(new Date())    + "/n与服务器连接成功/n已登录聊天室/n");   in = new DataInputStream(clientsocket.getInputStream());   out = new DataOutputStream(clientsocket    .getOutputStream());   read.setDataInputStream(in);   readData.start();   }  } catch (Exception e1) {   textShow.append(new java.text.SimpleDateFormat(    "yy-MM-dd HH:mm:ss").format(new Date())    + "/n服务器连接失败/n");  }  } }); button.addActionListener(new ActionListener() {  public void actionPerformed(ActionEvent e) {  Send send = new Send();  Thread sendData = new Thread(send);  send.setDataOutputStream(out);  sendData.start();  } }); addWindowListener(new WindowAdapter() {  //响应关闭按钮的功能  public void windowClosing(WindowEvent e) {  int option = JOptionPane   .showConfirmDialog(null, "亲爱的你真的要离开聊天室么?",    " 好好学习,天天向上", JOptionPane.YES_NO_OPTION,    JOptionPane.QUESTION_MESSAGE);  if (option == JOptionPane.YES_OPTION)   System.exit(0);  } }); } // init结束 class Read implements Runnable {  //读取输入流的线程 DataInputStream in; public void setDataInputStream(DataInputStream in) {  this.in = in; } public void run() {  String result;  while (true) {  try {   result = in.readUTF();   textShow.append(new java.text.SimpleDateFormat(    "yy-MM-dd HH:mm:ss").format(new Date())    + "/n"    + result);  } catch (IOException e) {   textShow.append(new java.text.SimpleDateFormat(    "yy-MM-dd HH:mm:ss").format(new Date())    + "/n与服务器断开连接/n");   break;  }  } } } class Send implements Runnable {  // 发送消息的输出流线程  DataOutputStream out; public void setDataOutputStream(DataOutputStream out) {  this.out = out; } public void run() {  String message = null;  message = NickName.getText() + ":" + inputText.getText() + "/n";  try {  out.writeUTF(message);  inputText.setText("");  } catch (Exception e2) {  textShow.append("发送失败:未连接到服务器/n");  } } } public static void main(String args[]) { Client client = new Client(); }}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表