"Java Programming" socket programming UDP analog Simple chat function __ Programming

Source: Internet
Author: User
Tags clear screen event listener

Simple interface:

Train of thought:
1. First create the interface in the previous illustration
2. Add related event listener Events
3. Use multithreading to design the sender and receiver
4. Create thread initiation threads in the Send button and the receiving end respectively.
5. The last Datagramsocket service (guaranteed uniqueness) to be sent and received separately in the main program

Import java.net.*;
Import java.io.*;
Import java.awt.*;
Import java.awt.event.*; Class Chatdemo {public static void main (string[] args) throws Socketexception,unknownhostexception {Dat
        Agramsocket send=new Datagramsocket ();
        Datagramsocket receive=new Datagramsocket (10001);
    New Framedemo (send,receive);
    } class Framedemo {private Button clearbut;
    Private Datagramsocket send;
    Private Datagramsocket receive;
    Private Frame F;
    Private TextArea Sendarea;
    Private TextArea Receivearea;

    Private Button but;
        Framedemo (Datagramsocket Send,datagramsocket receive) {this.send=send;
        this.receive=receive;
    Init ();
        public void init () {f=new Frame ("chat panel");
        F.setbounds (600,500,500,500);
        F.setlayout (New FlowLayout ());
        Sendarea=new TextArea ();     
        Receivearea=new TextArea ();
        F.add (Receivearea);
        F.add (Sendarea); Clearbut=new BuTton ("clear screen");
        But=new button ("send");
        F.add (clearbut);
        F.add (But);
        MyEvent ();     
    F.setvisible (TRUE); The public void MyEvent () {F.addwindowlistener () {The new Windowadapter () {public void window
            Closing (WindowEvent e) {system.exit (0);
        }
        });               
                But.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e) {
                New Thread (New Send (Send,sendarea)). Start ();
            New Thread (new receive (Receive,receivearea)). Start ();
        }
        });
            Clearbut.addactionlistener (new ActionListener () {public void actionperformed (ActionEvent e)
            {Receivearea.settext ("");
    }
        });
    }///Receive class receive implements Runnable {private Datagramsocket ds;
    Private TextArea Receivearea; Receive (DatagramsOcket Ds,textarea Receivearea) {this.ds=ds;
    This.receivearea=receivearea;
        public void Run () {//Create packet byte[] Buf=new byte[1024];       
            Datagrampacket dp=new Datagrampacket (buf,buf.length);         
            try {ds.receive (DP);
            catch (IOException e) {throw new RuntimeException ("Receive-side error");
            String ip=dp.getaddress (). gethostaddress ();
            String Data=new string (Dp.getdata (), 0,dp.getlength ());
    Receivearea.append (ip+ "\ r \ n" +data+ "\ r \ n");
    }//Send End Class Send implements Runnable {private TextArea sendarea;
    Private Datagramsocket DS;
        Send (Datagramsocket ds,textarea sendarea) {this.ds=ds;
    This.sendarea=sendarea;
        public void Run () {String senddata=sendarea.gettext ();   
        Sendarea.settext (""); Create a packet byte[]Buf=senddata.getbytes ();
        Datagrampacket Dp=null;                 
        try {dp=new datagrampacket (buf,buf.length,inetaddress.getbyname ("127.0.0.1"), 10001);
        catch (Unknownhostexception e) {throw new RuntimeException ("Send-side error");
            try {if (buf.length==0) return;                    
        Ds.send (DP);
        catch (IOException e) {throw new RuntimeException ("Send-side error");
 }
    }

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.