用java实现QQ群聊

用java实现QQ群聊,第1张

很久前写的代码了,以前能运行。电脑没装jdk,没试试,自亏李喊己扰滑试一下把。。

---------------服务器------------

import java.awt.*

import java.awt.event.*

import java.net.*

import java.io.*

import java.util.logging.Level

import java.util.logging.Logger

class myframe extends Frame implements ActionListener,WindowListener,Runnable,KeyListener

{

Thread mythread = new Thread(this)

Socket mysocket

DataInputStream in

DataOutputStream out

Label label_ip = new Label("IP")

Label label_port = new Label("Port")

TextField text_ip = new TextField("127.1.1.0",15)

TextField text_port = new TextField("8888",15)

Button button_connect = new Button("连接")

TextArea text_area_show = new TextArea()

TextField text_send = new TextField(45)

Button button_send = new Button("发送"销野)

myframe()

{

Panel panel1 = new Panel()

Panel panel2 = new Panel()

panel1.setLayout(new FlowLayout())

panel1.add(label_ip)

panel1.add(text_ip)

panel1.add(label_port)

panel1.add(text_port)

panel1.add(button_connect)

panel2.setLayout(new FlowLayout())

panel2.add(text_send)

panel2.add(button_send)

add(panel1,BorderLayout.NORTH)

add(text_area_show,BorderLayout.CENTER)

add(panel2,BorderLayout.SOUTH)

text_send.addKeyListener(this)

button_connect.addActionListener(this)

button_send.addActionListener(this)

addWindowListener(this)

this.setTitle("客户端")

setBounds(200,200,400,350)

setVisible(true)

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource() == button_connect)

{

try

{

String ip = null,port = null

ip = text_ip.getText()

port = text_port.getText()

mysocket = new Socket(ip, Integer.parseInt(port))

in = new DataInputStream(mysocket.getInputStream())

out = new DataOutputStream(mysocket.getOutputStream())

}

catch (UnknownHostException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex)

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex)

}

mythread.start()

}

if(e.getSource() == button_send)

{

if(mysocket.isConnected() == true)

{

String temp = null

temp = text_send.getText()

try

{

out.writeUTF(temp)

text_send.setText(null)

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex)

}

}

}

}

public void run()

{

while(true)

{

String temp = null

try

{

temp = in.readUTF()

}

catch (IOException ex)

{

text_area_show.append("服务器退出\n")

return

}

temp += "\n"

text_area_show.append(temp)

}

}

public void keyPressed(KeyEvent e)

{

if(e.getKeyCode() == KeyEvent.VK_ENTER)

{

String temp = null

temp = text_send.getText()

try

{

out.writeUTF(temp)

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex)

}

}

}

public void windowClosing(WindowEvent e)

{

System.exit(0)

}

public void windowOpened(WindowEvent e) {}

public void windowClosed(WindowEvent e) {}

public void windowIconified(WindowEvent e) {}

public void windowDeiconified(WindowEvent e) {}

public void windowActivated(WindowEvent e) {}

public void windowDeactivated(WindowEvent e) {}

public void keyTyped(KeyEvent e) {}

public void keyReleased(KeyEvent e) {}

}

public class mywindow

{

public static void main(String argv[])

{

myframe f = new myframe()

}

}

-----------客户端-----------

import java.net.*

import java.awt.*

import java.awt.event.*

import java.io.*

import java.util.logging.Level

import java.util.logging.Logger

import javax.swing.*

class SocketList

{

int length = 0

Socket socketlist[] = new Socket[10]

SocketList()

{

}

void add(Socket s)

{

if(length == 10)

{

return

}

socketlist[length] = s

length++

}

void Delete(Socket s)

{

int i = 0

for(i = 0i <lengthi++)

{

if(socketlist[i] == s)

{

break

}

}

for(int j = ij <lengthj++)

{

socketlist[j] = socketlist[j + 1]

}

length--

/*

for(int k = 0k <lengthk++)

{

if(socketlist[k].isBound() == true)

{

System.out.print("error")

}

}*/

}

}

class FrameThread extends Thread

{

myframe frame

FrameThread(myframe f)

{

frame = f

start()

}

public void run()

{

while(true)

{

InetAddress addr = null

Socket s = null

try

{

s = frame.server.accept()

addr = s.getInetAddress()

frame.socketlist.add(s)

for(int i = 0i <frame.socketlist.lengthi++)

{

if(frame.socketlist.socketlist[i] != s )

{

DataInputStream in = new DataInputStream(frame.socketlist.socketlist[i].getInputStream())

DataOutputStream out= new DataOutputStream(frame.socketlist.socketlist[i].getOutputStream())

out.writeUTF(addr + "连接到服务器")

}

}

}

catch (IOException ex)

{

Logger.getLogger(FrameThread.class.getName()).log(Level.SEVERE, null, ex)

}

frame.text_area_show.append(addr + "连接到服务器\n")

try

{

frame.list.add(InetAddress.getLocalHost().getHostAddress() + "-" + s.getPort())

}

catch (UnknownHostException ex)

{

Logger.getLogger(FrameThread.class.getName()).log(Level.SEVERE, null, ex)

}

try

{

new ClientThread(s, frame)

}

catch (IOException ex)

{

Logger.getLogger(FrameThread.class.getName()).log(Level.SEVERE, null, ex)

}

}

}

}

class ClientThread extends Thread

{

DataInputStream in = null

DataOutputStream out = null

Socket mysocket = new Socket()

myframe frame = null

InetAddress addr = null

ClientThread(Socket s,myframe f) throws IOException

{

mysocket = s

addr = mysocket.getLocalAddress()

frame = f

in = new DataInputStream(mysocket.getInputStream())

out = new DataOutputStream(mysocket.getOutputStream())

out.writeUTF("成功连接服务器")

start()

}

public void run()

{

while(true)

{

String temp = null

try

{

temp = in.readUTF()

for(int i = 0i <frame.socketlist.lengthi++)

{

DataInputStream in_temp = new DataInputStream(frame.socketlist.socketlist[i].getInputStream())

DataOutputStream out_temp = new DataOutputStream(frame.socketlist.socketlist[i].getOutputStream())

out_temp.writeUTF(addr + "说: " + temp)

}

frame.text_area_show.append(addr + "说: " + temp + "\n")

}

catch (IOException ex)

{

frame.socketlist.Delete(mysocket)

frame.text_area_show.append(addr + "退出服务器\n")

for(int i = 0i <frame.socketlist.lengthi++)

{

try

{

DataInputStream in_temp = new DataInputStream(frame.socketlist.socketlist[i].getInputStream())

}

catch (IOException ex1)

{

Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex1)

}

DataOutputStream out_temp = null

try

{

out_temp = new DataOutputStream(frame.socketlist.socketlist[i].getOutputStream())

}

catch (IOException ex1)

{

Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex1)

}

try

{

out_temp.writeUTF(addr + "退出服务器")

}

catch (IOException ex1)

{

Logger.getLogger(ClientThread.class.getName()).log(Level.SEVERE, null, ex1)

}

}

return

}

}

}

}

class myframe extends JFrame implements ActionListener,WindowListener,KeyListener

{

List list = new List(16)

TextArea text_area_show = new TextArea(15,40)

TextField text_send = new TextField(50)

Button button_send = new Button("发送")

ServerSocket server = null

FrameThread thread

SocketList socketlist = new SocketList()

myframe() throws IOException

{

Panel panel1 = new Panel()

panel1.setLayout(new FlowLayout())

panel1.add(text_area_show)

panel1.add(list)

list.addActionListener(this)

Panel panel2 = new Panel()

panel2.setLayout(new FlowLayout())

panel2.add(text_send)

panel2.add(button_send)

setLayout(new FlowLayout())

add(panel1)

add(panel2)

text_send.addKeyListener(this)

button_send.addActionListener(this)

addWindowListener(this)

server = new ServerSocket(8888)

thread = new FrameThread(this)

setTitle("服务器")

setBounds(200,200,450,330)

setVisible(true)

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource() == list)

{

text_area_show.append(list.getSelectedItem() + "\n")

}

if(e.getSource() == button_send)

{

for(int i = 0i <socketlist.lengthi++)

{

try

{

DataInputStream in = new DataInputStream(socketlist.socketlist[i].getInputStream())

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex)

}

DataOutputStream out = null

try

{

out = new DataOutputStream(socketlist.socketlist[i].getOutputStream())

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex)

}

try

{

out.writeUTF(text_send.getText())

text_send.setText(null)

//text_area_show.append(text_send.getText() + "\n")

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex)

}

}

}

}

public void windowClosing(WindowEvent e)

{

System.exit(0)

}

public void windowOpened(WindowEvent e) {}

public void windowClosed(WindowEvent e) {}

public void windowIconified(WindowEvent e) {}

public void windowDeiconified(WindowEvent e) {}

public void windowActivated(WindowEvent e) {}

public void windowDeactivated(WindowEvent e) {}

public void keyTyped(KeyEvent e) {}

public void keyPressed(KeyEvent e)

{

if(e.getKeyCode() == KeyEvent.VK_ENTER)

{

for(int i = 0i <socketlist.lengthi++)

{

try

{

DataInputStream in = new DataInputStream(socketlist.socketlist[i].getInputStream())

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex)

}

DataOutputStream out = null

try

{

out = new DataOutputStream(socketlist.socketlist[i].getOutputStream())

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex)

}

try

{

out.writeUTF(text_send.getText())

text_area_show.append(text_send.getText() + "\n")

}

catch (IOException ex)

{

Logger.getLogger(myframe.class.getName()).log(Level.SEVERE, null, ex)

}

}

}

}

public void keyReleased(KeyEvent e) {}

}

public class mywindow

{

public static void main(String argv[]) throws IOException

{

myframe f = new myframe()

}

}

公安局加入qq群的代码如下:

1、QQ群分享官网地址为“http://qun.qq.com/join.html”,复制到浏览器打开即可进入QQ群一键建群分享首页。

2、进入后即可看到底边左侧有Q群选择,右边是代码获取。

3、网页代码获取,iphone和安卓代码获取以及二维码图片下载等。

4、选择自己需要的方式,将代码复制,放在到网页需要显示的地方即可

5、比如小编这里获取网页烂梁代码,将代码放置到我需要显示的地方。

6、这样,网站访问用户可以点击加入Q群即可加饥梁运入群内了。很方便的一键加群方式。

QQ群成员标签怎么设置:

1、开启群成员标签的功能。请群主或管理员先进入群设置中将“在群成员管理页展示群成员标签”勾选上。

2、设置群成员标签。进入群设置-成员-点击“标签”右边的编辑按钮-输入渣改标签-点击“添加”即可。标签名称长度不能超过8个汉字,标签的总个数不能超过20个。

3、进入群设置-成员-将鼠标放在某个群成员的标签处,会出现空白框-选择您要设置的标签即可。


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

原文地址:https://54852.com/yw/12387265.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存