1 socket套接字  class 对象

    2 socket UDP通信

In [1]: import socket

In [2]: help(socket.socket)

 

class socket(_socket.socket)
 |  A subclass of _socket.socket adding the makefile() method.
 |  
 |  Method resolution order:
 |      socket
 |      _socket.socket
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  __enter__(self)
 |  
 |  __exit__(self, *args)
 |  
 |  __getstate__(self)

 

2.socket通信udp

    2 socket UDP通信

    2 socket UDP通信

 

from socket import *

udp_socket = socket(AF_INET,SOCK_DGRAM)

udp_socket.sendto("haha",("192.168.123.1",8080))

    2 socket UDP通信

 

 

 

 

 3.端口的问题

      2 socket UDP通信

 

#-*- coding:utf-8 -*-
from socket import *

udp_socket = socket(AF_INET,SOCK_DGRAM)

udp_socket.sendto("haha",("192.168.123.1",8080))

#使用upd发送的数据,在每一次都要写上接收方的ip和port
udp_socket.sendto("haha",("192.168.123.1",8080))

 

    2 socket UDP通信

 

 

 

  2)绑定端口号

#-*- coding:utf-8 -*-
from socket import *

udp_socket = socket(AF_INET,SOCK_DGRAM)

udp_socket.bind(("",8888))

udp_socket.sendto("haha",("192.168.123.1",8080))

 

     2 socket UDP通信

 

4.接受数据

  • 接收方需要绑定端口
  • 发送方不需要绑定
#-*- coding:utf-8 -*-
from socket import *

udp_socket = socket(AF_INET,SOCK_DGRAM)

udp_socket.bind(("",7777))

recv_data = udp_socket.recvfrom(1024)   #套接字接受数据
print(recv_data)

      2 socket UDP通信

      2 socket UDP通信

 

5.upd网路通信过程

 

      2 socket UDP通信

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2021-11-18
  • 2022-12-23
猜你喜欢
  • 2021-12-01
  • 2021-12-17
  • 2021-09-07
  • 2021-09-13
  • 2021-09-05
  • 2021-12-14
  • 2021-08-08
相关资源
相似解决方案