java--Network Programming 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: First, TCP-based Simple 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 (); }} to illustrate here that the server--Client connection, only one client can be connected at a time, and there is no data communication, how to implement multi-client connection and complete 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 (); }}} which adds a bit about Java--io the knowledge of the stream, if you feel that you do not understand, I in yesterday's blog on java--IO Stream do 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.
Reprinted from: Http://www.cnblogs.com/AndroidJotting/p/3939144.html
java--Network Programming