Java Socket Server Client

Source: Internet
Author: User

Server

Package Com.witwicky.socket.basicsocket;import Java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;import Java.net.serversocket;import Java.net.socket;public class Server {public static void main (        String[] (args) {int port = 43523;        ServerSocket serversocket = null;            try {System.out.println ("===\nstart recive message ...");            ServerSocket = new ServerSocket (port);            Socket socket = serversocket.accept ();            InputStream InputStream = Socket.getinputstream ();            StringBuffer sb = new StringBuffer ();            byte[] bytes = new byte[1024];            int Len;                while (len = inputstream.read (bytes))! =-1) {System.out.println ("Length:" + len);            Sb.append (New String (bytes, 0, Len, "UTF-8"));            } System.out.println (Sb.tostring ());            System.out.println ("End recive message......\n==="); String returncontent = "recive oveR. ";            OutputStream outputstream = Socket.getoutputstream ();            Outputstream.write (Returncontent.getbytes ("UTF-8"));            Outputstream.close ();            Inputstream.close ();            Socket.close ();        Serversocket.close ();        } catch (IOException e) {e.printstacktrace (); }    }}
Connected to the target VM, address: ' 127.0.0.1:61818 ', Transport: ' Socket ' ===start recive message......length:180 because the C programme Language is very famous for using it as the first demo program, so later programmers continue this habit when they learn to program or perform device debugging. End Recive message......===disconnected from the target VM, address: ' 127.0.0.1:61818 ', Transport: ' Socket ' Process finish Ed with exit code 0

Client

Package Com.witwicky.socket.basicsocket;import Java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;import Java.net.socket;public class Client {public static void main (string[] args) {Strin        G host = "127.0.0.1";        int port = 43523;            try {System.out.println ("= = =");            Socket socket = new socket (host, port);            OutputStream outputstream = Socket.getoutputstream (); String content = "because it was used as the first demo program in the C programme Language, it was so famous that later programmers continued the habit when they learned to program or debug the device."            ";            Outputstream.write (Content.getbytes ());            Socket.shutdownoutput ();            int Len;            byte[] bytes = new byte[1024];            InputStream InputStream = Socket.getinputstream ();            StringBuffer sb = new StringBuffer ();            while (len = inputstream.read (bytes))! =-1) {Sb.append (New String (bytes, 0, Len, "UTF-8")); } System.out.println ("Service return Content:" + SB. toString ());            System.out.println ("= = =");            Inputstream.close ();            Outputstream.close ();        Socket.close ();        } catch (IOException e) {e.printstacktrace (); }    }}
Connected to the target VM, address: ' 127.0.0.1:61828 ', Transport: ' Socket ' ===service return content:recive Over.===disco Nnected from the target VM, address: ' 127.0.0.1:61828 ', Transport: ' Sockets ' Process finished with exit code 0

Java Socket Server Client

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