@@ -219,7 +219,6 @@ def set_dhcp(self, response_timeout=1):
219
219
_subnet_mask ,
220
220
_gw_addr ,
221
221
self ._dns ))
222
- self ._src_port = 0
223
222
return 0
224
223
return - 1
225
224
@@ -496,14 +495,10 @@ def socket_connect(self, socket_num, dest, port, conn_mode=SNMR_TCP):
496
495
print ("*** Connecting: Socket# {}, conn_mode: {}" .format (socket_num , conn_mode ))
497
496
498
497
# initialize a socket and set the mode
499
- res = self .socket_open (socket_num , port , conn_mode = conn_mode )
498
+ res = self .socket_open (socket_num , dest , port , conn_mode = conn_mode )
500
499
if res == 1 :
501
500
raise RuntimeError ('Failed to initalize a connection with the socket.' )
502
501
503
-
504
- self ._write_sndipr (socket_num , dest )
505
- self ._write_sndport (socket_num , port )
506
-
507
502
if conn_mode == SNMR_TCP :
508
503
# TCP client - connect socket
509
504
self ._send_socket_cmd (socket_num , CMD_SOCK_CONNECT )
@@ -544,7 +539,7 @@ def get_socket(self, sockets):
544
539
print ("Allocated socket #{}" .format (sock ))
545
540
return sock
546
541
547
- def socket_open (self , socket_num , port , conn_mode = SNMR_TCP ):
542
+ def socket_open (self , socket_num , dest , port , conn_mode = SNMR_TCP ):
548
543
"""Opens a socket to a destination IP address or hostname. By default, we use
549
544
'conn_mode'=SNMR_TCP but we may also use SNMR_UDP.
550
545
"""
@@ -553,19 +548,23 @@ def socket_open(self, socket_num, port, conn_mode=SNMR_TCP):
553
548
print ("*** Opening socket %d" % socket_num )
554
549
if self ._read_snsr (socket_num )[0 ] == SNSR_SOCK_CLOSED :
555
550
if self ._debug :
556
- print ("w5k socket begin, protocol={}, port={}" .format (conn_mode , port ))
551
+ print ("w5k socket begin, protocol={}, port={}, ip={}" .format (conn_mode , port ,
552
+ self .pretty_ip (dest )))
557
553
time .sleep (0.00025 )
558
554
559
555
self ._write_snmr (socket_num , conn_mode )
560
556
self ._write_snir (socket_num , 0xFF )
561
557
562
- if port > 0 :
563
- # write to defined port
564
- self ._write_sock_port (socket_num , port )
558
+ if self . _src_port > 0 :
559
+ # write to socket source port
560
+ self ._write_sock_port (socket_num , self . _src_port )
565
561
else :
566
- # if source port is not set, set the local port number
567
562
self ._write_sock_port (socket_num , randint (49152 , 65535 ))
568
563
564
+ # set socket destination IP and port
565
+ self ._write_sndipr (socket_num , dest )
566
+ self ._write_sndport (socket_num , port )
567
+
569
568
# open socket
570
569
self ._write_sncr (socket_num , CMD_SOCK_OPEN )
571
570
self ._read_sncr (socket_num )
@@ -581,7 +580,6 @@ def socket_close(self, socket_num):
581
580
print ("*** Closing socket #%d" % socket_num )
582
581
self ._write_sncr (socket_num , CMD_SOCK_CLOSE )
583
582
self ._read_sncr (socket_num )
584
- self ._src_port = 0
585
583
586
584
def socket_disconnect (self , socket_num ):
587
585
"""Disconnect a TCP connection."""
0 commit comments