想求一个java聊天小程序,使用图形用户界面和socket通讯 功能:可以实现两个人私聊功能和多人聊天功能。

想求一个java聊天小程序,使用图形用户界面和socket通讯 功能:可以实现两个人私聊功能和多人聊天功能。,第1张

我只介绍思路:

使用websocket创建ws服务器,nat123解析一个地址,分配一个端口羡毕搏给他用,发布到外网。

客户端拨通地址 登陆 接入websocket服务,心跳机制要做好,上下线掉线更新列表,注册存储用户信息,更新用户列表。

客户端可以发起群聊、私聊、收发文件、分享,兄祥甚至发送服务器消息,然后反馈消息,响应远程数差服务事件。

这就是一个聊天软件所做的事情,我介绍websocket可以响应多平台,多端通信。

给你一个简单的实现吧拍饥,注意一戚贺慎定要高敬先运行MyServer.java

//MyCilent.java

import java.io.*

import java.net.*

import javax.swing.*

import java.awt.*

import java.awt.event.*

public class MyClient extends JFrame implements ActionListener{

JTextField tf

JTextArea tx

JButton bt

PrintWriter out

public MyClient(){

tf=new JTextField(20)

tx=new JTextArea()

tx.setLineWrap(true)

tx.setWrapStyleWord(true)

JPanel pan=new JPanel()

JScrollPane jsp=new JScrollPane(tx)

add(jsp,"Center")

bt=new JButton("SEND")

bt.addActionListener(this)

pan.add(tf)

pan.add(bt)

add(pan,"South")

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0)

}

})

setTitle("THE CLIENT")

setSize(400,300)

setVisible(true)

try{

Socket socket=new Socket("127.0.0.1",1680)

out=new PrintWriter(socket.getOutputStream(),true)

InputStreamReader in = new InputStreamReader(socket.getInputStream())

BufferedReader sin=new BufferedReader(in)

String s

while(true){

s=sin.readLine()

tx.append("#Server Said#: "+s+"\n")

}

}catch(Exception e){

e.printStackTrace()

}

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==bt){

tx.append("@Client Said@: "+tf.getText()+"\n")

out.println(tf.getText())

tf.setText("")

}

}

public static void main(String[] args){

MyClient mct = new MyClient()

}

}

//MyServer.java

import java.io.*

import java.net.*

import javax.swing.*

import java.awt.*

import java.awt.event.*

public class MyServer extends JFrame implements ActionListener{

JTextField tf

JTextArea tx

JButton bt

JScrollPane jsp

JPanel pan

PrintWriter out

public MyServer(){

tx=new JTextArea()

tx.setLineWrap(true)

tx.setWrapStyleWord(true)

jsp=new JScrollPane(tx)

tf=new JTextField(20)

bt=new JButton("SEND")

bt.addActionListener(this)

pan=new JPanel()

pan.add(tf)

pan.add(bt)

add(pan,"South")

add(jsp,"Center")

this.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0)

}

})

setTitle("THE SERVER")

setSize(400,300)

setVisible(true)

try{

ServerSocket server = new ServerSocket(1680)

Socket socket = server.accept()

InputStreamReader in = new InputStreamReader(socket.getInputStream())

BufferedReader sin=new BufferedReader(in)

out=new PrintWriter(socket.getOutputStream(),true)

while(true){

String s=sin.readLine()

tx.append("@Client Said@: "+s+"\n")

}

}catch(Exception e){

e.printStackTrace()

}

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==bt){

String st = tf.getText()

tx.append("#Server Said#: "+st+"\n")

out.println(st)

tf.setText("")

}

}

public static void main(String[] args){

MyServer msr = new MyServer()

}

}

import java.awt.*

import javax.swing.*

import java.awt.event.*

import java.net.*

import java.io.*

class MyChatter extends JFrame {

private JLabel jLName = new JLabel()

private JTextField jTName = new JTextField()

private JLabel jLSendMss = new JLabel()

private JLabel jLReceiveMss = new JLabel()

private JButton jBSend = new JButton()

private JButton jBClear = new JButton()

private JScrollPane jScrollPane1 = new JScrollPane()

private JTextPane jTSendMss = new JTextPane()

private JScrollPane jScrollPane2 = new JScrollPane()

private JTextPane jTReceiveMss = new JTextPane()

//窗口适配器

private MyWindowAdapter mwa=null

//通讯用成员变量

private byte[] receiveBuf=new byte[1000]

private byte[] sendBuf=null

private DatagramSocket datagramServer=null

private DatagramSocket datagramClient=null

private DatagramPacket receivePacket=null

private DatagramPacket sendPacket=null

private static final int PORT=5000

private InetAddress inetAddr=null

private Server server=null

private String machineName=null

private String sendMss=null

class Server extends Thread{

public Server(){

start()

}

public void run(){

while(true){

try{

//System.out.println(datagramServer)

datagramServer.receive(receivePacket)

displayMss()

}

catch(Exception e){

e.printStackTrace()

}

}

}

}

class MyWindowAdapter extends WindowAdapter{

public void windowClosing(WindowEvent e) {

close()

System.exit(0)

}

}

class MyKeyAdapter extends KeyAdapter{

public void keyPressed(KeyEvent e){

//System.out.println(e.getKeyCode())

if(e.isShiftDown()&&e.getKeyCode()==10){

sendReady()

}

}

}

private void sendReady(){

if(!checkMachineName()){

return

}

if(!checkSendMss()){

return

}

if(sendMss()){

JOptionPane.showMessageDialog(null,"发送消息成功!","提示",JOptionPane.ERROR_MESSAGE)

}

else{

JOptionPane.showMessageDialog(null,"发送消息失败!","提示",JOptionPane.ERROR_MESSAGE)

}

}

private boolean sendMss(){

try{

sendPacket=toDatagram(sendMss,inetAddr,PORT)

datagramClient.send(sendPacket)

}

catch(Exception e){

return false

}

return true

}

private DatagramPacket toDatagram(String s,InetAddress destIA,int destPort){

//sendBuf=new byte[s.length()+1]

//s.getBytes(0,s.length(),sendBuf,0)

try{

sendBuf=s.getBytes("GB2312")

}

catch(Exception e){

}

return new DatagramPacket(sendBuf,sendBuf.length,destIA,destPort)

}

private boolean checkMachineName(){

machineName=jTName.getText()

if(machineName.length()==0){

JOptionPane.showMessageDialog(null,"请输入机器名!","警告",JOptionPane.ERROR_MESSAGE)

return false

}

try{

inetAddr=InetAddress.getByName(machineName)

}

catch(UnknownHostException e){

JOptionPane.showMessageDialog(null,"不能识别的机器名或者机器不存在!","警告",JOptionPane.ERROR_MESSAGE)

return false

}

return true

}

private boolean checkSendMss(){

sendMss=jTSendMss.getText()

if(sendMss.length()==0){

JOptionPane.showMessageDialog(null,"要发送的消息不能为空!","警告",JOptionPane.ERROR_MESSAGE)

return false

}

return true

}

private void displayMss(){

String mss=null

String tmp=jTReceiveMss.getText()

try{

mss=new String(receivePacket.getData(),0,receivePacket.getLength(),"GB2312")

}

catch(Exception e){

}

jTReceiveMss.setText(tmp+mss+"\n")

}

private void close(){

}

private void init() throws IOException{

mwa=new MyWindowAdapter()

datagramServer=new DatagramSocket(PORT)

datagramClient=new DatagramSocket()

receivePacket=new DatagramPacket(receiveBuf,receiveBuf.length)

jTSendMss.addKeyListener(new MyKeyAdapter())

server=new Server()

}

public MyChatter() throws HeadlessException {

try {

jbInit()

init()

}

catch(Exception e) {

e.printStackTrace()

}

}

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

MyChatter myChatter1 = new MyChatter()

myChatter1.addWindowListener(myChatter1.mwa)

myChatter1.setSize(400,410)

myChatter1.setVisible(true)

}

private void jbInit() throws Exception {

jLName.setText("机器名或IP地址:")

jLName.setBounds(new Rectangle(15, 7, 108, 18))

this.getContentPane().setLayout(null)

jTName.setBounds(new Rectangle(13, 27, 368, 22))

jLSendMss.setText("发送消息:")

jLSendMss.setBounds(new Rectangle(12, 54, 92, 20))

jLReceiveMss.setText("接收消息:")

jLReceiveMss.setBounds(new Rectangle(12, 208, 86, 19))

jBSend.setBounds(new Rectangle(97, 178, 68, 19))

jBSend.setText("发送")

jBSend.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jBSend_actionPerformed(e)

}

})

jBClear.setText("清空")

jBClear.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(ActionEvent e) {

jBClear_actionPerformed(e)

}

})

jBClear.setBounds(new Rectangle(224, 179, 68, 19))

this.setResizable(false)

this.setTitle("张辉的聊天程序")

jScrollPane1.setBounds(new Rectangle(13, 77, 368, 90))

jScrollPane2.setBounds(new Rectangle(13, 228, 368, 145))

jTReceiveMss.setEditable(false)

this.getContentPane().add(jLName, null)

this.getContentPane().add(jTName, null)

this.getContentPane().add(jLSendMss, null)

this.getContentPane().add(jLReceiveMss, null)

this.getContentPane().add(jBSend, null)

this.getContentPane().add(jBClear, null)

this.getContentPane().add(jScrollPane1, null)

this.getContentPane().add(jScrollPane2, null)

jScrollPane2.getViewport().add(jTReceiveMss, null)

jScrollPane1.getViewport().add(jTSendMss, null)

}

void jBClear_actionPerformed(ActionEvent e) {

jTSendMss.setText("")

}

void jBSend_actionPerformed(ActionEvent e) {

sendReady()

}

}

绝对能用,OK!给分!!!


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

原文地址: http://outofmemory.cn/yw/12310644.html

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

发表评论

登录后才能评论

评论列表(0条)

保存