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