Python3.5 socket communication example (TCP)

Source: Internet
Author: User
This article mainly introduces the socket communication example (TCP) implemented by python3.5. I think it is quite good. I will share it with you and give you a reference. Let's take a look at the small Editor. This article mainly introduces the socket communication example (TCP) of python3.5. The small editor thinks it is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian.

TCP connection:

Tcp is a connection-oriented protocol, which means that a TCP connection needs to be created by handshaking before the client and server develop and send data. One end of the TCP connection is connected with the client socket, and the other end is connected with the server socket. When creating the TCP connection, we need to associate the client with the socket address (IP address and port number) of the server. Use the created TCP connection. when one side sends data to the other side, it only needs to pass the data through its socket to the TCP connection and does not need to attach the destination address again.

Client-server program using TCP connection:

TCPServer. py

import socketimport socketserverimport timeimport threading serverPort = 50007serverSocket =socket.socket(socket.AF_INET,socket.SOCK_STREAM)serverSocket.bind (('',serverPort))serverSocket.listen(1)print('The server is ready to receive')while 1:  connectionSocket,addr = serverSocket.accept()  sentence = connectionSocket.recv(1024)  capitalizedSentence = sentence.upper()  connectionSocket.send(capitalizedSentence)  connectionSocket.close()

TCPClient. py

from socket import *serverName = '127.0.0.1'serverPort = 50007clientSocket =socket(AF_INET,SOCK_STREAM)clientSocket.connect((serverName,serverPort))sentence = input('Input lowercase sentence:')clientSocket.send(sentence.encode())modifiedSentence = clientSocket.recv(1024)print('From Server:',modifiedSentence.decode())clientSocket.close()  

Note:

1. before the client can use a TCP socket to send data to the server, a TCP connection must be created between the client and the server;

2. clientSocket. connect (serverName, serverPort): execute three handshakes to create a TCP connection;

3. clientSocket. close (): Closes the TCP connection between the client and the server;

4. connectionSocket. close (): close the connection socket after sending it to the client.

Two sockets are required for TCP connection, and only one socket is required for UDP connection,

The TCP server supports n parallel connections, each of which comes from different clients. the TCP connection requires n + 1 socket.

The above is all the content of this article. I hope it will help you learn and support PHP.

For more articles about how to implement socket communication example (TCP) in python3.5, refer to PHP Chinese network!

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.