当前位置:编程学习 > JAVA >>

Java聊天室服务器端和客户端的完整代码

根据JAVA的网络相关知识和多线程的知识,要简单的 能够实现一对一通讯 务必能运行!加注释。hey_tear@yeah.net(326290171) 这是邮箱地址 谢谢。
答案:

下面有个IP你把它设成对方的ip地址就可以了,在他的电脑上也把ip设为你自己的就可以实现QQ聊天了,就是界面有点丑!!希望能帮助到你!你可以查考下!!

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.TextArea;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.IOException;

import java.net.DatagramPacket;

import java.net.DatagramSocket;

import java.net.InetAddress;

import java.net.SocketException;

import java.net.UnknownHostException;

 

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JScrollBar;

import javax.swing.JScrollPane;

 

 

public class UDPChat extends JFrame implements ActionListener{

 

    public TextArea textmessage = null;

    public TextArea sendtext = null;

    public DatagramSocket socket;

    public JScrollBar vsBar;

    public UDPChat()

    {

       super();

       setTitle("小型聊天程序");

       setBounds(200,150,350,280);

       JPanel panel1 = new JPanel();

       JPanel panel2 = new JPanel();

       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       setBackground(Color.BLACK);

       textmessage = new TextArea();

       sendtext = new TextArea();

      

       textmessage.setEditable(false);

       textmessage.setColumns(35);

       textmessage.setRows(10);

    //  textmessage.setLineWrap(true);

      

       sendtext.setColumns(35);

       sendtext.setRows(2);

 

       JButton buttonOK = new JButton("确定");

       JButton buttonclean = new JButton("清空");

       JButton buttonquit = new JButton("退出");

       GridLayout grid =  new GridLayout(3,1);

       grid.setHgap(10);

       grid.setVgap(10);

       panel1.setLayout(grid);

    //  panel1.setLayout(new GridLayout(3,1));

 

       panel1.add(buttonOK);

       panel1.add(buttonclean);

       panel1.add(buttonquit);

 

       panel2.setLayout(new FlowLayout());

       panel2.add(textmessage);

       panel2.add(sendtext);

       getContentPane().add(panel1,BorderLayout.EAST);

       getContentPane().add(panel2,BorderLayout.CENTER);

       setVisible(true);

       buttonOK.addActionListener(this);

       buttonclean.addActionListener(this);

       buttonquit.addActionListener(this);

       server();

    }

    public void server()

    {

       try{

           socket = new DatagramSocket(9527);

           byte[] buf = new byte[1024];

           final DatagramPacket dp1 = new DatagramPacket(buf,buf.length);

           Runnable run = new Runnable(){

              public void run()

              {

                  while(true)

                  {

                     try{

                         Thread.sleep(100);

                         socket.receive(dp1);

                         int len = dp1.getLength();

                         String message = new String(dp1.getData(),0,len);

                         String ip = dp1.getAddress().getHostAddress();

                         System.out.println(ip);

                         if(!InetAddress.getLocalHost().getHostAddress().equals(ip))

                            t

上一个:学java更好还是学c#更好?
下一个:java课程设计---计算器 要求如下:

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,