Socket
- Interface between the application layer and transport layer
- OS controlled interface into which application process can both send and recieve messages
Addressing:
- Host address and process identifier
- Ip address + Port number
Socket is an endpoint for communication. A combination of host IP address and port number
Socket types
UDP sever: Connectionless (Iterative)
- All wil go to the same port
- ALl will go to the same queue
We can pqaralised
TCP server: Connection oriented (COncurrent)
- Create a seperate child process
- Child process may use same or new port
- The port is that dedicated to that client
- The next client will then go to the next child server
- Given that each socket is identified by local ip and port and remote ip and port, no need for emphemeral port at server sides (Uniquely identifieable)
- Can be iterative but now majority are concurrent
Socket Data structure
UNIX LINUX SOCKET API (C/C++)
Creating a socket
sockDes = new Socket(pf, type, protocol)
- pf : protocol family
- type : SOCK_STREAM for TCP, SOCK_DGRAM for UDP, SOC_SEQPACKET for sctp, SOCK_RAW only for priviledge programs
- Protocol : Is always 0 by default.. There was suppose to be more protocol but until it still the same
Binding
- `bind(sockDes, localaddr, addrlen)
- socket will have a wild card foreign destination
- localaddr is pointer to a structure defined in socket.h which specifies the local socket address
UDP Server
- sockDes = new Socket(pf, type, protocol)
- bind(sockDes, localaddr, addrlen)
- socket will have wild-card foreign destination; foreign addr is yet to be assigned
- rescfrom(sockDes, buffer, length,flags, fromaddr, addrlen)
- OS will record the fromaddr and addlen based on datagram recievedf
- sendto(sockDes, message, length, flags, destaddr, addrlen)
- close(sockDes)
UDP Client
- sockDes = new socket(pf, type, protocol)
- sendto(sockDes, message, length, flag, destaddr, addrlen)
- recvfrom(sockDes, buffer, length, flags, fromaddr, addrlen)
- close(sockDes)
Bind is optional.. our client could take a random port address
TCP Server
- sockDes = new socket(pf, type, protocol)
- bind(sockDes, localaddr, addrlen)
- socket will have wild-card foreign destination; foreign addr is yet to be assigned
- listen(sockDes, qlength)
- newSockDes = accept(sockDes, addr, addrlen)
- new socket will have requesting client as dest and returns newSockDes to server, connection is establised to specific client
- read/write(newSockDes, buffer, length)
- close(sockDes); close(newSockDes)
TCP Client
- sockDes = new socket(pf, type, protocol)
- connect(sockDes, destaddr, addrlen)
- read/write(sockDes, buffer, length)
- close(sockDes)
Raw socket
See the slides for commands