120 likes | 456 Views
Socket Programming with TCP and UDP. Julia Ljunbjörk and Anita Mugenyi. What is a socket ?. Like a house Between the layers. Socket programming. Client –server Two ways. Socket programming.
E N D
SocketProgramming with TCP and UDP Julia Ljunbjörk and Anita Mugenyi
What is a socket? • Like a house • Between the layers
Socketprogramming • Client –server • Twoways
Socketprogramming • A client reads a line from its standard input (keyboard) and sends the lineoutitssocket to the server • The server reads a line from itssocket • The server converts the line to uppercase • The server sends the modifiedlineoutitssocket to the client • The client reads the modifiedline from itssocket and prints the line on its standard output(monitor)
host or server host or server process process socket socket TCP with buffers, variables TCP with buffers, variables controlled by app developer Internet
Socketprogramming with TCP • Reliable, byte stream-oriented • Three-way handshake • Three sockets • Streams
create socket, connect to hostid, port=x create socket, port=x, for incoming request: clientSocket = Socket() welcomeSocket = ServerSocket() TCP connection setup wait for incoming connection request connectionSocket = welcomeSocket.accept() send request using clientSocket read request from connectionSocket write reply to connectionSocket read reply from clientSocket close connectionSocket close clientSocket Server (running on hostid) Client
Socketprogramming with UDP • No streams • No connection • Datagram socket • Individual packets
Client create socket, clientSocket = DatagramSocket() Create datagram with server IP and port=x; send datagram viaclientSocket read datagram from serverSocket write reply to serverSocket specifying client address, port number read datagram from clientSocket close clientSocket Server (running on hostid) create socket, port= x. serverSocket = DatagramSocket()