用Java完成服务器和客户端的循环聊天

用Java完成服务器和客户端的循环聊天,第1张

import javaawtColor;

import javaawtDimension;

import javaawtFont;

import javaawteventActionEvent;

import javaawteventActionListener;

import javaawteventKeyEvent;

import javaawteventKeyListener;

import javaioIOException;

import javanetDatagramPacket;

import javanetDatagramSocket;

import javanetInetAddress;

import javaxswingJButton;

import javaxswingJFrame;

import javaxswingJPanel;

import javaxswingJScrollPane;

import javaxswingJSplitPane;

import javaxswingJTextArea;

public class ChatFrame extends JPanel{

private static final long serialVersionUID = 1L;

private int width=450,height=450;

boolean flag;

int port = 10086;

String address = "1921683352";

// int port = 2425;

DatagramSocket send;

DatagramSocket receive;

UDP_Send udpSend;

JTextArea showTextArea;

JTextArea inputTextArea;

JPanel rightPanel;

JScrollPane scrollPane1;

JScrollPane scrollPane2;

JSplitPane splitPane1;

JSplitPane splitPane2;

JButton sendButton;

JButton closeButton;

JPanel buttonpane;

public ChatFrame() throws Exception {

super(null, true);

send = new DatagramSocket();

receive = new DatagramSocket(port);

udpSend =new UDP_Send(send,port);

showTextArea = new JTextArea();

showTextAreasetLineWrap(true);

showTextAreasetEditable(false);

showTextAreasetForeground(new Color(0xf90033));

scrollPane1 = new JScrollPane(showTextArea);

inputTextArea = new JTextArea();

inputTextAreasetLineWrap(true);

scrollPane2 = new JScrollPane(inputTextArea);

rightPanel = new JPanel();

rightPanelsetBackground(new Color(0x0099ff));

splitPane1 = new JSplitPane(JSplitPaneVERTICAL_SPLIT,true,scrollPane1,scrollPane2);

splitPane2 = new JSplitPane(JSplitPaneHORIZONTAL_SPLIT,true,splitPane1,rightPanel);

inputTextAreaaddKeyListener(new KeyListener() {

@Override

public void keyTyped(KeyEvent e) {}

@Override

public void keyReleased(KeyEvent e) {}

@Override

public void keyPressed(KeyEvent e) {

switch (egetKeyCode()) {

case KeyEventVK_ENTER:

udpSendsend(port);

inputTextAreasetText("");

break;

default:

break;

}

}

});

sendButton = new JButton("发送");

sendButtonaddActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

udpSendsend(port);

inputTextAreasetText("");

}

});

closeButton = new JButton("关闭");

closeButtonaddActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

Systemexit(0);

}

});

buttonpane = new JPanel();

buttonpaneadd(closeButton);

buttonpaneadd(sendButton);

splitPane1setBounds(0, 0, width, height-32);

splitPane1setDividerLocation(08);

splitPane2setBounds(0, 0, width, height-32);

splitPane2setDividerLocation(08);

buttonpanesetBounds(width-250, height-32, 140, 32);

thisadd(splitPane2);

thisadd(buttonpane);

thissetPreferredSize(new Dimension(width,height));

new Thread(new UDP_Receive(receive))start();

}

public static void main(String[] args) throws Exception {

JFrame frame = new JFrame();

framesetContentPane(new ChatFrame());

framepack();

framesetResizable(false);

framesetLocationRelativeTo(null);

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

framesetVisible(true);

}

class UDP_Send{

DatagramSocket datagramSocket;

int port;

String text;

public UDP_Send(DatagramSocket datagramSocket,int port) {

thisdatagramSocket = datagramSocket;

thisport = port;

}

public void send(int port) {

try {

text = new String(inputTextAreagetText()getBytes(), "utf-8");

byte buffered[] = textgetBytes();

DatagramPacket database = new DatagramPacket(buffered,

bufferedlength, InetAddressgetByName(address),port) ;

showTextAreasetFont(new Font("微软雅黑", FontCENTER_BASELINE, 24));

showTextAreaappend("本机"+":\n");

showTextAreasetFont(new Font("仿宋", FontCENTER_BASELINE, 16));

showTextAreaappend(" "+text+"\n");

datagramSocketsend(database);

} catch (IOException e) {

eprintStackTrace();

}

}

}

class UDP_Receive implements Runnable {

DatagramSocket datagramSocket;

public UDP_Receive(DatagramSocket datagramSocket) {

thisdatagramSocket = datagramSocket;

}

@Override

public void run() {

try {

byte buff[] = new byte[4096];

DatagramPacket database = new DatagramPacket(buff, bufflength);

while (true) {

datagramSocketreceive(database);

String text = new String(databasegetData(), 0,databasegetLength(),"utf-8");

showTextAreasetFont(new Font("微软雅黑", FontCENTER_BASELINE, 24));

showTextAreaappend(databasegetAddress()+":\n");

showTextAreasetFont(new Font("仿宋", FontCENTER_BASELINE, 16));

showTextAreaappend(" "+text+"\n");

}

} catch (IOException e) {

eprintStackTrace();

}

}

}

}

想要实现群聊功能其实也并不难,你可以选择用第三方的sdk做一些开发。推荐你可以接入ZEGO即时通讯SDK,发者通过IM SDK和API可快速开发IM功能,让您快速实现1v1,群聊,聊天室等功能,支持Java等语言开发,就算是千万级并发也稳定,比自主开发节省90%时间和成本。

以上就是关于用Java完成服务器和客户端的循环聊天全部的内容,包括:用Java完成服务器和客户端的循环聊天、java群聊功能是如何实现的、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10102319.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存