Java: The use of network programming value TCP

Source: Internet
Author: User

Demo TCP Transport 1.TCP Sub-client and server2. The client side corresponds to the Scoketthe object that corresponds to the server is ServerscoketClient:by reviewing the Scoket object, it is found that the specified host can be connected when the object is created. because TCP is connection-oriented, in the establishment of the Scoket service, there must be a service side exists, and the connection is successful, after the formation of a path, the transmission of data in the channel.  Requirement: Send a text data to the serversteps:1. Create the socket service and specify the host and port to connect to. You can use the constructor method socket (String host, int port) or object method Connect (socketaddress Endpoint)2. Gets the output stream in the Sockett, which is used to send the data. Method is OutputStream Getoutputstream ()3. Close the client stream. //Examples are as follows:
Importjava.net.*;ImportJava.io.*;classtcpclient{ Public Static voidMain (string[] args)throwsException {//Create a Client socket service that specifies the destination host and PortSocket s =NewSocket ("192.168.1.105", 8888); //In order to send data, you should get the output stream in the socket,OutputStream out =S.getoutputstream (); Out.write ("TCP is coming!". GetBytes ()); //Close Client FlowS.close (); }}

requirements: Define the endpoint to receive data and print it on the console. Service side:1. Set up the socket service on the server, constructor serverscoket (int port), and listen to a port2. Get connected to the client terminal object, method socket accept (), this method is blocking, no connection, etc. 3. If the customer sends over the data, then the service side will use the corresponding customer service object, and use the client read stream object to read the data sent. and print it in the console. 4. Close the service side (optional operation)//Examples are as follows:
classtcpserver{ Public Static voidMain (string[] args)throwsException {//set up the socket service on the server and listen to a portServerSocket SS =NewServerSocket (8888); //get the Connected customer service object through the Accept method .Socket s =ss.accept (); String IP=s.getinetaddress (). gethostaddress (); System.out.println (IP+ "... connected!"); //gets the data that the client sends to the server to read the data using the read stream class of the customer-side object. Method InputStream getInputStream ()InputStream in =S.getinputstream (); byte[] buf =New byte[1024]; intLen =In.read (BUF); System.out.println (NewString (buf,0, Len)); S.close ();//shutting down the clientss.close ();//Close the service side (optional operation)    }}

Java: The use of network programming value TCP

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.