- 我打开服务器
- 我打开客户端
- 客户端向服务器发送“连接请求”
- 服务器接收“连接请求”
- 服务器向客户端发送“已连接”
- 客户端recvs从服务器“连接”
- 客户端打印“已连接”!
- 服务器崩溃,我找不到原因(我也看不到错误)。
服务器:
from socket import *;
iSocket = socket( AF_INET, SOCK_DGRAM );
iSocket.bind( ( "", 4325 ) );
while True:
recv_data, addr = iSocket.recvfrom( 2048 );
if addr[ 0 ] == "127.0.0.1":
#stuff
elif recv_data == "on":
print "New connection:", addr[ 0 ];
elif recv_data == "connectrequest":
iSocket.sendto( "connected", addr );
客户:
import time;
import select;
from socket import *;
address = ( '192.168.0.101', 4325 );
iSocket = socket( AF_INET, SOCK_DGRAM );
iSocket.connect( address );
reconnect = 10;
last_reconnect = 0;
connected = False;
while connected == False:
if last_reconnect <= int( time.time( ) ) + reconnect:
print "Connecting...";
iSocket.sendto( "connectrequest", address );
data_available = select.select( [ iSocket ], [ ], [ ], 11 )
if data_available[ 0 ]:
recv_data, addr = iSocket.recvfrom( 2048 );
if recv_data == "connected":
connected = True;
print "Connected!";
iSocket.sendto( "on", address );
while True:
recv_data, addr = iSocket.recvfrom( 2048 );
print recv_data, addr;