Python Socket Learning Tour (ii)

Source: Internet
Author: User

    • Socket function

Annotations:

Close and shutdown--end data transfer of the socket: close-----Close the socket ID of this process, but the link is still open, other processes with this socket ID can also use this link to read or write the socket ID shutdown--the socket link, read the time may be detective to the EOF Terminator, when writing may receive a sigpipe signal, this signal may not be received until the socket buffer is filled, shutdown also has a closed mode parameter, 0 Can not read, 1 can no longer write, 2 read and write can not. Multi-process applications:

1. If multiple processes share a socket, close is called once, and the count is reduced by 1 until the count is 0 o'clock, which means that the used process calls close and the socket is freed.

2. In a multi-process process, if a process is shutdown (SFD, shut_rdwr), other processes will not be able to communicate. If a process close (SFD) will not affect other processes.

    • Socket Programming Ideas

TCP Service side:

1 Create socket, bind socket to local IP with Port # socket.socket (Socket.af_inet,socket. SOCK_STREAM), S.bind ()

2 Start listening for connection #s. Listen ()

3 Enter the loop and continuously accept the client's connection request #s. Accept ()

4 then receive the incoming data, and send the data #s. recv (), S.sendall ()

5 When the transfer is complete, close the socket #s. Close ()

TCP Client:

1 Create socket, connect to remote address # Socket.socket (socket.af_inet,socket. SOCK_STREAM), S.connect ()

2 Send data and receive data after connection # S.sendall (), S.RECV ()

3 When the transfer is complete, close the socket #s. Close ()

Python Socket Learning Tour (ii)

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.