Download presentation
1
Tutorial 8 Socket Programming
Internet Networking Tutorial 8 Socket Programming
2
What is socket? First appeared in 4.1 BSD UNIX 1982.
An integer associated with a network connection. Implemented as file descriptor. Types of Internet Sockets: Stream Sockets for TCP connection. Datagram Socket for UDP communication. Raw Sockets.
3
Socket Interface User Space App1 App2 Socket I/F IP Stack Kernel ND1
4
Structures ‘Struct sockaddr’ holds socket address information:
unsigned short sa_family; // AF_INET for internet char sa_data[14]; // 14 bytes of protocol address. contains a destination address and port number for the socket. };
5
Structures ‘struct sockaddr_in’ is used to deal with ‘struct sockaddr’ for internet family (AF_INET): struct sockaddr_in { short int sin_family; // AF_INET for internet unsigned short int sin_port; // Port number struct in_addr sin_addr; // Internet address unsigned char sin_zero[8]; // padding (must be zero) }; A pointer to a ‘struct sockaddr_in’ can be cast to a pointer to a ‘struct sockaddr’ and vice-versa, since they both have the same size.
6
Structures In order to set the IP address one should use ‘struct in_addr’: struct in_addr { unsigned long s_addr; // IPv4 address };
7
Bytes Order The CPU and the NIC may store and read data from the memory in a different order. Memory CPU R/W R/W NIC
8
Bytes Order Two ways to store data in the memory:
Big-Endian Little-Endian One should convert the order of the bytes (if necessary) in order to transmit them in network in the same way.
9
Bytes Order Accessories Function:
htons(x) – Convert short from host to network order. htonl(x) ntohs(x) ntohl(x) In order to be portable one should use this function even if not necessary (i.e. in a little-endian machine).
10
System Calls - Socket int socket(int domain, int type, int protocol);
Return a file descriptor (or -1 in case of error). domain: AF_INET for internet. type: SOCK_STREAM for TCP, SOCK_DGRAM for UDP and SOCK_RAW for raw socket.
11
System Call - Bind int bind(int sockfd, struct sockaddr *my_addr, int addrlen); ‘sockfd’ is the socket file descriptor. Associate a socket with a port on the local machine (usually used by servers). ‘my_addr’ is a pointer to a struct sockaddr that contains information about the port. ‘addrlen’ is set to sizeof(struct sockaddr). return 0 on success or -1 on failure (when?).
12
System Call - Connect int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); Used by clients to connect a socket to a server. ‘sockfd’ is the socket file descriptor. ‘struct sockaddr’ contains the destination port and IP address ‘addrlen’ can be set to sizeof(struct sockaddr).
13
System Call - Listen int listen(int sockfd, int backlog);
Define the maximum of pending TCP connections. ‘sockfd’ is the socket file descriptor. ‘backlog is the number of pending connections.
14
System Call - Accept int accept(int sockfd, void *addr, int *addrlen);
The accept system call is used by a TCP server to accept pending connection. Blocked until a connection is arrived. Return a new socket file descriptor that is used to receive and send data on this connection. ‘sockfd’ is the socket file descriptor. ‘addr’ contains information (port and IP address) of the incoming connection. ‘addrlen’ is a local integer variable that should be set to sizeof(struct sockaddr_in).
15
Data Transfer int send(int sockfd, const void *msg, int len, int flags); Transmit data to through a socket. Can be used only when the socket is connected. Return the number of bytes that has been sent (or -1 in case of error). ‘buf’ is a pointer to the buffer to be sent, while ‘len’ is its length.
16
Data Transfer int recv(int sockfd, void *buf, int len, unsigned int flags); Obtain data from the socket. Used only when the socket is in a connected state. Return the number of bytes that has been obtailed input (or -1 in case of error). ‘buf’ is a pointer to the buffer for holding the received data and ‘len’ is its length.
17
Data Transfer int sendto(int sockfd, const void *msg, int len, unsigned int flags, const struct sockaddr *to, int tolen); Transmit data to through a socket. The destination (port and IP address) is specified in the call. Used for UDP communication.
18
Data Transfer int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen); Obtain data from the socket. ‘sockaddr’ contains information about the originator of the data. Used for UDP communication.
19
Data Transfer Since ‘send’ and ‘recv’ are used in a connected mode, the usually used in TCP connection while ‘recvfrom’ and ‘sendto’ are used for UDP communication.
20
Other Services Dealing with IP Addresses DNS
ina.sin_addr.s_addr = inet_addr(" "); printf("%s", inet_ntoa(ina.sin_addr)); DNS int gethostname(char* name, int namelen) struct hostent *gethostbyname(char* name)
21
Client-Server Model UDP Client sd=socket(…) sendto(sd,…)
recvfrom(sd,…) close(sd) UDP Server sd=socket(…) bind(sd,…) recvfrom(sd,…) sendto(sd,…) close(sd)
22
Client-Server Model TCP Client sd=socket(…) connect(sd) send(sd,…)
recv(sd,…) close(sd) TCP Server sd=socket(…) bind(sd,…) listen(sd,…) new_sd=accept(sd,…) fork() recv(new_sd,…) send(new_sd,…) close(new_sd)
23
Port Assignment Ports 0 through 1023 are reserved.
In order to let the system to assign the port number, one should set the port number (e.g. in ‘bind’) to zero.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.