A TCP transmission instance _java Java implementation socket

Source: Internet
Author: User

This article illustrates the TCP transmission of Java implementation socket. Share to everyone for your reference. The specific analysis is as follows:

Client sends data to server

* TCP transmission, the process of client establishment.
* 1, create the TCP client socket service. The socket object is used.
* It is recommended that the object be clearly defined as soon as it is created. The host to connect to.
* 2, if the connection is established successfully, the data transfer channel has been established.
* This channel is the socket stream, is the bottom set up. Since it is a stream, it shows that there is both input and output.
* Want to input or output stream objects, you can find a socket to get.
* Getoutputstream (), and getInputStream () can be used to get two bytes of stream.
* 3, using the output stream, write the data.
* 4, close the resource.

Package Com.socket.tcp.demo; 
Import java.io.IOException; 
Import Java.io.OutputStream; 
Import Java.net.Socket; 
Import java.net.UnknownHostException; 
  public class Clientdemo {/** * @param args * @throws ioexception * @throws unknownhostexception * * public static void Main (string[] args) throws Unknownhostexception, IOException {//client sends data to server/* TCP transmission, 
     The process of establishing the client. * 1, create the TCP client socket service. 
     The socket object is used. * It is recommended that the object be clearly defined as soon as it is created. 
     The host to connect to. 
     * 2, if the connection is established successfully, the data transfer channel has been established. * This channel is the socket stream, is the bottom set up. 
     Since it is a stream, it shows that there is both input and output. 
     * Want to input or output stream objects, you can find a socket to get. 
     * Getoutputstream (), and getInputStream () can be used to get two bytes of stream. 
     * 3, using the output stream, write the data. 
     * 4, close the resource. 
    *//CREATE client Socket service. 
    Connect the address of the target server, 192.168.1.100 is the address of the target server, 10002 is the port of the target server socket socket = new Socket ("192.168.1.100", 10002); Gets the output stream in the socket stream. 
    Output a message to the server that sends a message to the server outputstream out = Socket.getoutputstream (); 
    Writes the specified data using the output stream. Out.write ("TCP demo: Dude's coming again!"). GetBytes ()); 
    Closes the resource. 
  Socket.close ();

 } 
}

The server receives the data sent over by the client and prints it on the console.

* Establish the TCP service side of the idea:
* 1, create the service-side socket service. Through the ServerSocket object.
* 2, the service side must provide a port, otherwise the client can not connect.
* 3, get the client object that is connected.
* 4, obtaining the socket stream from the client object to read the data sent by the client
* and print on the console.
* 5, close the resource. Close the client, turn off the service side.

Package Com.socket.tcp.demo; 
Import java.io.IOException; 
Import Java.io.InputStream; 
Import Java.net.ServerSocket; 
Import Java.net.Socket; public class Serverdemo {//udp: sender, Receiver (no connection)//TCP: Client, server side (to connect) Start the server first, start the client/** * @param args * @throw  
    s ioexception/public static void main (string[] args) throws IOException {//server-side receives the data sent by the client and prints it on the console. * * To establish the TCP service side of the idea: * 1, to create a service-side socket service. 
     Through the ServerSocket object. 
     * 2, the service side must provide a port, otherwise the client can not connect. 
     * 3, get the client object that is connected. 
     * 4, the client object gets the socket stream to read the data sent by the client * and prints it on the console. * 5, close the resource. 
     Close the client, turn off the service side. 
    *///1 Create a service-side object.
    ServerSocket ss = new ServerSocket (10002); 
    Monitor the application of the server's 10002 port,/See if there is no client connection, or send message//2, Get connected client objects. 
    Socket s = ss.accept ();//blocking.
    String IP = s.getinetaddress (). gethostaddress (); 
    Get the IP address of the client connected//3, get the input stream through the socket object, read the data from the client, inputstream in = S.getinputstream (); 
    byte[] buf = new byte[1024]; 
  int len = In.read (BUF);  String text = new string (Buf,0,len); 
    System.out.println (ip+ ":" +text); 
    S.close (); Ss.close ()//Shut down the server, theoretically not shut down}}

Run Effect chart: (Start the server first, then start the client)

I hope this article will help you with your Java 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.