@@ -219,6 +219,7 @@ def set_dhcp(self, response_timeout=1):
219
219
_subnet_mask ,
220
220
_gw_addr ,
221
221
self ._dns ))
222
+ self ._src_port = 0
222
223
return 0
223
224
return - 1
224
225
@@ -492,16 +493,19 @@ def socket_connect(self, socket_num, dest, port, conn_mode=SNMR_TCP):
492
493
"""
493
494
assert self .link_status , "Ethernet cable disconnected!"
494
495
if self ._debug :
495
- print ("*** Connecting: Socket# {}, conn_mode: {}" .format (socket_num , conn_mode ))
496
-
496
+ print ("* w5k socket connect, protocol= {}, port={}, ip= {}" .format (conn_mode , port ,
497
+ self . pretty_ip ( dest )))
497
498
# initialize a socket and set the mode
498
- res = self .socket_open (socket_num , dest , port , conn_mode = conn_mode )
499
+ res = self .socket_open (socket_num , conn_mode = conn_mode )
499
500
if res == 1 :
500
501
raise RuntimeError ('Failed to initalize a connection with the socket.' )
501
502
503
+ # set socket destination IP and port
504
+ self ._write_sndipr (socket_num , dest )
505
+ self ._write_sndport (socket_num , port )
506
+ self ._send_socket_cmd (socket_num , CMD_SOCK_CONNECT )
507
+
502
508
if conn_mode == SNMR_TCP :
503
- # TCP client - connect socket
504
- self ._send_socket_cmd (socket_num , CMD_SOCK_CONNECT )
505
509
# wait for tcp connection establishment
506
510
while self .socket_status (socket_num )[0 ] != SNSR_SOCK_ESTABLISHED :
507
511
time .sleep (0.001 )
@@ -539,17 +543,16 @@ def get_socket(self, sockets):
539
543
print ("Allocated socket #{}" .format (sock ))
540
544
return sock
541
545
542
- def socket_open (self , socket_num , dest , port , conn_mode = SNMR_TCP ):
543
- """Opens a socket to a destination IP address or hostname . By default, we use
546
+ def socket_open (self , socket_num , conn_mode = SNMR_TCP ):
547
+ """Opens a TCP or UDP socket . By default, we use
544
548
'conn_mode'=SNMR_TCP but we may also use SNMR_UDP.
545
549
"""
546
550
assert self .link_status , "Ethernet cable disconnected!"
547
551
if self ._debug :
548
552
print ("*** Opening socket %d" % socket_num )
549
553
if self ._read_snsr (socket_num )[0 ] == SNSR_SOCK_CLOSED :
550
554
if self ._debug :
551
- print ("w5k socket begin, protocol={}, port={}, ip={}" .format (conn_mode , port ,
552
- self .pretty_ip (dest )))
555
+ print ("* Opening W5k Socket, protocol={}" .format (conn_mode ))
553
556
time .sleep (0.00025 )
554
557
555
558
self ._write_snmr (socket_num , conn_mode )
@@ -561,10 +564,6 @@ def socket_open(self, socket_num, dest, port, conn_mode=SNMR_TCP):
561
564
else :
562
565
self ._write_sock_port (socket_num , randint (49152 , 65535 ))
563
566
564
- # set socket destination IP and port
565
- self ._write_sndipr (socket_num , dest )
566
- self ._write_sndport (socket_num , port )
567
-
568
567
# open socket
569
568
self ._write_sncr (socket_num , CMD_SOCK_OPEN )
570
569
self ._read_sncr (socket_num )
0 commit comments