Two small exercises about TCP _ The first blog ~, First blog of tcp exercises

Source: Internet
Author: User

Two small exercises about TCP _ The first blog ~, First blog of tcp exercises

First, a local client sends a request, and the server receives the simple code of the request.

1 package com. TCP. java; 2 3 import java. io. IOException; 4 import java. io. inputStream; 5 import java. io. outputStream; 6 import java.net. inetAddress; 7 import java.net. serverSocket; 8 import java.net. socket; 9 import org. junit. test; 10 11 // the client sends a message to the server. The server outputs this information to 12 public class TestTCP {13 // client 14 @ Test15 public void client () {16 Socket socket = null; 17 OutputStream OS = null; 18 try {19 // 1. create a Socket object, specify the IP address of the server through the constructor, and the port number of the receiving program 20 socket = new Socket (InetAddress. getByName ("192.168.1.101"), 9090); 21 // 2. getOutputStream (): sends data. The method returns the OutputStream object 22 OS = socket. getOutputStream (); 23 // 3. specific output process 24 OS. write ("I Am a client ". getBytes (); 25} catch (IO Exception e) {26 e. printStackTrace (); 27} finally {28 // close the corresponding stream and Socket object 29 try {30 if (OS! = Null) {31 OS. close (); 32} 33 if (socket! = Null) {34 socket. close (); 35} 36} catch (IOException e) {37 e. printStackTrace (); 38} 39} 40} 41 42 // server 43 @ Test44 public void server () {45 ServerSocket ss = null; 46 Socket s = null; 47 InputStream is = null; 48 try {49 // 1. create a ServerSocket object and specify its own port number through the constructor: 50 ss = new ServerSocket (9090); 51 // 2. call its accept () method and return a Socket object 52 s = ss. accept (); 53 // 3. call the getInputStream () method of the Socket object to obtain an input stream sent from the client 54 is = S. getInputStream (); 55 // 4. operate the obtained stream 56 byte [] B = new byte [20]; 57 int length; 58 while (length = is. read (B ))! =-1) {59 String str = new String (B, 0, length); 60 System. out. println (str); 61} 62 System. out. println ("received from" + s. getInetAddress (). getHostName () + "connection"); 63} catch (IOException e) {64 e. printStackTrace (); 65} finally {66 // close the corresponding stream, Socket, ServerSocket object 67 if (is! = Null) {68 try {69 is. close (); 70} catch (IOException e) {71 e. printStackTrace (); 72} 73} 74 if (s! = Null) {75 try {76 s. close (); 77} catch (IOException e) {78 e. printStackTrace (); 79} 80} 81 if (ss! = Null) {82 try {83 ss. close (); 84} catch (IOException e) {85 e. printStackTrace (); 86} 87} 88} 89} 90}

 

In a more complex way, when the server receives a request from the client, it gives a friendly feedback.

 

1 package com. TCP. java; 2 3 import java. io. IOException; 4 import java. io. inputStream; 5 import java. io. outputStream; 6 import java.net. inetAddress; 7 import java.net. serverSocket; 8 import java.net. socket; 9 import java.net. unknownHostException; 10 11 import org. junit. test; 12 13 // the client sends information to the server, and the server prints the information to the console, send "received message" to client 14 public class TestTCP2 {15 // client 16 @ Test 17 public void client () {18 Sock Et s = null; 19 OutputStream OS = null; 20 InputStream is = null; 21 try {22 s = new Socket (InetAddress. getByName ("192.168.1.101"), 9797); 23 OS = s. getOutputStream (); 24 OS. write ("I Am a client ". getBytes (); 25 26 // shutdownOutput (): execute this method to explicitly tell the server that the message has been sent for 27 s. shutdownOutput (); 28 29 is = s. getInputStream (); 30 byte [] B = new byte [20]; 31 int length; 32 while (length = is. read (B ))! =-1) {33 String str = new String (B, 0, length); 34 System. out. println (str); 35} 36} catch (UnknownHostException e) {37 // TODO Auto-generated catch block 38 e. printStackTrace (); 39} catch (IOException e) {40 // TODO Auto-generated catch block 41 e. printStackTrace (); 42} 43 if (is! = Null) {44 try {45 is. close (); 46} catch (IOException e) {47 // TODO Auto-generated catch block 48 e. printStackTrace (); 49} 50} 51 if (OS! = Null) {52 try {53 OS. close (); 54} catch (IOException e) {55 // TODO Auto-generated catch block 56 e. printStackTrace (); 57} 58} 59 if (s! = Null) {60 try {61 s. close (); 62} catch (IOException e) {63 // TODO Auto-generated catch block 64 e. printStackTrace (); 65} 66} 67} 68 // server 69 @ Test 70 public void server () {71 ServerSocket ss = null; 72 Socket s = null; 73 InputStream is = null; 74 OutputStream OS = null; 75 try {76 ss = new ServerSocket (9797); 77 s = ss. accept (); 78 is = s. getInputStream (); 79 80 byte [] B = new byte [20]; 81 int length; 82 while (length = is. read (B ))! =-1) {83 String str = new String (B, 0, length); 84 System. out. println (str); 85} 86 87 OS = s. getOutputStream (); 88 OS. write ("received request ". getBytes (); 89} catch (IOException e) {90 // TODO Auto-generated catch block 91 e. printStackTrace (); 92} 93 finally {94 if (OS! = Null) {95 try {96 OS. close (); 97} catch (IOException e) {98 // TODO Auto-generated catch block 99 e. printStackTrace (); 100} 101} 102 if (is! = Null) {103 try {104 is. close (); 105} catch (IOException e) {106 // TODO Auto-generated catch block107 e. printStackTrace (); 108} 109} 110 if (s! = Null) {111 try {112 s. close (); 113} catch (IOException e) {114 // TODO Auto-generated catch block115 e. printStackTrace (); 116} 117} 118 if (ss! = Null) {119 try {120 ss. close (); 121} catch (IOException e) {122 // TODO Auto-generated catch block123 e. printStackTrace (); 124} 125} 126} 127} 128}

 

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.