
* 基于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()
}
}
}
要开发移动聊天应用程序,您需要结合使用软件工具和编程语言。 以下是移动应用程序开发中最常用的工具和技术的列表:集成开发环境 (IDE):IDE 是一种软件应用程序,可为编码、调试和测试提供综合环境。 流行的 IDE 示例包括 Android Studio(用于 Android 应用程序开发)、Xcode(用于 iOS 应用程序开发)和 Visual Studio Code(用于跨平台应用程序开发)。
编程语言:移动应用程序开发最常用的编程语言是 Java(用于 Android 应用程序开发)、Swift(用于 iOS 应用程序开发)和 React Native(用于跨平台应用程序开发)。
后端服务:为了支持应用程序的聊天功能,您需要一个后端服务来处理消息传递和存储。 您可以使用 Node.js 或 Ruby on Rails 等服务器端编程语言构建自定义后端,或使用 Firebase 或 AWS 等基于云的后端即服务 (BaaS) 平台。
数据库:要存储用户和聊天数据,您将需要一个数据库。 移动应用程序开发的流行数据库选项包括 SQLite(用于简单应用程序)、MySQL(用于更复杂的应用程序)和 NoSQL 数据库,例如 MongoDB 或 Cassandra。
设计工具:要为您的应用程序创建一个有吸引力且用户友好的界面,您将需要设计工具,例如 Sketch、Adobe Photoshop 或 Figma。
还有许多其他工具和技术可用于支持移动应用程序开发,您需要的具体工具和技术将取决于您应用程序的具体要求。 最好研究和试验不同的工具,以找到最适合您需要的工具。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)