⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainwin.java

📁 JAVA作的一个通讯工具 类似QQ 很好用 代码注释详细 用了WINSOCK套接字 多线程
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Vector;
import java.net.*;
import java.io.*; 

//以下是主程序
public class MainWin extends JFrame implements Runnable{
  JPanel contentPane;
///////////////////////////friendinfo
//以下是好友基本信息变量,比如呢称,ip地址等
 Vector friendnames=new Vector();
int friendnum;//friend number
   private String [] picsonline=new String []{
      "1.jpg","3.jpg","5.jpg","7.jpg"};
  private String [] picsoffline=new String[]{
   "2.jpg","4.jpg", "6.jpg","8.jpg"};
  Vector friendjicq=new Vector();
  Vector udpport=new Vector();
  Vector friendips=new Vector();
  Vector friendemail=new Vector();
  Vector friendinfo=new Vector();
  Vector picno=new Vector();
  Vector status=new Vector();
 // Vector pic=new Vector();
//以下是临时变量保存临时的好友
  Vector tempname=new Vector();
  Vector tempjicq=new Vector();
  Vector tempip=new Vector();
  Vector temppic=new Vector();
  Vector tempstatus=new Vector();
  Vector whoaddmesip=new Vector();//get whoaddme as friend
  Vector tempemail=new Vector();
  Vector tempinfo=new Vector();
   int index;//get list index
   int index3;//get firiend onlineinfo
   int index4;//get message from info
   boolean fromunknow=false;
//以下建立窗口类
FindFriend2 findf;
JDialog hello=new JDialog();
JDialog OneAddyou=new JDialog();
JDialog DirectAdd=new JDialog();
int tempgetjicq;//get the tempgetjicq
/////////////////////////////friend info
//以下是程序界面的变量
  ImageIcon icon1=new ImageIcon("cab_small.gif");
   ImageIcon icon6=new ImageIcon("sun_small.gif");
  JButton jButton1 = new JButton();
  JButton direct = new JButton();
  JLabel info=new JLabel();
  JDialog about=new JDialog();
  JDialog senddata=new JDialog();
   JDialog getdata=new JDialog();
  JButton ok=new JButton();
  JPopupMenu jPopupMenu1 = new JPopupMenu();
  JMenuItem sendmessage = new JMenuItem();
  JMenuItem getmessage = new JMenuItem();
  JMenuItem lookinfo = new JMenuItem();
  JMenuItem chatrecord = new JMenuItem();
  JLabel name = new JLabel();
  JTextField nametext = new JTextField();
  JLabel icq = new JLabel();
  JTextField icqno = new JTextField();
  JButton send = new JButton();
  JButton cancel = new JButton();
  JTextArea sendtext = new JTextArea();
  JList list;
  JLabel jLabel2 = new JLabel();
  JButton find = new JButton();
  FlowLayout flowLayout1 = new FlowLayout();
 //*************net
//以下是网络相关变量
  Socket socket;
  BufferedReader in;
   PrintWriter out;
  DatagramPacket sendPacket,receivePacket;
  DatagramSocket sendSocket,receiveSocket;
  int udpPORT=5001;//
  int sendPort=5000;//单机调试请改动这里,谢谢!!!!!
  String server;
  int serverport;
   byte array[]=new byte[512];
  Thread thread;
  int myjicq;
  String received;
////以下是程序界面的变量
  JLabel jLabel3 = new JLabel();
  JTextField getfromname = new JTextField();
  JLabel jLabel4 = new JLabel();
  JTextField getfromjicq = new JTextField();
  JTextArea getinfo = new JTextArea();
  JButton getok = new JButton();
  String theip;
  JButton update = new JButton();
  JMenuItem delfriend = new JMenuItem();
  JButton myinfo = new JButton();
  JButton online = new JButton();
  JLabel jLabel1 = new JLabel();
  JTextField helloname = new JTextField();
  JLabel jLabel5 = new JLabel();
  JTextField hellojicq = new JTextField();
  JLabel jLabel6 = new JLabel();
  JTextField helloemail = new JTextField();
  JLabel jLabel7 = new JLabel();
  JTextArea helloinfo = new JTextArea();
  JButton jButton3 = new JButton();
  JButton hellook = new JButton();
  JLabel jLabel8 = new JLabel();
  JLabel jLabel9 = new JLabel();
  JLabel jLabel10 = new JLabel();
  JLabel oneaddme = new JLabel();
  JButton addit = new JButton();
  JButton iknow = new JButton();
  JLabel jLabel11 = new JLabel();
  JLabel jLabel12 = new JLabel();
  JTextField friendid = new JTextField();
  JButton directaddok = new JButton();
//***************net

/*以下该函数连接服务器*/
  public void  ConnectServer(int myid){
   try{ socket=new Socket(InetAddress.getByName(server),serverport);

       in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
       out=new PrintWriter(new BufferedWriter(
                             new OutputStreamWriter(socket.getOutputStream())),true);

  //this is call my friend info
    //以下读取好友信息
          out.println("friend");
          out.println(myid);
        friendnum=Integer.parseInt(in.readLine());
         String friendname=" ";

        String friendjicqno,friendip,friendstatus,picinfo,email,infos;
        do{friendname=in.readLine();
            if(friendname.equals("over"))  break;
            friendnames.add(friendname);
            friendjicqno=in.readLine();
            friendjicq.add(new Integer(friendjicqno));
            friendip=in.readLine();
            friendips.add(friendip);
          friendstatus=in.readLine();
          status.add(friendstatus);
          picinfo=in.readLine();
          picno.add(new Integer(picinfo));
          email=in.readLine();
          friendemail.add(email);
          infos=in.readLine();
         friendinfo.add(infos);
          }while(!friendname.equals("over"));
  }catch(IOException e1){System.out.println("false");}
//以下在列表中显示好友
DefaultListModel mm=(DefaultListModel)list.getModel();
int  picid;
for(int p=0;p<friendnames.size();p++){
 picid=Integer.parseInt(picno.get(p).toString());
if(status.get(p).equals("1")){
     mm.addElement(new Object[]{friendnames.get(p),new ImageIcon(picsonline[picid])});}
else {
 mm.addElement(new Object[]{friendnames.get(p),new ImageIcon(picsoffline[picid])});}
     }//for
 }//connectto server
//*****************************
//以下函数无限监听好友的消息
public  void  run(){

while(true)
{
try{ for(int x=0;x<512;x++)array[x]=' ';
//创建数据报
	receivePacket=new DatagramPacket(array,array.length);
    receiveSocket.receive(receivePacket);
    byte[] data=receivePacket.getData();
           String infofromip=receivePacket.getAddress().getHostAddress().toString().trim();
           index3=0;
              received=new String(data,0,data.length);
               received.trim();
            //  System.out.println("get"+received.substring(0,6));
                 String tempstr;
             int tx;
           //friend online
    if(received.substring(0,6).equals("online")){//如果有好友上线就变彩色
           tempstr=received.substring(6).trim();
           // System.out.println("str"+tempstr);
           tempgetjicq=Integer.parseInt(tempstr);
         // System.out.println("id"+tempgetjicq);
          do{  tx= Integer.parseInt(friendjicq.get(index3).toString());
          //System.out.println("tx"+tx);
          if(tempgetjicq==tx)break;
          index3++;
          }while(index3<friendjicq.size());
          friendips.setElementAt(infofromip,index3);
         // status.setElementAt(,index3);
        //System.out.println(index3);
          DefaultListModel mm3=(DefaultListModel)list.getModel();
          int  picid=Integer.parseInt(picno.get(index3).toString());
               mm3.setElementAt(new Object[]{friendnames.get(index3),new ImageIcon(picsonline[picid])},index3);
              }//end online
    //friend offline
      else  if(received.substring(0,7).equals("offline")){//如果有好友下线就变灰色
      tempstr=received.substring(7).trim();
            System.out.println("str"+tempstr);
           tempgetjicq=Integer.parseInt(tempstr);
          System.out.println("id"+tempgetjicq);
          do{  tx= Integer.parseInt(friendjicq.get(index3).toString());
          System.out.println("tx"+tx);
          if(tempgetjicq==tx)break;
          index3++;
          }while(index3<friendjicq.size());
          infofromip="null";
          friendips.setElementAt(infofromip,index3);
         // status.setElementAt(,index3);
        System.out.println(index3);
          DefaultListModel mm3=(DefaultListModel)list.getModel();
          int  picid=Integer.parseInt(picno.get(index3).toString());
               mm3.setElementAt(new Object[]{friendnames.get(index3),new ImageIcon(picsoffline[picid])},index3);

      }//end friend offline
      //someone add me as friend
      else if(received.substring(0,9).equals("oneaddyou")){
              //如果有人加我为好有,选择加还是不
      tempstr=received.substring(9).trim();
            System.out.println("str"+tempstr);
           tempgetjicq=Integer.parseInt(tempstr);
          System.out.println("id"+tempgetjicq);
  //JOptionPane.showMessageDialog(this,"收到"+tempgetjicq+"addyou","ok",JOptionPane.INFORMATION_MESSAGE);
    oneaddme.setText(tempgetjicq+"把你加为好友");
    OneAddyou.setBounds(400,300,250,200);
     OneAddyou.show();


      } //endsomeone add me as friend
  else{//否则就显示收到消息
        index4=0;
        //  String infofromip=receivePacket.getAddress().getHostAddress().toString().trim();
         do{
            String friendip=friendips.get(index4).toString().trim();
            if (infofromip.equals(friendip)){
              String nameinfo=friendnames.get(index4).toString().trim();
   JOptionPane.showMessageDialog(this,"收到"+nameinfo+"的消息","ok",JOptionPane.INFORMATION_MESSAGE);
                fromunknow=false;
                break;   }//if
               index4++;
               if(index4>=friendnames.size()){
               fromunknow=true;//收到陌生人的消息
                JOptionPane.showMessageDialog(this,"收到陌生人"+infofromip+"的消息","ok",JOptionPane.INFORMATION_MESSAGE);
               }
              }while(index4<friendnames.size());//while
               System.out.println(index4);

       };

}catch(IOException  ex){ex.printStackTrace();}
}


}//run end
//**********************
//以下创建数据报
public void CreatUDP(){
try{
	
	/****************************************/
	sendSocket=new DatagramSocket();	
	System.out.println("zhy--"+sendSocket);
    receiveSocket=new DatagramSocket(udpPORT);
    System.out.println("zhy--"+receiveSocket.getInetAddress()+":"+receiveSocket.getPort()+","+receiveSocket.getLocalAddress()+receiveSocket.getLocalPort());
    /****************************************/
    
   // System.out.println("udp "+udpPORT+" ok");
}catch(SocketException se){se.printStackTrace();System.out.println("false udp!");}
}// creat udp end
//main ****************
  public MainWin(int s,String sername,int serport) {//主程序构造函数
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
       myjicq=s;
       server=sername;
       serverport=serport;
      jbInit();
      ConnectServer(myjicq);
       CreatUDP();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -