java套接字编程

java socket programing

本博客转自网络,我做了一定的整理,便于以后的复习

Datagram communication:

The datagram communication protocol, known as UDP (user datagram protocol), is a connectionless protocol, meaning that each time you send datagrams, you also need to send the local socket descriptor and the receiving socket’s address. As you can tell, additional data must be sent each time a communication is made.
Key word : datagram(资料包), UDP(unreliable, packet-switched, packet data, no connection overhead, application-level protocols exchange information immediately, two-way communication)

Stream communication

The stream communication protocol is known as TCP (transfer control protocol). Unlike UDP, TCP is a connection-oriented protocol. In order to do communication over the TCP protocol, a connection must first be established between the pair of sockets. While one of the sockets listens for a connection request (server), the other asks for a connection (client). Once two sockets have been connected, they can be used to transmit data in both (or either one of the) directions.
Key word : connection-oriented, TCP(reliable, connection-oriented, byte-stream, connection established before application-level protocols exchange information, two-way communication.)

TCP or UDP?

  • UDP:

    Every time you send a datagram, you have to send the local descriptor and the socket address of the receiving socket along with it.In UDP, there is a size limit of 64 kilobytes on datagrams you can send to a specified location.UDP is an unreliable protocol – there is no guarantee that the datagrams you have sent will be received in the same order by the receiving socket. UDP is less complex and incurs fewer overheads. It is often used in implementing client/server applications in distributed systems built over local area networks.

  • TCP:

    TCP is a connection-oriented protocol, on the other hand, a connection must be established before communications between the pair of sockets start. So there is a connection setup time in TCP. Once a connection is established, the pair of sockets behaves like streams: All available data are read immediately in the same order in which they are received.TCP is a reliable protocol; it is guaranteed that the packets you send will be received in the order in which they were sent.TCP is useful for implementing network services – such as remote login (rlogin, telnet) and file transfer (FTP) – which require data of indefinite length to be transferred.

socket programming using TCP/IP method(只是记录关键的步骤)

  • client:

    Socket clientSocket = new Socket(server_ip, port_number); // open a socket  
    DataInputStream dis = new DataInputStream(clientSocket.getInputStream()); // get response from the server  
    DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream()); // write java primitive type to the output stream  
    dos.close(); dis.close(); clientSocket.close(); // should always close the output stream and input stream before close the socket  
    
  • server:

    SeverSocket serverSocket = new ServerSocket(port_number); // open a socket  
    Socket connection = serverSocket.accept(); // accept connections from the client  
    DataInputStream dis = new DataInputStream(connection.geiInputStream()); // get message from the client  
    DataOutputStream dos = new DataOutputstream(conncetion.getOutputStream()); // send infomation to the client  
    dos.close(); dis.close(); connection.close(); serverSocket.close(); // should always close the output stream and input stream before close the socket  
    

猜你喜欢

转载自blog.csdn.net/qq_37993487/article/details/79922137