「用java编写聊天室」java聊天室的设计与实现代码
本篇文章给大家谈谈用java编写聊天室,以及java聊天室的设计与实现代码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、如何用Java编写一个聊天室程序,可以支持单聊和多聊?
- 2、JAVA编局域网聊天室
- 3、用JAVA 编写简单网络聊天程序
- 4、java语音聊天室怎样实现啊?
- 5、java一对一聊天室是怎么实现的?
- 6、java简易聊天室
如何用Java编写一个聊天室程序,可以支持单聊和多聊?
聊天程序又叫即时通讯系统
分类两部分:客户端和服务端
客户端:用户聊天的界面
服务端:接收消息并转发到指定用户
其中服务端和客户端用tcp或者udp连接,使用socket编程完成通信。
按着这个思路可以开发出一套聊天程序
客户端常用界面 bs版本的又layim
服务端 openfire或者自己实现
JAVA编局域网聊天室
我是用多播实现的,MulticastSocket是DatagramSocket的子类,暂时还只能用于局域网,不过多播是可以在广域网上进行的
大概步骤如下:
1.加入多播组
2.发送/接收数据包
发送需要从文本输入
接收时使用了一个线程循环接收
3.发生异常离开多播组
我也见过用ServerSocket,Socket实现的,随便找本网络编程的书,上面都有类似的代码
编程环境:JDK1.7,eclipse3.5
用JAVA 编写简单网络聊天程序
/**
* 基于UDP协议的聊天程序
*
* 2007.9.18
* */
//导入包
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.net.*;
public class Chat extends JFrame implements ActionListener
{
//广播地址或者对方的地址
public static final String sendIP = "172.18.8.255";
//发送端口9527
public static final int sendPort = 9527;
JPanel p = new JPanel();
List lst = new List(); //消息显示
JTextField txtIP = new JTextField(18); //填写IP地址
JTextField txtMSG = new JTextField(20); //填写发送消息
JLabel lblIP = new JLabel("IP地址:");
JLabel lblMSG = new JLabel("消息:");
JButton btnSend = new JButton("发送");
byte [] buf;
//定义DatagramSocket的对象必须进行异常处理
//发送和接收数据报包的套接字
DatagramSocket ds = null;
//=============构造函数=====================
public Chat()
{
CreateInterFace();
//注册消息框监听器
txtMSG.addActionListener(this);
btnSend.addActionListener(this);
try
{
//端口:9527
ds =new DatagramSocket(sendPort);
}
catch(Exception ex)
{
ex.printStackTrace();
}
//============接受消息============
//匿名类
new Thread(new Runnable()
{
public void run()
{
byte buf[] = new byte[1024];
//表示接受数据报包
while(true)
{
try
{
DatagramPacket dp = new DatagramPacket(buf,1024,InetAddress.getByName(txtIP.getText()),sendPort);
ds.receive(dp);
lst.add("【消息来自】◆" + dp.getAddress().getHostAddress() + "◆"+"【说】:" + new String (buf,0,dp.getLength()) /*+ dp.getPort()*/,0);
}
catch(Exception e)
{
if(ds.isClosed())
{
e.printStackTrace();
}
}
}
}
}).start();
//关闭窗体事件
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent w)
{
System.out.println("test");
int n=JOptionPane.showConfirmDialog(null,"是否要退出?","退出",JOptionPane.YES_NO_OPTION);
if(n==JOptionPane.YES_OPTION)
{
dispose();
System.exit(0);
ds.close();//关闭ds对象//关闭数据报套接字
}
}
});
}
//界面设计布局
public void CreateInterFace()
{
this.add(lst,BorderLayout.CENTER);
this.add(p,BorderLayout.SOUTH);
p.add(lblIP);
p.add(txtIP);
p.add(lblMSG);
p.add(txtMSG);
p.add(btnSend);
txtIP.setText(sendIP);
//背景颜色
lst.setBackground(Color.yellow);
//JAVA默认风格
this.setUndecorated(true);
this.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
this.setSize(600,500);
this.setTitle("〓聊天室〓");
this.setResizable(false);//不能改变窗体大小
this.setLocationRelativeTo(null);//窗体居中
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.setVisible(true);
txtMSG.requestFocus();//消息框得到焦点
}
//===============================Main函数===============================
public static void main(String[]args)
{
new Chat();
}
//================================发送消息===============================
//消息框回车发送消息事件
public void actionPerformed(ActionEvent e)
{
//得到文本内容
buf = txtMSG.getText().getBytes();
//判断消息框是否为空
if (txtMSG.getText().length()==0)
{
JOptionPane.showMessageDialog(null,"发送消息不能为空","提示",JOptionPane.WARNING_MESSAGE);
}
else{
try
{
InetAddress address = InetAddress.getByName(sendIP);
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);
ds.send(dp);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
txtMSG.setText("");//清空消息框
//点发送按钮发送消息事件
if(e.getSource()==btnSend)
{
buf = txtMSG.getText().getBytes();
try
{
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName(txtIP.getText()),sendPort);
}
catch(Exception ex)
{
ex.printStackTrace();
}
txtMSG.setText("");//清空消息框
txtMSG.requestFocus();
}
}
}
java语音聊天室怎样实现啊?
实现java语音聊天室的方法很简单,要么自己从零研发,要么直接使用别人的成品,意思是找网上的第三方,他们自己有研发产品的。建议考虑ZEGO即构科技,他们的语聊房SDK很好用的,提供低延迟和低成本的方案供选择,在保障语音质量的前提下,码率最低可到8kbps,可以试试。
java一对一聊天室是怎么实现的?
想要实现java一对一聊天室的方法比较简单,要么直接找源码,要么使用第三方的sdk做一些开发。建议可以考虑接入ZEGO即时通讯SDK来实现,支持Android java开发,集成方便,一对一、一对多聊天室都可快速搭建,重要的是不担心消息会丢失,千万级并发也稳定,个人建议你们可以试试。
java简易聊天室
你要的就是点对点通信,见以下例子:
1.简单服务器端
/*
import java.net.*;
import java.io.*;
*/
ServerSocket server=null;
try {
server=new ServerSocket(%%1);
}catch(Exception e){
System.out.println("不能监听:"+e.toString());
}
Socket socket=null;
try {
socket=server.accept();
BufferedReader %%3=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter %%4=new PrintWriter(socket.getOutputStream());
String %%2=%%3.readLine();
%%4.println("");
%%4.flush();
%%4.close();
%%3.close();
}
catch(IOException e){
System.out.println("出错:"+e.toString());
}finally{
try {
if(socket!=null){
socket.close();
server.close();
}
}
catch(IOException e){
e.printStackTrace();
}
}
2.简单客户端
/*
import java.net.*;
import java.io.*;
*/
Socket socket=null;
try {
socket=new Socket(%%1,%%2);
PrintWriter %%3=new PrintWriter(socket.getOutputStream());
BufferedReader %%4 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
%%3.println("");
%%3.flush();
String %%5=%%4.readLine();
%%6
%%3.close();
%%4.close();
}catch(Exception e){
e.printStackTrace();
}
finally{
try {
socket.close();
}
catch(IOException e){
e.printStackTrace();
}
}
3.获得本机IP
//import java.net.*;
String strIP = null;
try
{
strIP =InetAddress.getLocalHost().getHostAddress().toString();
}
catch(UnknownHostException e)
{
e.printStackTrace();
}
/*
%%1=InetAddress.getLocalHost().getHostAddress();
EnumerationNetworkInterface netInterfaces = null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
System.out.println("DisplayName:" + ni.getDisplayName());
System.out.println("Name:" + ni.getName());
EnumerationInetAddress ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
System.out.println("IP:"
+ ips.nextElement().getHostAddress());
}
}
} catch (Exception e) {
e.printStackTrace();
}
*/
5.点对点通信
/*
import java.io.*;
import java.net.*;
*/
public class %%6 extends Thread {
@Override
public void run() {
ServerSocket server = null;
try {
server = new ServerSocket(5000);
} catch (Exception e) {
System.out.println("不能监听:" + e.toString());
}
Socket socket = null;
try {
socket = server.accept();
BufferedReader req = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter os = new PrintWriter(socket.getOutputStream());
Debug.p(req.readLine());
os.println("Server");
os.flush();
os.close();
req.close();
} catch (IOException e) {
System.out.println("出错:" + e.toString());
} finally {
try {
if (socket != null) {
socket.close();
server.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Thread t = new %%6();
t.start();
String strIP = null;
try {
strIP = InetAddress.getLocalHost().getHostAddress().toString();
} catch (UnknownHostException e) {
e.printStackTrace();
}
Socket socket = null;
try {
socket = new Socket(strIP, 4000);
PrintWriter pw = new PrintWriter(socket.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
pw.println("Client");
pw.flush();
Debug.p(br.readLine());
pw.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
关于用java编写聊天室和java聊天室的设计与实现代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-01,除非注明,否则均为
原创文章,转载请注明出处。