1 / 20

Socket Programming in C

Socket Programming in C. Professor: Dr. Shu-Ching Chen TA: Hsin -Yu Ha. What is socket?. An interface between application processes An Application Programming Interface (API) used for InterProcess Communications (IPC) Sockets bound to some IP and port number

Download Presentation

Socket Programming in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Socket Programming in C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha

  2. What is socket? • An interface between application processes • An Application Programming Interface (API) used for InterProcess Communications (IPC) • Sockets bound to some IP and port number • The socket library provides various system calls Examples: socket(), bind(), listen(), connect(), accept(),send(), recv(), close()

  3. Client-server paradigm • Client: • initiates contact with server (“speaks first”) • typically requests service • from server, • For Web, client is implemented in browser; • Server: • provides requested service to client • typically requests service from server, • e.g., Web server sends requested Web page.

  4. Types of socket (1) • 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

  5. Types of socket (2) • Stream sockets – connection-oriented (TCP)

  6. Types of socket (3) • Datagram sockets- connectionless socket (UDP)

  7. 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);

  8. 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(intsockfd, structsockaddr *my_addr, intaddrlen);

  9. 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);

  10. 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 tostructsockaddr that contains information on destination IP address and port • Addrlen: It is set to sizeof(structsockaddr) int connect(intsockfd, structsockaddr *serv_addr, intaddrlen);

  11. 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 structsockaddr which stores the information about incoming connection • Addrlen: It is set to sizeof(structsockaddr_in) int accept(intsockfd, structsockaddr *addr, int *addrlen);

  12. 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(intsockfd, const void *msg, intlen, int flags);

  13. 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 intrecv(intsockfd, void *buf, intlen, unsigned int flags);

  14. 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(intsockfd, inthow);

  15. Primary Socket Calls (9) • Close() : Close connection corresponding to the socket descriptor and frees the socket descriptor. int close(intsockfd)

  16. example – server (1)

  17. example – server (2)

  18. example – Client (1)

  19. example – client (2)

  20. 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

More Related