Presentation is loading. Please wait.

Presentation is loading. Please wait.

Socket Programming in C

Similar presentations


Presentation on theme: "Socket Programming in C"— Presentation transcript:

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

2 What is socket? An interface between an application process and transport layer. An Application Programming Interface (API) used for InterProcess Communications (IPC) It can also be called as Berkeley Socket or BSD Socket – The application process can send/receive messages to/from another application process (local or remote)via a socket - An interface through which processes can send /receive information

3 Types of socket (1) Address domain Two types of internet Sockets
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 Stream sockets use TCP (Transmission Control Protocol), which is a reliable, stream oriented protocol, and datagram sockets use UDP (Unix Datagram Protocol), which is unreliable and message oriented.

4 Types of socket (2) Stream sockets – connection-oriented (TCP)
Connection–based sockets communicate client-server: the server waits for a connection from the client

5 Types of socket (3) Datagram sockets- connectionless socket (UDP)

6 TCP-based sockets **** Sever **** socket: create the socket
bind: give the address of the socket on the server listen: specifies the maximum number of connection requests that can be pending for this process accept: establish the connection with a specific client send,recv: stream-based equivalents of read and write (repeated) shutdown: end reading or writing close: release kernel data structure **** Client **** connect: connect to a server send,recv: (repeated) shutdown close

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); The type argument can be: I SOCK STREAM: Establishes a virtual circuit for stream I SOCK DGRAM: Establishes a datagram for communication I SOCK SEQPACKET: Establishes a reliable, connection based, two way communication with maximum message size. (This is not available on most machines.) protocol is usually zero, so that type defines the connection within domain. c Mani Radhakrishnan and

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(int sockfd, struct sockaddr *my_addr, int addrlen); sockfd is the socket descriptor returned by socket() – my_addr is pointer to struct sockaddr that contains information about your IP address and port – addrlen is set to sizeof(struct sockaddr) – returns -1 on error – my_addr.sin_port = 0; //choose an unused port at random – my_addr.sin_addr.s_addr = INADDR_ANY; //use my IP adr

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); listen for connections on a socket SYNOPSIS Where size it the number of pending connection requests allowed (typically limited by Unix kernels to 5).

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 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); Specifies the destination to form a connection with (addrPtr), and returns a 0 if successful, -1 otherwise.

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 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); if lenPtr or addrPtr equal zero, no address structure is returned. lenPtr is the maximum size of address structure that can be called, returns the actual value. Waits for an incoming request, and when received creates a socketfor it.

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(int sockfd, const void *msg, int len, int flags); flag is either I 0: default I MSG OOB: Out-of-band high priority communication

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 int recv(int sockfd, void *buf, int len, unsigned int flags); returns the number of bytes actually read into the buffer or -1 on error returns 0, the remote side has closed connection on you flags can be either I 0: default I MSG OOB: out-of-bound message I MSG PEEK: look at message without removing

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(int sockfd, int how);

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

16 example – server (1) read() is equivalent to recv() with a flags parameter of 0. Other values for the flags parameter change the behaviour of recv(). Similarly, write() is equivalent to send() with flags == 0.

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/share /S5958v2.pdf Beej's Guide to Network Programming Using Internet Sockets Sockets Tutorial


Download ppt "Socket Programming in C"

Similar presentations


Ads by Google