Java Socket Programming Experiment Summary

Source: Internet
Author: User


1. Using the Socket class

if (socket = = NULL)
Socket = new Socket (address, 2333);
Do not use ports prior to 1024

2.socket communicates through OutputStream and InputStream operations

  if NULL )        new  PrintWriter (Socket.getoutputstream ());  Writer.println (tosend);  Writer.flush ();//Be sure to remember flush refresh
New BufferedReader (new     InputStreamReader (Socket.getinputstream ()));                      while (! socket.isclosed ()) {                        sleep (+);                         = reader.readline ();                         if (line!=null) {                            + "\ n");}                    }                    Reader.close ();    

3. Modify JFrame default Close window event

 Addwindowlistener (new   Windowadapter () {  public  void   windowclosing (WindowEvent e) { super  .windowclosing (e);  try   { if  (Socket! = null  ) {so                    Cket.close ();                    }}  catch   (Exception ex) {                Ex.printstacktrace ();            } system.exit ( 0); }        });


1. Using ServerSocket

  New ServerSocket (2333);

2. Broadcast implementation

Socketlist =NewArraylist<socket>(); Try{ServerSocket=NewServerSocket (2333);  while(true) {Socket Socket=serversocket.accept (); if(!socketlist.contains (socket))                    {Socketlist.add (socket); Newsocketthread (socket); }            }        } Catch(Exception e) {e.printstacktrace (); }

3. Using multithreading

BufferedReader reader =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ()));  while(socket.isclosed () = =false) {Sleep (100); String Line=Reader.readline (); if(Line! =NULL) {                         for(inti = 0; I < socketlist.size (); i++) {Socket iter=(Socket) socketlist.get (i); PrintWriter writer=NewPrintWriter (Iter.getoutputstream ());                            System.out.println (line);                            Writer.println (line);                        Writer.flush (); }}} socketlist.remove (socket);

When I first run the client on my roommate's computer, I can't get access to my server, and I run the firewall after it has been hit by a jar package and then successfully completed the communication!

Java Socket Programming Experiment Summary

Related Article

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: 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.