Project Features:
Implement one-time send and receive, the server receives the content sent by the client and prints out
In the most popular language, the middle-large ellipse is the server, a, C is the client, when the client and the server through the socket to establish a connection
1. A channel is formed between the two, in which the content to be sent is placed in the stream: Out.wirte ("byte array")
2. The server side uses the client's object to get the socket s,s including the client's IP, port, content and other properties
3.in to get the content in S, which is the client's out and server-side in one by one correspondence
4. The last server will be closed (based on client-generated) s. Only the server socket SS can be selectively closed
Code:
1 /*2 * Client:3 * By looking up the socket object, it is found that TCP is connection-oriented, and when all objects are established, the specified host is connected4 * The service side exists and the connection is successful, and after the channel is formed, it is transmitted again.5 6 steps:7 1. Create socket service, specify destination host and Port8 */9 ImportJava.io.*;Ten Importjava.net.*; One A classtcpClient - { - Public Static voidMain (string[] args)throwsioexception{ the //1. Create socket service, specify destination host and Port -Socket s =NewSocket ("192.168.1.103", 10000); - - //to send data, you should get the output stream of the socket stream +OutputStream out =S.getoutputstream (); - + //putting content in a stream AOut.write ("TCP I am a client". GetBytes ()); at - s.close (); - } - } - - /* in requirements: Define the endpoint to receive data and print it in the console - to Service side: + 1. Create the server-side ServerSocket object and specify the port - When not specified, use the Connect method to specify the 2. Get the client object connected * wait through ServerSocket's accept method, blocking, no connection waiting $ 3. If the client sends over the data, the server uses the corresponding connection to fetch the data from the read stream.Panax Notoginseng Print at the service desk - 4, turn off the server (optional) the */ + A classTCPServer the { + Public Static voidMain (string[] args)throwsioexception{ - $ //set up a service-side socket service and listen for ports $ServerSocket SS =NewServerSocket (10000); - - //Get the Client object (content, port, IP attribute) from the link through the Accept method theSocket s =ss.accept (); - //IP:WuyiString IP =s.getinetaddress (). gethostaddress (); theSystem.out.println (ip+ "... Connection Successful " ); - //content: Gets the data sent by the client, then uses the client object s WuInputStream in =S.getinputstream (); - About byte[] buf =New byte[1024]; $ intlen=In.read (BUF); -String content =NewString (buf,0, Len); -System.out.println ("Content:" +content); - AS.close ();//shut down the client, the server can control the customer +Ss.close ();//close the server, optional operation the } -}
Bi Xiangdong TCP Learning Note 1