TCP communication program

 

Communication Theory TCP
TCP is a reliable protocol network protocol, in which each of the ends of the communication to establish a Socket object, thereby forming a virtual network across the communication link,
once the virtual network link is established, the procedure ends it can communicate through a virtual link.


Java network based on the TCP protocol provides a good package, using Socket object to represent the communication ports on both ends, and produces IO stream through Socket for network communications
Java client provides Socket talk, provided ServerSocket class of server-side

 

TCP sends data


TCP step of sending the data
① to create a client Socket object (Socket)
  Socket (String hqst, int Port)
② obtain the output stream to write data
  OutputStream getOutputStream0
③ release resources
  void close (

public class Socket
extends Object
implements Closeable
This class implements client sockets (also called a "socket"). A socket is an endpoint of communication between the two machines.
Package com.ThreadTest;
 / * 
    the TCP transmission data in step 
        1: Create the client Socket object (Socket) 
        2: obtain an output stream, the write data 
        3: release resources 

 * / 

Import java.io.IOException;
 Import a java.io.OutputStream;
 Import the java.net.Socket; 


public  class ClientDemo {
     public  static  void main (String [] args) throws IOException {
 //         . 1: Create the Socket target client (Socket)
 //         Socket (InetAddress address, int Port) create flow sleeve Sockets and connect it to the specified port number specified IP address.
//     the Socket new new S = the Socket (InetAddress.getByName ( "192.168.1806"), 10000);
 //        Socket (String host, int port) creates a stream socket and connects to the specified port number on the specified host. 
        S = the Socket new new the Socket ( "192.168.18.6", 10000 ); 

        // Get the output stream, the write data
 //         the Socket class following methods public OutputStream getOutputStream () throws IOException Returns socket output stream. 
        Os = OutputStream s.getOutputStream (); 
        os.write ( "I came, the Hello, TCP" .getBytes ()); 

        // release resources 
        S.CLOSE (); 
    } 
}

TCP receives data

ServerSocket​(int port)
Creating bind to the specified port server socket.

Step TCP receive data
① created Socket object server (the ServerSocket)
  the ServerSocket (int Port)
② listen for client connections, returns a Socket object
  Socket Accept ()
③ acquired input stream, the read data, and the data displayed in the console
  InputStream getInputStream ()
④ release resources
  void close ()

Package com.ThreadTest;
 / * 
the step of receiving data TCP 

        1: Create the Socket object server (the ServerSocket) 
        2: takes an input stream, the read data, and the data displayed in the console 
        3: releasing resources 

 * / 

Import java.io.IOException ;
 Import a java.io.InputStream;
 Import the java.net.ServerSocket;
 Import the java.net.Socket; 

public  class ServerDemo {
     public  static  void main (String [] args) throws IOException {
 //         . 1: Create the Socket object server (the ServerSocket )
 //         ServerSocket (int port) create a binding to the specified port server socket. 
        SS = ServerSocket new newThe ServerSocket (10000 ); 

//         2 Accept () is connected to this socket and listens to accept it. 
        S = the Socket ss.accept ();
 //         . 3: takes an input stream, the read data, the data displayed on the console and 
        the InputStream IS = s.getInputStream ();
         byte [] = BYS new new  byte [1024 ];
         int len;
         the while (! (len = is.read (BYS)) = -1 ) { 
            System.out.println ( "data:" + new new String (BYS, 0 , len)); 
        } 
        // . 4 release resources 
        ss.close (); 


    } 
}

 

Guess you like

Origin www.cnblogs.com/lsswudi/p/11439973.html