<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Networking :: CS2 course</title><link>https://robark.gitlab.io/networking/index.html</link><description>Chapter 8 Networking UDP TCP</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Tue, 30 May 2023 13:51:59 -0700</lastBuildDate><atom:link href="https://robark.gitlab.io/networking/index.xml" rel="self" type="application/rss+xml"/><item><title>Intro</title><link>https://robark.gitlab.io/networking/intro/index.html</link><pubDate>Wed, 13 Jan 2021 20:58:19 +0000</pubDate><guid>https://robark.gitlab.io/networking/intro/index.html</guid><description>Lesson 23: Networking Overview Here is a video which explains port forwarding really well.</description></item><item><title>UDP</title><link>https://robark.gitlab.io/networking/udp/index.html</link><pubDate>Wed, 13 Jan 2021 21:00:02 +0000</pubDate><guid>https://robark.gitlab.io/networking/udp/index.html</guid><description>Lesson 24: Simple UDP Another version of this video:
Server code starts first.
#server-receive udp # usage: python3 udprecv.py localhost 5555 #server receives import socket, sys #a socket is a pipe that connects to a port s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #Address Family_ Internet Datagram is udp host = sys.argv[1] # host should be localhost or 0.0.0.0 port = int(sys.argv[2]) # min 1025 max 65535 s.bind((host, port)) while True: print('before') data, addr = s.recvfrom(1024) # this line blocks print('after') data=data.decode() if data=='q': break print(f'{data} is from {addr}') Client sends data to server:</description></item><item><title>TCP</title><link>https://robark.gitlab.io/networking/tcp/index.html</link><pubDate>Thu, 14 Jan 2021 20:18:21 +0000</pubDate><guid>https://robark.gitlab.io/networking/tcp/index.html</guid><description>Lesson 25: Simple TCP Another version of this video: Server code:
#server-receive tcp import socket #a socket is a pipe that connects to a port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Address Family_ Internet TCP s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # so we can keep using the same port without waiting (optional) host = 'localhost' #or 0.0.0.0 when using the real network port = 3339 s.bind( (host, port) ) #bind to port 3339 because we are receiving s.listen(1) #the number of allowed pending connections print('before accept') conn, addr = s.accept() #blocking (same as input but instead waits for an incoming connection) print('after accept') print('conn = ', conn) print('addr = ', addr) data=True while data: data = conn.recv(1024) # also blocking but now for data from the established connection print(data.decode()) conn.close() Client code:</description></item><item><title>Tcp Echo Server</title><link>https://robark.gitlab.io/networking/tcpecho/index.html</link><pubDate>Fri, 15 Jan 2021 21:00:23 +0000</pubDate><guid>https://robark.gitlab.io/networking/tcpecho/index.html</guid><description>Lesson 26: TCP echo server Another version of this video:
Server code:
#server-receive tcp import socket, sys #a socket is a pipe that connects to a port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #Address Family_ Internet TCP s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # so we can keep using the same port host = sys.argv[1] port= int(sys.argv[2]) s.bind( (host, port) ) #bind to port 3335 because we are receiving s.listen(1) #the server listens for incomming connections try: while True: print('before accept') conn, addr = s.accept() #blocking (waits for an incoming connection) print('after accept') #print('conn = ', conn) #print('addr = ', addr) data = conn.recv(1024) # blocking but now for data from the established connection data=data.decode() print(f'{addr[0]} says {data}') data= 2*data conn.sendall(data.encode()) #or sendall except KeyboardInterrupt: conn.close() #close the last blocking s.accept with Ctrl-c Client code:</description></item><item><title>Tcp two computers</title><link>https://robark.gitlab.io/networking/tcp2comp/index.html</link><pubDate>Mon, 18 Jan 2021 21:19:41 +0000</pubDate><guid>https://robark.gitlab.io/networking/tcp2comp/index.html</guid><description>Lesson 27: TCP from two computers Alternate Video: Server side:
#server-receive tcp import socket, sys,os print('server pid:',os.getpid()) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) host = sys.argv[1] port= int(sys.argv[2]) s.bind( (host, port) ) s.listen(1) print(s) print() conn, addr = s.accept() print(conn) while True: reply = input("Send: ") conn.sendall(reply.encode()) if reply == 'bye': break data = conn.recv(1024) print('Client says: ',data.decode()) conn.close() Client side:</description></item><item><title>Pickle objects over network</title><link>https://robark.gitlab.io/networking/pickle/index.html</link><pubDate>Wed, 19 Jan 2022 20:07:17 +0000</pubDate><guid>https://robark.gitlab.io/networking/pickle/index.html</guid><description>Serializing python objects over network Here is a way to use the pickle module to send objects like lists, dictionaries or any object over the network using tcp:
Beware unpickling from untrusted sources is dangerous as this could execute arbitary code if the sender is malicious. In our example below it’s not an issue. Server code:
# tcp server: serializing python objects over network import socket, pickle s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) host = 'localhost' port= 4444 s.bind( (host, port) ) s.listen(1) conn, addr = s.accept() try: #must be sure that object fits in 1024 bytes L = pickle.loads(conn.recv(1024)) print(f'{addr[0]} sends object of type {type(L)}') print(L) L.append({'a':3, 'c':9}) data=pickle.dumps(L) conn.sendall(data) except pickle.UnpicklingError: print('Error: 1024 bytes read buffer is too small.Increase buffer size') finally: conn.close() Client code:</description></item><item><title>File_transfer</title><link>https://robark.gitlab.io/networking/file_transfer/index.html</link><pubDate>Tue, 30 May 2023 13:51:59 -0700</pubDate><guid>https://robark.gitlab.io/networking/file_transfer/index.html</guid><description>Transferring files over network Sending any file from the client to the server over the network using tcp:
Server code:
#filename: file_recv.py #usage: python3 file_recv.py 0.0.0.0 5555 cat2.jpg #file transfer demo. Recieve file (server) import socket, sys s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) host = sys.argv[1] port= int(sys.argv[2]) s.bind( (host, port) ) s.listen() conn, addr = s.accept() data=True with open(sys.argv[3], 'wb') as f: while data: data = conn.recv(1024) f.write(data) conn.close() Client code:</description></item><item><title>Async_UDP using pyFltk</title><link>https://robark.gitlab.io/networking/async_udp/index.html</link><pubDate>Tue, 19 Jan 2021 21:09:06 +0000</pubDate><guid>https://robark.gitlab.io/networking/async_udp/index.html</guid><description>Lesson 28: Asynchronous UDP using pyFltk event loop Alternate video:
import socket,sys from fltk import * # 3 args (client/server) host port #usage: python program.py server localhost 5555 #usage: python program.py client localhost 5555 #remember machine to machine, host = 0.0.0.0 class udpwin(Fl_Window): def __init__(self,x,y,w,h,label): Fl_Window.__init__(self,x,y,w,h,label) self.begin() self.inp=Fl_Input(50,80,300,40,"type") self.brow=Fl_Multi_Browser(50,180,300,150) self.end() self.inp.when(FL_WHEN_ENTER_KEY) self.inp.callback(self.send_cb) self.host = sys.argv[2] self.port=int(sys.argv[3]) self.s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) if sys.argv[1] == "server": self.s.bind((self.host, self.port)) # servers bind fd=self.s.fileno() print(fd) ''' fd or a file descriptor is an integer the OS uses to reference the socket. ''' Fl.add_fd( fd, self.receive_data) ''' an fltk function which watches the fd during the event loop. If any data is received by that socket then function "receive_data" is called ''' def send_cb(self, widget): #server receives before sending text=self.inp.value() self.brow.add("@B222@r"+text) #post your own messages # with Background color 222 and right justification if sys.argv[1] == "server": self.s.sendto(text.encode(), self.addr) #server else: #client self.s.sendto(text.encode(),(self.host,self.port)) def receive_data(self, fd): (text, self.addr)=self.s.recvfrom(1024) self.brow.add(text.decode()) a=udpwin(55,55,400,400,"sockets "+sys.argv[1]) a.show() Fl.run()</description></item><item><title>Async_TCP using pyFltk</title><link>https://robark.gitlab.io/networking/async_tcp/index.html</link><pubDate>Wed, 20 Jan 2021 20:00:24 +0000</pubDate><guid>https://robark.gitlab.io/networking/async_tcp/index.html</guid><description>Lesson 29: Asynchronous TCP using pyFltk event loop Server code:
#tcp server import socket from fltk import * class tcpwin(Fl_Window): def __init__(self,x,y,w,h,label): Fl_Window.__init__(self,x,y,w,h,label) self.begin() self.conbut=Fl_Light_Button(40,30,270,40,"Accept Connection") self.inp=Fl_Input(40,80,270,40,"Send:") self.brow=Fl_Multi_Browser(40,130,270,140) self.end() self.callback(self.close) self.conbut.callback(self.conbut_cb) self.inp.callback(self.send_cb) self.inp.when(FL_WHEN_ENTER_KEY) def conbut_cb(self, wid): host = 'localhost' port= 4444 self.s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #fd=3 self.s.bind((host, port)) self.s.listen() fdl=self.s.fileno() #listening fd Fl.add_fd(fdl, self.acceptConnections) #watch for connection requests def acceptConnections(self, fdl): #runs when data comes to socket s self.conn, raddr = self.s.accept() #fd=4 blocking self.fd=self.conn.fileno() #4 Fl.add_fd(self.fd, self.receive_data) #watch for data through established connection def close(self,wid): try: # in case no connection self.conn.close() except: print('closing without a connection') finally: self.hide() def send_cb(self, widget): self.conn.sendall(self.inp.value().encode()) def receive_data(self, fd): data=self.conn.recv(1024) print(data) if data==b'': self.conn.close() Fl.remove_fd(self.fd) else: self.brow.add(data.decode()) a=tcpwin(55,55,333,333,"TCP Server") a.show() Fl.run() Client code:</description></item><item><title>TCP chat server using pyFltk</title><link>https://robark.gitlab.io/networking/tcp_chatserver/index.html</link><pubDate>Tue, 26 Jan 2021 19:05:34 +0000</pubDate><guid>https://robark.gitlab.io/networking/tcp_chatserver/index.html</guid><description>Lesson 30: TCP chat server using pyFltk Chat Server code:
#tcp chat server import socket from fltk import * class tcpwin(Fl_Window): def __init__(self,x,y,w,h,label): Fl_Window.__init__(self,x,y,w,h,label) self.begin() self.conbut=Fl_Light_Button(10,30,170,40,"Accept Connection") self.conbut.callback(self.conbut_cb) self.end() self.callback(self.close) self.connD={} #dictionary of established connections def conbut_cb(self, wid): host = 'localhost' port= 4444 self.s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #fd=3 self.s.bind((host, port)) self.s.listen() fdl=self.s.fileno() #listening fd Fl.add_fd(fdl, self.acceptConnection) #watch for new connections def acceptConnection(self, fdl): #runs when data comes to socket s conn, raddr = self.s.accept() fd=conn.fileno() #file descriptor for new established connection Fl.add_fd(fd, self.receive_data) self.connD[fd]=conn #add fd(key) and conn(value) to connD dict def close(self,wid): try: # in case no connection for fd,conn in self.connD.items(): conn.close() Fl.remove_fd(fd) except: print('closing without a connection') finally: self.hide() def receive_data(self, fd): data= self.connD[fd].recv(1024) print(data) if data==b'': self.connD[fd].close() Fl.remove_fd(fd) self.connD.pop(fd) else: for conn in self.connD.values(): conn.sendall(data) a=tcpwin(55,55,200,200,"TCP Server") a.show() Fl.run() Client code: exactly the same as last lesson</description></item></channel></rss>