你应该看到这样的局域网ip地址:192.168.1.102,通常是无线路由器默认的本地地址。例如,通过使用ipconfig,您将在windows命令提示符中看到以下内容:C:\Users\jackc>ipconfig
Windows IP Configuration
Wireless LAN adapter Wireless Network Connection 3:
Media State . . . . . . . . . . . : Media disconnected
Connection-specific DNS Suffix . :
Wireless LAN adapter Wireless Network Connection:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::ed97:91a4:9449:204b%13
IPv4 Address. . . . . . . . . . . : 192.168.8.106
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.8.1
我尝试了以下对我有效的测试代码。
客户:#!/usr/bin/env python3
import sys
from socket import socket, AF_INET, SOCK_DGRAM
SERVER_IP = '192.168.8.102'
PORT_NUMBER = 5000
SIZE = 1024
print ("Test client sending packets to IP {0}, via port {1}\n".format(SERVER_IP, PORT_NUMBER))
mySocket = socket( AF_INET, SOCK_DGRAM )
myMessage = "Hello!"
myMessage1 = ""
i = 0
while i < 10:
mySocket.sendto(myMessage.encode('utf-8'),(SERVER_IP,PORT_NUMBER))
i = i + 1
mySocket.sendto(myMessage1.encode('utf-8'),(SERVER_IP,PORT_NUMBER))
sys.exit()
服务器:#!/usr/bin/env python3
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM
import sys
PORT_NUMBER = 5000
SIZE = 1024
hostName = gethostbyname( '0.0.0.0' )
mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )
print ("Test server listening on port {0}\n".format(PORT_NUMBER))
while True:
(data,addr) = mySocket.recvfrom(SIZE)
print data
sys.exit()