1 / 31

Socket Programming

Socket Programming. Basics. Socket is an interface between application and network Application creates a socket Socket type dictates the style of communication Once socket is configured, applications Pass data to the socket for network transmission

inga-dixon
Download Presentation

Socket Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Socket Programming

  2. Basics • Socket is an interface between application and network • Application creates a socket • Socket type dictates the style of communication • Once socket is configured, applications • Pass data to the socket for network transmission • Receive data transmitted across the network from the socket

  3. Server Program

  4. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  5. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  6. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  7. Creating Sockets sockfd = socket(socket_family, socket_type) socket_family: Network Layer Protocol • AF_INET – IPv4 • AF_INET6 – IPv6 socket_type: Transport Layer Protocol • SOCK_STREAM – TCP • SOCK_DGRAM – UDP

  8. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  9. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  10. Binding Sockets sockfd.bind((host_address, port)) Binds the socket to particular address and port • ‘ ’ indicates “any interface” address Why no bind called for client??

  11. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  12. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  13. Listen for Connections sockfd.listen(backlog) • Prepares socket to accept connections • backlog: number of pending connections allowed • Allows sockets to respond to new connections using the three-way handshake

  14. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  15. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  16. Accept Connections client, address = sockfd.accept() • WAITS for a client to establish the connection • client: socket fd for handling the client connection • address: IP address of the client

  17. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  18. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  19. Receive Data data = sockfd.recv(sz, [flags]) • WAITS for data on sockfd • Retrieves up to ‘sz’ bytes of data when available • flags indicate property of recv function- • MSG_DONTWAIT: make recv non-blocking • MSG_PEEK: only peek data; don’t remove from buffer • And many more… (do man recv)

  20. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  21. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  22. Close Socket sockfd.close() Close the connection by sending FIN

  23. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  24. Wait for Input • accept and recv are blocking • Server needs to handle multiple connections • Cannot proceed by blocking on every connection • Need a single function to wait for input on “any” socket fd

  25. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close()

  26. Wait for Input r,w,x = select(rlist, wlist, xlist, [timeout]) • rlist: list of file descriptor to wait on for reading • r: file descriptor ready for reading • wlist: list of file descriptor to wait on for writing • w: file descriptor ready for writing • xlist: list of file descriptor to wait on for exceptions • w: file descriptor ready for exception handling • Waits on any fd in any of these lists until “timeout”

  27. import select import socket import sys server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind((‘ ‘,50000)) server.listen(30) input = [server] while 1: inputready,outputready,exceptready = select.select(input,[],[]) for s in inputready: if s == server: client, address = server.accept() input.append(client) else: data = s.recv(4096) print data server.close() Wait for read on any socket fd in input Add server fd to input list Handle read on server by ‘accept’ Add new client fd in input list Handle read on client by ‘recv’

  28. Client Program

  29. import socket import sys s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((‘10.0.0.1’, 50000)) s.send(“Hello Server!”) s.close()

  30. Summary

  31. 1.create Client Server 2. bind 1.create 3. listen 3-way handshake 2. connect 4. accept data 3. send 5. recv

More Related