简单聊天程序(基于UDP协议)
对等端甲
package cn.point; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class project extends JFrame implements Runnable,ActionListener{ JTextField out_message=new JTextField(6); JTextField one=new JTextField(3); JTextField two=new JTextField(3); JTextField three=new JTextField(3); JButton submit=new JButton("submit"); JTextArea in_message=new JTextArea(); JButton send=new JButton("发送"); project(){ this.setTitle("甲方"); this.setSize(230,125); this.setVisible(true); send.addActionListener(this); JPanel pSouth=new JPanel(); pSouth.add(out_message); pSouth.add(send); this.add(pSouth,"South"); JPanel pCenter=new JPanel(); pCenter.add(one); pCenter.add(two); pCenter.add(three); pCenter.add(submit); this.add(pCenter,"Center"); this.add(new JScrollPane(in_message),"North"); this.validate(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Thread thread=new Thread(this); thread.start(); } public float powfunction(int data,int e,int n) { int temp=data; for(int k=1;k<e;k++) { data=temp*data; data=data%n; } return data; } public void actionPerformed(ActionEvent event){ //监听器,监听事件,主要用于处理数据发送部分 char getdata[]=this.out_message.getText().toCharArray(); int p=17,q=7; int n=119,o=96; int d=77,e=5; //cout<<"请输入要加密的信息(*结束,无标点):"<<endl; int m1[] = new int[16]; float m2[] = new float[16]; int j; //char data; for(j=0;j<500;j++) { //cin>>data; m1[j]=getdata[j]-(int)'a'; if(getdata[j]=='*') break; m2[j]=powfunction(m1[j],e,n); //通过调用这个函数防止使用pow数学库函数产生的溢出,加密过程 } //cout<<"密文为:"<<endl; int k; String mess = new String(); for(k=0;k<j;k++) mess+=m2[k]; System.out.println("密文为:"+mess); //cout<<m2[k]<<" "; //cout<<endl; //cout<<"明文为:"<<endl; String mess1 = new String(); for(k=0;k<j;k++) { m2[k]=powfunction((int)m2[k],d,n); mess1+=(char)(m2[k]+(int)'a'); } System.out.println("明文为:"+mess1); //byte buffer[]=out_message.getText().trim().getBytes(); byte buffer[]=mess.getBytes(); try{ InetAddress address=InetAddress.getByName("127.0.0.1"); DatagramPacket data_pack=new DatagramPacket(buffer,buffer.length,address,666); DatagramSocket mail_data=new DatagramSocket(); mail_data.send(data_pack); }catch(Exception e1){} this.out_message.setText(""); } public void run(){ //线程,主要用于接收发送过来的数据 DatagramPacket pack=null; DatagramSocket mail_data=null; byte data[]=new byte[8192]; try{ pack=new DatagramPacket(data,data.length); mail_data=new DatagramSocket(888); }catch(Exception e){} while(true){ if(mail_data==null)break; else try{ mail_data.receive(pack); String message=new String(pack.getData(),0,pack.getLength()); in_message.append("收到数据:"+message+"\n"); }catch(Exception e){} } } } package cn.point; public class main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub project myproject=new project(); } }
对等端乙:
package cn.point; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; class project extends JFrame implements Runnable,ActionListener{ JTextField out_message=new JTextField(6); JTextArea in_message=new JTextArea(); JButton send=new JButton("发送"); project(){ this.setTitle("乙方"); this.setSize(230,150); this.setVisible(true); send.addActionListener(this); JPanel pSouth=new JPanel(); pSouth.add(out_message); pSouth.add(send); this.add(pSouth,"South"); this.add(new JScrollPane(in_message),"Center"); this.validate(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Thread thread=new Thread(this); thread.start(); } public void actionPerformed(ActionEvent event){ byte buffer[]=out_message.getText().trim().getBytes(); try{ InetAddress address=InetAddress.getByName("127.0.0.1"); DatagramPacket data_pack=new DatagramPacket(buffer,buffer.length,address,888); DatagramSocket mail_data=new DatagramSocket(); mail_data.send(data_pack); }catch(Exception e){} this.out_message.setText(""); } public void run(){ DatagramPacket pack=null; DatagramSocket mail_data=null; byte data[]=new byte[8192]; try{ pack=new DatagramPacket(data,data.length); mail_data=new DatagramSocket(666); }catch(Exception e){} while(true){ if(mail_data==null)break; else try{ mail_data.receive(pack); String message=new String(pack.getData(),0,pack.getLength()); in_message.append("收到数据:"+message+"\n"); }catch(Exception e){} } } } package cn.point; public class main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub project myproject=new project(); } }
实验结果: