Socket Programming
Socket: Door between application process and end to end transport protocol
Each application is run as a process in the OS of the end host. For the two process to communicate with each other, they will need to read and write throguh a programming abstraction called a socket. The application developer has the control of one side of the socket which is at the applicaition layer but has little control over the other side of the socket which is implemented by the transpoert layer services.
The socket is use to utilized the underlying transport layer services.
<insert slide 8>
Client want to contact an server running the apache server
1. Both must have ip adderss
2. Both processes have to talk to each other though the socket
3. Both process have TCP sockets.
4. The port number is use to further identify which process is calling.
Port number:
16 bit integer
1-1023 are reserved for standard use.
Create sockets:
- Corresponding port number
- Define a destination address
(Ip address + port number)
There are only 2 types of services,
- UDP
Unreliable datagram, connectionless
The packet is define in transport layer
- TCP
Reliable service, provides abstraction of byte stream
It is a stream of bytes.
Connection oriented
App example:
1. The client read a line of data from keyboard and send data to server
1. The client read a line of data from keyboard and send data to server
2. server receives the data and convert the chara to upper case
3. The server sened modified data
4. The client receives and display the line on screen
UDP Sockets
- Connectionless = do not need to do handshaking before communication
- Sending packet to server: Explicitly attach the IP address and the port number to packet
(The IPadress and port number can auto identify the process)
- Receiver receives: Knows the tIP address and port of the sender (client)
- Receiver reply: Try to extract the ip and port number from the received packet to send it back
- Receiver sends: Also try to attach IP address
However the UPD transmitted data may lost or recieve out of order.
Application viewpoint:
Unreliable transfer of datagram (Packets)
Note: DNS service use UDP as its transport protocol
The server process only use one socket to serve all the different client and queries from different client
1. Server is running
Specify the port number for server socket
serverSocket = socket(AF_INET,SOCK_DGRAM)
2. Client need to create socket and a packet
clientSocket = socket(AF_INET,SOCK_DGRAM)
Create a packet with server ip and port. Send packet through clientSocket
3. Server reply
Write reply trough serverSocket
Specify the client address and port number
4. Once client receives, it can close its socket
Code example:
Client
<Insert slide 13>- from socket import * (Import)
- clientSocket = socket(AF_NET, SOCK_DGRAM) //Create UDP socket for server at client side
//we can get the ip address by invoking the dns
//AF_NET = gives the address family (Default IPV4)
//SOCK_DGRAM = tells the type of the datagram , this says that it is a UDP
- message = input("dqdqwdwd")
- clientSocket.sendto(message.encode(), serverName, serverPort) //sends this message to this dest
//encode convert the string to a string of bytes, an array of bytes
- modifiedMessage, serverAddress = clientSocket.recvfrom(2048) //recieve from the socket
//will return two parameters
//modifiedMessage is the message recieved from somebody
//Serveraddress is the ipaddress and port number
- print(modifiedMessage.decode()) //decoding the modified message backt string
- clientSocket.close() //closing the socket
Server
<Insert slide 14>
- serverSocket = socket(AD_INET, SOCK_DRAM)
//same as client
//Specify UPD
- serverSocket.bind(("", serverPort))
//tries to bind a server port number to this socket
// "" says it is a binding method
// serverPort specify the number
//reason for binding: Client's port number do not need to worry because the OS will decide for you, we can still bind it nevertheless if we want. (OS will auto assign if we did not specify)
-while True //goes into an infinite loop
- message, clientAddress = serverSocket.recvfrom(2048)
//same as client
- serverSocket.sendto(modifiedMessage.encode(), clientAddress)
//encode the changed message
//sends the back to clientAddress
Socket Programming with TCP (Welcome socket)
- Client must contact server
- Server must be running already
- Server must have create a socket that welcomes client's contact
This socket is different from UDP
We need specify when we start creating the socket.
TCP client need to establish a connection to the TCP server
Server side
Must have a welcome side
Each contacting client, server need to create a socket for each
Server have multiple sockets and need to identify which client belongs to which socket
Source port number use to identify
Application viewpoint:
- Reliable pipe of bytestream transfer
- Reliable pipe of bytestream transfer
1. TCP establish connect
2. When connection establish, data flows between in continuous stream
3. When contacted by client, server TCP creates a new socket for server process to communicate with clients
Each client got its own specific socket (Connection socket)
Server use source IP address and port number to identify which socket belongs to who
1. Server creates a socket, waits for incoming connection
connectionSocket = serverSocket.accepts()
2. Client create a socket
clientSocket = socket()
3. Send request using clientSocket
4. Read request from connection socket
5. Reply to conectionSocket
6. read reply from clientsocket
7. Both sides will close the connection
Client side
<insert slide 18>
- clientSocket = socket(AD_INET, SOCK_STREAM)
//SOCK_STREAM says its TCP
- clientSocket.connect((serverName, serverPort))
//tries to do a connection first
//specify the server and the port
//invokes the handshaking and builds the TCP connection
- clientSocket.send(sentence.encode())
//convert to byte stream
//no need to attach server name and port, this is because of connect method
- modifiedSentence = clientSocket.recv(1024)
//we know it is recieve from
Server side
<Insert slide 19>
- serverSocket = socket(AF_INET, SOCK_STREAM)
- serverSocket.listen(1)
//this says that it is a welcome socket that listen to incoming request
//specify the max number of request it can serve simultaneously (here its 1)
-while TRUE: //infinite loopp
- connectionSocket, addr = serverSocket.accept()
//some request and we will accept this connection by creating a connectionSocket
//we will get the client address as well
- sentence = connectionSocket.recv(1024).decode()
//1024 is buffer size
// we recieve the stream of bytes and we call the decode to translate into string
- connectionSocket.send(captilizedsentence.encode())
//send the encoded sentence
- connectionSocket.close
//close the socket
If we want the server to serve like 100 client, we can change the number 1 in listener to 100. But in order to serve concurrently, it depends on the libraries and programming languages that are unique to multithreading.
For each simultaneous, we have to create a new thread.
Unfortunately, for python, there is no such multithreading. We have to use other languages.
UDP socket vs TCP socket
UDP Socket
- Only one socket for all client (Server)
- No connection establish (both sides)
- Sender have to explicitly attach destinationIP address and port (client)
- Unreliable datagram: May lost or out of order (Client)
TCP Socket
- Welcome socket for all request (server)
- Each accepted client will have a connection socket each (server)
- Connection establish (both sides)
- Reliable stream pipe: Data guaranteed to be received in order (client)
No comments:
Post a Comment