Day9 network programming and day9 Network Programming

Source: Internet
Author: User

Day9 network programming and day9 Network Programming

OSI (Open System Interconnect), that is, Open System Interconnection.

International Standardization Organization (ISO)

OSI Layer-7 model:

TCP/IP protocol:

TCP three-way handshake:

TCP four waves:

TCP communication:

Simulate simple client-server TCP Communication

Socket_server.py

#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# Author: wanghuafeng


Import socket
Ip_port = ('2017. 0.0.1 ', 127)
# Buy a mobile phone
S = socket. socket ()
# Buy a mobile phone card. The parameter in bind is yuanzu.
S. bind (ip_port)
# Start
S. listen (5)
# Waiting for a call. conn is a communication link.
Conn, addr = s. accept ()
# Message receiving, 1024 -- indicates bytes
Recv_data = conn. recv (1024)
Print (str (recv_data, encoding = 'utf-8 '))
# Sending messages
Send_data = recv_data.upper ()
Conn. send (send_data)
# Call
Conn. close ()
socket_client.py
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# Author: wanghuafeng


Import socket
Ip_port = ('2017. 0.0.1 ', 127)
# Buy a mobile phone
Sk = socket. socket ()
# Dialing
Sk. connect (ip_port)
# Sending messages
Send_data = input (">>:"). strip ()
Sk. send (bytes (send_data, encoding = 'utf-8 '))
# Receiving messages
Recv_data = sk. recv (1024)
Print (recv_data)
# Call
Sk. close ()
UDP Communication

# Server
Import socket
Ip_port = ('2017. 0.0.1 ', 127)
Sk = socket. socket (socket. AF_INET, socket. SOCK_DGRAM, 0)
Sk. bind (ip_port)

While True:
Data, (host, port) = sk. recvfrom (1024)
Print (data, host, port)
Sk. sendto (bytes ('OK', encoding = 'utf-8'), (host, port ))


# Client
Import socket
Ip_port = ('2017. 0.0.1 ', 127)

Sk = socket. socket (socket. AF_INET, socket. SOCK_DGRAM, 0)
While True:
Indium = input ('data: '). strip ()
If indium = 'exit ':
Break
Sk. sendto (bytes (indium, encoding = 'utf-8'), ip_port)
Data = sk. recvfrom (1024)
Print (data)

Sk. close ()

More functions

Sk. bind (address)

S. bind (address) binds the socket to the address. The address format depends on the address family. In AF_INET, the address is expressed in the form of a tuple (host, port.

Sk. listen (backlog)

Start listening for incoming connections. Backlog specifies the maximum number of connections that can be suspended before a connection is rejected.

Backlog = 5 indicates that the kernel has received a connection request, but the server has not called accept to process up to 5 connections.
This value cannot be infinitely large because the connection queue needs to be maintained in the kernel.

Sk. setblocking (bool)

Whether to block (True by default). If False is set, an error is returned if no data exists in accept and recv.

Sk. accept ()

Accept the connection and return (conn, address). conn is a new socket object and can be used to receive and send data. Address is the address used to connect to the client.

Receive TCP client connection (blocking) waiting for connection arrival

Sk. connect (address)

Connect to the socket at address. Generally, the format of address is tuples (hostname, port). If a connection error occurs, the socket. error is returned.

Sk. connect_ex (address)

The same as above, but there will be a return value. When the connection is successful, 0 is returned, and the encoding is returned when the connection fails, for example: 10061

Sk. close ()

Disable socket

Sk. recv (bufsize [, flag])

Accept socket data. Data is returned as a string, specified by bufsizeMaximumThe number of messages that can be received. Flag provides other information about messages, which can be ignored.

Sk. recvfrom (bufsize [. flag])

Similar to recv (), but the return value is (data, address ). Data is the string containing the received data, and address is the socket address for sending data.

Sk. send (string [, flag])

Send the data in the string to the connected socket. The returned value is the number of bytes to be sent, which may be smaller than the size of the string. That is, all specified content may not be sent.

Sk. sendall (string [, flag])

Send data in string to the connected socket, but try to send all data before returning. If None is returned successfully, an exception is thrown.

Internally, send all content by calling send recursively.

Sk. sendto (string [, flag], address)

Sends data to the socket. The address is a tuples in the form of (ipaddr, port) and specifies the remote address. The returned value is the number of bytes sent. This function is mainly used for UDP.

Sk. settimeout (timeout)

Sets the superperiod of socket operations. timeout is a floating point number in seconds. If the value is None, there is no superperiod. Generally, it should be set when the socket is just created during off-peak periods, because they may be used for connection operations (for example, a client connection can wait up to 5 s)

Sk. getpeername ()

Returns the remote address of the socket. The returned value is usually a tuples (ipaddr, port ).

Sk. getsockname ()

Return the address of the socket. It is usually a tuple (ipaddr, port)

Sk. fileno ()

Socket file descriptor

Send messages cyclically

Socket_server.py

#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# Author: wanghuafeng


Import socket
Ip_port = ('2017. 0.0.1 ', 127)
# Buy a mobile phone
S = socket. socket ()
# Buy a mobile phone card. The parameter in bind is yuanzu.
S. bind (ip_port)
# Start
S. listen (5)
# Waiting for a call. conn is a communication link.
Conn, addr = s. accept ()
While True:
Try:
# Message receiving, 1024 -- indicates bytes
Recv_data = conn. recv (1024)
Print (str (recv_data, encoding = 'utf-8 '))
If str (recv_data, encoding = 'utf-8') = 'eg': break
# Sending messages
Send_data = recv_data.upper ()
Conn. send (send_data)
Failed t Exception:
Break
# Call
Conn. close ()
socket_client.py
#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
# Author: wanghuafeng


Import socket
Ip_port = ('2017. 0.0.1 ', 127)
# Buy a mobile phone
Sk = socket. socket ()
# Dialing
Sk. connect (ip_port)
# Sending messages
While True:
Send_data = input (">>:"). strip ()
If len (send_data) = 0: continue
Sk. send (bytes (send_data, encoding = 'utf-8 '))
If send_data = 'exit ': break
# Receiving messages
Recv_data = sk. recv (1024)
Print (recv_data)
# Call
Sk. close ()
 

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.