python-- acquaintance socket

What is a socket  

  Two programs on the network via a bidirectional communication to realize the exchange of data, called the connection end of a socket. socket commonly referred to as "socket" is used to describe IP address and port, a communication link is the handle can be used to enable communication between different virtual machine or on different computers. Hosts on the Internet generally running multiple software services, while providing several services. Each service will open a Socket, and bind to a port, different port corresponding to the different services.

  English Socket original meaning is "hole" or "socket." Socket as its English intended as a porous plug like. If a host socket covered with a variety of rooms, each socket has a serial number, some outlets provide 220 volts AC, and some provide 110 volts AC, while others provide cable programming. The client software will plug into the socket different numbers, you can get different services.

  I know that part of the above words are boring, then forget it! We only need to remember: two programs through the "Network" interactive data on the use of socket, it is responsible for two things: to establish a connection, transfer data. No longer a concise! ! !

How to perform a socket section of the code?

  First, we look at the example below, nothing needs to understand, we only know so you can achieve write a socket, so that the two pass messages to each other:

  server:     client:                      

  Let me talk about how they run up, the new two python files: socket_server.py, socket_client.py respectively demo1_server Code and demo1_client Code code stuck to the corresponding file, perform socket_server.py, then execute socket_client. py, you can see the effect it! Take the code, first to experience the effects of the socket.

  demo1_server Code
  demo1_client Code

One minute has its own socket program!

  The successful implementation of it? Let us not explain the principles, all socket to be able to use them as a precondition, the above code as a template code to achieve socket, take a look at what things we can replace, first put together a program of their own socket.

  server:        client:

                           

  We start to print out the contents of view, server receives the information sent from the client end over 'help'. And sends a confirmation message 'received' assigned to the client, the client end we have received this message. This is the data exchange process. send a message method, recv message receiving method. We only need to modify the contents of recv and send in, you can achieve data exchange program of the two.

  Realization socket is like the process of taking delivery of the courier, sent the parcel is server-side, client-side courier is content, send and recv is that we want to send something, so ready to be sent stuff we want to tell the courier to pick where to look sock.bind method in the server, there passed a tuple ( 'ip', 'port') to bind method, it is to put this package on this address port waiting for the courier; and obj_client.connect method is to tell the client end of the courier, to the address to find the port to pick up the parcel. So this tuple ip and port must be consistent, and I couriers appointment. What do I send the content does not matter, what I send, and what the client recv, because what I want to send, couriers get what is, he has no right to send me about something. Act now, to send the stuff and put it in the address into your own, to get one of their own socket program it.

Detailed program code socket:

   server:     client: 

 Create a socket object - arguments detailed:

 

                                     

  A parameter: address family

    socket.AF_INET IPv4 (默认)
    socket.AF_INET6 IPv6

    socket.AF_UNIX 只能够用于单一的Unix系统进程间通信

  参数二:类型

    socket.SOCK_STREAM  流式socket , for TCP (默认)
    socket.SOCK_DGRAM   数据报式socket , for UDP

    socket.SOCK_RAW 原始套接字,普通的套接字无法处理ICMP、IGMP等网络报文,而SOCK_RAW可以;其次,SOCK_RAW也可以处理特殊的IPv4报文;此外,利  用原始套接字,可以通过IP_HDRINCL套接字选项由用户构造IP头。
    socket.SOCK_RDM 是一种可靠的UDP形式,即保证交付数据报但不保证顺序。SOCK_RAM用来提供对原始协议的低级访问,在需要执行某些特殊操作时使用,如发送ICMP报文。SOCK_RAM通常仅限于高级用户或管理员运行的程序使用。
    socket.SOCK_SEQPACKET 可靠的连续数据包服务

  参数三:协议(这个参数用默认的就好,所以在我们创建对象的时候没有使用此参数,忘记这个参数吧)

    0  (默认)与特定的地址家族相关的协议,如果是 0 ,则系统就会根据地址格式和套接类别,自动选择一个合适的协议'

UDP协议实现socket代码实例:

  我们刚刚看的例子中都是使用TCP协议进行传输,下面上一段UDP的例子。

  UDP Server Code
  UDP Client Code

 

参考:

socket详解:http://www.cnblogs.com/wupeiqi/articles/5040823.html

TCP和UDP协议的区别:http://jingyan.baidu.com/article/6dad5075df3452a123e36ecb.html

Guess you like

Origin www.cnblogs.com/fjhboy/p/12129071.html