Commits

Brian Thorne committed b357792

Fix to basic socket example to actually create a new thread. Also hardcode freerunner address.

  • Participants
  • Parent commits 5f956cb

Comments (0)

Files changed (2)

basic_socket_client.py

 # Echo client program
 import socket
 
-HOST = 'localhost'    # The remote host
-PORT = 50007          # The same port as used by the server
+#host = 'localhost'    # The "remote" host
+host = '192.168.7.2'
+random_port = 50007          # The same port as used by the server
 
-af, socktype, proto, _, sa = socket.getaddrinfo(HOST, 
-                                                PORT, 
+af, socktype, proto, _, sa = socket.getaddrinfo(host, 
+                                                random_port, 
                                                 socket.AF_UNSPEC, 
                                                 socket.SOCK_STREAM)[0]
 

basic_socket_server.py

-# Echo server program
+#!/usr/bin/env python3
+# Echo consumer_server program
 import socket
 import threading
 
-HOST = None               # Symbolic name meaning all available interfaces
-PORT = 50007              # Arbitrary non-privileged port
+host = "localhost"               # Symbolic name meaning all available interfaces
+random_port = 50007              # Arbitrary non-privileged port
 
 def echo(conn, addr):
     while True:
         conn.send(data)
     conn.close()
 
-def server(address_family, socket_type, protocol, canonname, sa):
+def consumer_server(address_family, socket_type, protocol, canonname, sa):
     try:
         s = socket.socket(address_family, socket_type, protocol)
     except OSError as msg:
         try:
             conn, addr = s.accept()
             print('Connected by', addr)
-            threading.Thread(target=echo, args=(conn, addr)).run()
+            threading.Thread(target=echo, args=(conn, addr)).start()
         except KeyboardInterrupt:
             s.close()
             raise SystemExit()
 
-res = socket.getaddrinfo(HOST, PORT, socket.AF_INET6, socket.SOCK_STREAM, 0, socket.AI_PASSIVE)[0]
-server(*res)
+address_info = socket.getaddrinfo(host, random_port, 
+                         socket.AF_UNSPEC, socket.SOCK_STREAM, 
+                         0, socket.AI_PASSIVE)[-1]
+consumer_server(*address_info)