Socket programming in C

778 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
778
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
50
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Socket programming in C

  1. 1. Socket Programming in C connecting processes Dipak Kumar Swain MCA 4th Semester Regd No-1105227024
  2. 2. Overview • Introduction to Sockets • A generic Client-Server application • Types of Socket • Socket APIs • Socket Programming with TCP • Socket Programming with UDP • Conclusion • References 2/11
  3. 3. Introduction to Sockets What is a Sockets? • Socket is an interface between application and network which is used for communication between processes • Data can be sent to or received through a socket from another process running on the same machine or a different machine • Socket internally represents the three things: – Protocol – IP address – Port number 3/11
  4. 4. Client Server Application Most interprocess communication uses client-server model • • • Server waits for client to request a connection and Client contacts server to establish a connection. Client sends request and Server sends reply. Client and/or server terminate connection. 4/11
  5. 5. Client Server Application • What makes a connection? {Source<IP address, Port #> , Destination <IP address, Port #>} i.e. source socket – destination socket pair uniquely identifies a connection. • Example 1343 Client 192.168.0.2 Server 80 1343 Client 192.168.0.3 192.168.0.1 5488 Client 192.168.0.2 5/11
  6. 6. Types of Socket Two essential types of sockets : – STREAM Socket – Treat communications as a continuous stream of characters. – DATAGRAM Socket– Read entire messages at once. Stream Socket(SOCK_TCP) Datagram Socket(SOCK_DGRAM) TCP UDP Connection Oriented Connection Less Reliable Delivery Unreliable Delivery In-Order Guaranteed No-Order Guaranteed Bidirectional Can Send Or Receive 6/11
  7. 7. Socket APIs Socket System Call – create an end Socket System Call UDP Socketend Some Essential System calls used byCall –Bind createaddress TCP & – an IP an Bind Systemfor communication Bind System Call –Bind an IP address point socket for communication point socket Listen System Call:– Used by Server Listen System Call:– Used by • socket ( ): and port number with a socket Server and port<sys/socket.h> socket number with a #includein TCP for passively waiting #includeSystem for passively waiting <sys/socket.h> Connect in TCP Call – Initiates a Connect Process System Call – Initiates a Process #include <sys/socket.h> #include <sys/socket.h> • bind( ): Accept SystemTCPSocket. a Accept SystemCall:– Accept a connection on TCP Socket. type, int connection on domain, int for connection domain, int type, int for connection Call:– Accept int socket(intCall– Send data to int socket(int Send Systemsockfd, Send data to Send SystemCallclient connection from client socket. connection from – struct int bind(<sys/socket.h>socket. int int bind(<sys/socket.h> • listen( ): #includeTCP socket. Receive data from #includeTCP sockfd, struct protocol);intsocket.Receive data from protocol); Call – Recv System Call – Recv System another <sys/socket.h> addrlen); another *serv_addr, int sockaddr<sys/socket.h> addrlen); #includeSystemCall – Send datagram sockaddr *serv_addr,Send int #include Sendto System Call integer datagram Sendto TCPunsigned –intstruct another an unsigned integercalled another <sys/socket.h> backlog); int listen( intintsockfd, struct • connect( ): intconnect( int sockfd, listen( intsockfd, sockfd, Returns TCPsocket. intbacklog); connect(socket. Returns <sys/socket.h> #include anSystemCallstructcalled #include System Call– Receive Recvfromonintsockfd, struct Recvfrom*addr, intthe specified UDP socket. to the specified sockfd, – Receive intaccept(*addr, intsocket. to accept(int UDP addrlen); #include0<sys/socket.h> Returnsdescriptorint addrlen); #include on Call sockaddr<sys/socket.h> the Returns 0on success socketSystemsockfd,Closes*msg, int sockaddr int success void socketdescriptor – void the Returns 0 int success *addrlen); int Close Systemsockfd,Closes*msg, • accept( ): int send( packet from*addrlen); int Returns 0 packet from void *msg, Close int send( on success void *msg, datagram *addr, int another UDP int datagram *addr, int sockaddrintintCall – void *msg, int sockaddr sockfd, int sendto( intsockfd, another UDP int sendto( sockfd, int recv( intsockfd, void *msg, int int recv( on success communication channel between size, int 0 on flags, between communication Returns flag);success struct sockaddr size, int 0 int Returns socket flag); flags, struct sockaddr socket a non-negative descriptor • send( ): noOfbytes, int channeldescriptoron noOfbytes, Returns server. size, int flag); Returns server. size, int flag); client & numbersockfd,void *msg,senton client & a non-negative *msg,int on Returns*sock_length); Returns number of characters int int recvfrom( int sockfd, void int int *to,recvfrom( int of characters sent on *to, int success *sock_length); success • recv( ): Returns number of characters sent on Returns int flags,struct sockaddr *from, noOfbytes,number of characters sent on int close(intflags,of bytes written*from, noOfbytes, int close(intsockfd); sockfd); success number of bytes writtento success number struct Returnsorint on error sockaddr to Returnsor-1 on error success int *sock_length); success -1 int *sock_length); • sendto( ): Returnonon success & sockfd is the Returnonsuccess or & on error socket 0 on success -1 sockfd is the socket 0 success or -1 on error Returnsdescriptorofbyteswill befrom Returnsnumber which read from socket descriptorofwhich will be socket number bytes read • recvfrom( ): socket on success or -1 on error socket closed. on success or -1 on error closed. • close( ): 7/11
  8. 8. Socket Programming With UDP Client Server socket socket bind sendto recvfrom Request Response recvfrom sendto close 8/11
  9. 9. Socket Programming With TCP Client Server socket socket bind listen connect Connect 3-way handshake accept send/recv recv/send close close 9/11
  10. 10. Conclusion Not only message can send but also a file can send through Socket. To handle multiple client simultaneously , we can use the following models at server side: Process Model Thread Model Worker Pool Model 10/11
  11. 11. References • Cystem Programming at the Roots… By Susant K Rout and T Prabakaran • UNIX Network Programming, by Richard Stevens. • LINUX man page Accessible through following command – man 2 <system_call_name> – E.g. man 2 socket 11/11

×