upd簡單廣播例子

2021-07-11 13:50:37 字數 1695 閱讀 9914

給區域網同一網段傳送廣播包(同區域網不同網段暫時還去看)

廣播包傳送者:

#coding=gbk

#!/python/bin

import socket

sock = socket.socket(socket.af_inet, socket.sock_dgram)

sock.setsockopt(socket.sol_socket, socket.so_reuseaddr, 1)

sock.setsockopt(socket.sol_socket, socket.so_broadcast, 1)

sock.bind(('', 3333))

while 1:

try:

data, addr = sock.recvfrom(1024)

print "r", data, addr

ret = sock.sendto("all msg", addr)

print "ret:", ret

except keyboardinterrupt:

raise

廣播接受者, 向區域網廣播乙個包, 如果有傳送接收(監聽), 傳送者則把內容傳送給廣播者

import socket

sock = socket.socket(socket.af_inet, socket.sock_dgram)

sock.setsockopt(socket.sol_socket, socket.so_broadcast, 1)

addr = ('

', 3333)

print sock.sendto("x", addr)

while 1:

data = sock.recvfrom(1024)

print data

異或這樣

#coding=gbk

#!/python/bin

import socket

import threading

import time

sock = socket.socket(socket.af_inet, socket.sock_dgram)

sock.setsockopt(socket.sol_socket, socket.so_reuseaddr, 1)

sock.setsockopt(socket.sol_socket, socket.so_broadcast, 1)

sock.bind(('', 3333))

def proc():

while 1:

for addr in addrs:

sock.sendto("msg", addr)

time.sleep(1)

t = threading.thread(target=proc)

t.setdaemon(true)

addrs =

t.start()

while 1:

try:

data, addr = sock.recvfrom(1024)

print "r", data, addr

ret = sock.sendto("all msg", addr)

print "ret:", ret

except keyboardinterrupt:

raise

乙個簡單的UDP廣播例子 含C S

乙個簡單的udp廣播通訊的例子程式,含有服務端和客戶端的 服務端 include stdafx.h include include pragma comment lib,ws2 32.lib int tmain int argc,tchar argv if 2 hibyte wsadata.wver...

python 多執行緒 UPD 聊天器簡單實現

執行緒1用來接收資料然後顯示 執行緒2用來檢測鍵盤資料然後通過udp傳送資料 import socket import threading help socket defrecv msg udp socket while true recv msg udp socket.recvfrom 1024 ...

AS3中不同類間的廣播與接收簡單例子

1.文件類 dispatcheventdemo.as package 2.中間介質類,連系不同的類 dt.as package 3.擴充套件事件類,方便進行事件中的引數傳遞 eventx.as package public function get arg object 4.包含事件接收的類 a.a...