S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
-
Upload
derick-harris -
Category
Documents
-
view
213 -
download
1
Transcript of S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
![Page 1: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/1.jpg)
SOCKET PROGRAMMING IN CProfessor: Dr. Shu-Ching Chen
TA: Hsin-Yu Ha
![Page 2: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/2.jpg)
WHAT IS SOCKET?
An interface between an application process An Application Programming Interface (API)
used for InterProcess Communications (IPC) It can also be called as Berkeley Socket or
BSD Socket
![Page 3: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/3.jpg)
TYPES OF SOCKET (1)
Address domain Unix domain : address format is Unix pathname Internet domain : address format is host and port
number
Two types of internet Sockets Stream sockets SOCK_STREAM
Connection oriented Rely on TCP to provide reliable two-way connected
communication Datagram sockets SOCK_DGRAM
Rely on UDP Connection is unreliable
![Page 4: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/4.jpg)
TYPES OF SOCKET (2)
Stream sockets – connection-oriented (TCP)
![Page 5: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/5.jpg)
TYPES OF SOCKET (3)
Datagram sockets- connectionless socket (UDP)
![Page 6: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/6.jpg)
TCP-BASED SOCKETS
![Page 7: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/7.jpg)
PRIMARY SOCKET CALLS (1)
Socket() : Returns a file descriptor(socket ID) if successful, -1 otherwise.
Arguments Domain: set to AF_INET Type:
SOCK_STREAM SOCK_DGRAM SOCK_SEQPACKET
Protocol: If it is set as zero, then socket will choose the correct protocol based on type.
int socket(int domain, int type, int protocol);
![Page 8: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/8.jpg)
PRIMARY SOCKET CALLS (2)
Bind() : Associate a socket id with an address to which other processes can connect.
Arguments Sockfd: It is the socket id My_addr: a pointer to the address family
dependent address structure Addrlen: It is the size of *my_addr
int bind(int sockfd, struct sockaddr *my_addr, int addrlen);
![Page 9: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/9.jpg)
PRIMARY SOCKET CALLS (3)
Listen() : Return 0 on success, or –1 if failure Arguments
Sockfd: It is socket id created by socket() Backlog : It is used to constraint the number of
connection
int listen(int sockfd, int backlog);
![Page 10: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/10.jpg)
PRIMARY SOCKET CALLS (4)
Connect() : connect to a remote host Arguments
Sockfd: It is the socket descriptor returned by socket()
serv_addr : It is a pointer to to struct sockaddr that contains information on
destination IP address and port Addrlen: It is set to sizeof(struct sockaddr)
int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);
![Page 11: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/11.jpg)
PRIMARY SOCKET CALLS (5)
Accept() : gets the pending connection on the port you are listen()ing on.
Arguments Sockfd: It is the same socket id used by listen() Addr: It is a pointer to a local struct sockaddr
which stores the information about incoming connection
Addrlen: It is set to sizeof(struct sockaddr_in)
int accept(int sockfd, struct sockaddr *addr, int *addrlen);
![Page 12: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/12.jpg)
PRIMARY SOCKET CALLS (6)
Send() : Send a message. Returns the number of bytes sent or -1 if failure.
Arguments Sockfd: It is the same socket id used by socket()
or accept() msg: It is the pointer to the data you want to
send Len: data length is sizeof(msg) Flags : It is set to be zero
int send(int sockfd, const void *msg, int len, int flags);
![Page 13: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/13.jpg)
PRIMARY SOCKET CALLS (7)
recv() : Receive up to len bytes in buf. Returns the number of bytes received or -1 on failure.
Arguments Sockfd: It is the socket descriptor to read from buf: It is the buffer to read the information info Len: It is the maximum length of the buffer Flags : It is set to be zero
int recv(int sockfd, void *buf, int len, unsigned int flags);
![Page 14: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/14.jpg)
PRIMARY SOCKET CALLS (8)
shutdown() : disable sending or receiving based on the value how.
Arguments Sockfd How
Set it to 0 will disable receiving Set it to 1 will disable sending Set it to 2 will disable both sending and receiving
int shutdown(int sockfd, int how);
![Page 15: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/15.jpg)
PRIMARY SOCKET CALLS (9)
Close() : Close connection corresponding to the socket descriptor and frees the socket descriptor.
int close(int sockfd)
![Page 16: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/16.jpg)
EXAMPLE – SERVER (1)
![Page 17: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/17.jpg)
EXAMPLE – SERVER (2)
![Page 18: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/18.jpg)
EXAMPLE – CLIENT (1)
![Page 19: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/19.jpg)
EXAMPLE – CLIENT (2)
![Page 20: S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.](https://reader035.fdocuments.net/reader035/viewer/2022072005/56649ce35503460f949af45d/html5/thumbnails/20.jpg)
REFERENCE http://home.iitk.ac.in/~chebrolu/ee673-f06/sockets.pdf ftp://
ftp.sas.com/techsup/download/SASC/share5958-59/S5958v2.pdf
http://www.slideshare.net/jignesh/socket-programming-tutorial
http://www.rites.uic.edu/~solworth/sockets.pdf Beej's Guide to Network Programming Using Internet
Sockets http://beej.us/guide/bgnet/output/html/multipage/index.html
Sockets Tutorial http://www.linuxhowtos.org/C_C++/socket.htm