java--Network Programming

Source: Internet
Author: User

Every time a mention of the network will feel suddenly become tall, but network programming is really so mysterious? Let's go over and review it now.

Many people put the website programming equivalent to network programming, here to everyone correct, network programming! = website Programming, then what is network programming? Network programming is based on TCP and UDP programming, such as: Game class, social products. For TCP communication, it is a kind of a question and answer of a programming, and UDP is similar to a walkie-talkie communication, both have advantages, TCP relative UDP more secure, UDP relative TCP transmission faster, in game development often use UDP communication.

I am not very clear about the deep differences between them, if you know, please ask for a chat.

Here we take a practical example, a brief introduction to network programming:

A simple TCP-based network connection:

Server-side code:

public class Testseriver {    /** server-side     * @param args *     /public    static void Main (string[] args) {        try {
   //Our machine has 65,526 ports, when we set the port, try to choose the port after 124, because the port before 124 has been set by a specific program, for example: 80: Browser address port            ServerSocket SS = new ServerSocket (888);//888: Is the port number we set for the convenience of the client            Socket s = ss.accept ();//used to receive client connections, note that this method is blocking, that is, if there is no client connection , the program will stay in this position            System.out.println ("A client connect!"),        } catch (IOException e) {            e.printstacktrace ();        }    }}

Client code:

public class TestClient {    /** client     * @param args     *    /public static void main (string[] args) {        try {            Socket s = new socket ("127.0.0.1", 888);//Here is a point, "127.0.0.1": Make our host number, 888: is the port number we need to connect        } catch ( Unknownhostexception e) {            e.printstacktrace ();        } catch (IOException e) {            e.printstacktrace ();}}    }

Here is a point, this server-client connection, can only connect one client at a time, and no data communication, how to implement a multi-client connection, and complete the communication? Next, let's introduce a way:

Server-side code:

public class TestSeriver1 {    /** server-side     * @param args *     /public    static void Main (string[] args) {        try {
   serversocket ss = new ServerSocket (999);            while (true) {                Socket s = ss.accept ();                SYSTEM.OUT.PRINTLN ("A client connect!");                InputStream is = S.getinputstream ();                DataInputStream dis = new DataInputStream (is);                System.out.println (Dis.readutf ());//The readUTF () method Here is also blocking, in order to see this effect I added a pause in the client input place, already labeled            }        } catch ( IOException e) {            e.printstacktrace ();}}    }

Client code:

public class TestClient1 {    /** client     * @param args     *    /public static void main (string[] args) {        try {
   
    socket s = new Socket ("127.0.0.1", 999);            OutputStream OS = S.getoutputstream ();            DataOutputStream dos = new DataOutputStream (OS);            Thread.Sleep (3000);//set to pause for three seconds, about this I am on the thread of the blog has instructions            dos.writeutf ("Hello server!");        } catch ( Unknownhostexception e) {            e.printstacktrace ();        } catch (IOException e) {            e.printstacktrace ();        } catch (Interruptedexception e) {            //TODO auto-generated catch block            e.printstacktrace ();        }    }}
   

This one added a bit about Java--io flow knowledge, if you feel not understand, I in yesterday's blog on the Java--io stream did a detailed introduction, you can go to review, in order to learn the network.

java--Network Programming

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.