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 classTestseriver {/**Server-side *@paramargs*/     Public Static voidMain (string[] args) {Try {            //One of our machines 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 portServerSocket SS =NewServerSocket (888);//Here's 888: The port number that we set for the convenience of the client connectionSocket s = ss.accept ();//to receive the client's connection, note that this method is blocking, that is, if there is no client connection, the program will stay in this locationSYSTEM.OUT.PRINTLN ("A client connect!"); } Catch(IOException e) {e.printstacktrace (); }    }}

  Client Code:

 Public classTestClient {/**Client *@paramargs*/     Public Static voidMain (string[] args) {Try{Socket s=NewSocket ("127.0.0.1", 888);//Here's 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 classTestSeriver1 {/**Server-side *@paramargs*/     Public Static voidMain (string[] args) {Try{ServerSocket ss=NewServerSocket (999);  while(true) {Socket s=ss.accept (); System.out.println ("A Client connect!"); InputStream is=S.getinputstream (); DataInputStream Dis=NewDataInputStream (IS); System.out.println (Dis.readutf ());//here The readUTF () method is also blocking, in order to see this effect I added a pause in the client input place, has been labeled            }        } Catch(IOException e) {e.printstacktrace (); }    }}

  Client Code:

 Public classTestClient1 {/**Client *@paramargs*/     Public Static voidMain (string[] args) {Try{Socket s=NewSocket ("127.0.0.1", 999); OutputStream OS=S.getoutputstream (); DataOutputStream dos=Newdataoutputstream (OS); Thread.Sleep (3000);//set a pause for three seconds, about this I'm on the thread's blog that explainsDos.writeutf ("Hello server!"); } Catch(unknownhostexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }Catch(interruptedexception e) {//TODO auto-generated Catch blockE.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.

Statement: If the above content is inappropriate, please leave a message, thank you.

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.