SlideShare a Scribd company logo
1 of 35
Socket Programming Jignesh Patel Palanivel Rathinam connecting processes
Overview ,[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
Introduction to Sockets ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],Socket
Introduction to Sockets ,[object Object],[object Object],[object Object],[object Object],[object Object],Server Client Client 192.168.0.1 192.168.0.2 192.168.0.2 80 1343 5488 Client 192.168.0.3 1343
Introduction to Sockets ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
A generic TCP application ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
A generic UDP application ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
Programming Client-Server in C ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Programming TCP Client in C #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h>  void error(char *msg){  perror(msg);  exit(0);} int main(int argc, char *argv[]){  int sockfd, portno, n;  struct sockaddr_in serv_addr;  struct hostent *server;  char buffer[256];  if (argc < 3) {   fprintf(stderr,&quot;usage %s hostname port&quot;, argv[0]);  exit(0);  }  portno = atoi(argv[2]);  sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);  if (sockfd < 0)  error(&quot;ERROR opening socket&quot;);  /* a structure to contain an internet address  defined in the include file <netinet/in.h> */ struct sockaddr_in { short  sin_family; /* should be AF_INET */ u_short sin_port; struct  in_addr sin_addr; char  sin_zero[8]; /* not used, must be zero */ }; struct in_addr { unsigned long s_addr; };  Client.c
Programming TCP Client in C #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h>  void error(char *msg){  perror(msg);  exit(0);} int main(int argc, char *argv[]){  int sockfd, portno, n;  struct sockaddr_in serv_addr;  struct hostent *server;  char buffer[256];  if (argc < 3) {   fprintf(stderr,&quot;usage %s hostname port&quot;, argv[0]);  exit(0);  }  portno = atoi(argv[2]);  sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);  if (sockfd < 0)  error(&quot;ERROR opening socket&quot;);  Client.c Socket System Call  – create an end point for communication #include <sys/types.h> #include <sys/socket.h> int socket(int  domain , int  type , int  protocol ); Returns a descriptor domain : selects protocol family  e.g. PF_IPX, PF_X25, PF_APPLETALK type : specifies communication semantics e.g. SOCK_DGRAM, SOCK_RAW protocol : specifies a particular protocol to be used e.g. IPPROTO_UDP, IPPROTO_ICMP
Programming TCP Client in C server = gethostbyname(argv[1]);  if (server == NULL) {  fprintf(stderr,&quot;ERROR, no such host&quot;); exit(0);  }  bzero((char *) &serv_addr, sizeof(serv_addr));  serv_addr.sin_family = AF_INET;  bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr , server->h_length);  serv_addr.sin_port = htons(portno);  if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)  error(&quot;ERROR connecting&quot;);  printf(&quot;Please enter the message: &quot;);  bzero(buffer,256);  fgets(buffer,255,stdin);  n = send(sockfd,buffer,strlen(buffer),0);  if (n < 0)  error(&quot;ERROR writing to socket&quot;);  bzero(buffer,256);  n = recv(sockfd,buffer,255,0);  if (n < 0)   error(&quot;ERROR reading from socket&quot;);  printf(&quot;%s&quot;,buffer);  close(sockfd);  return 0; }  Client.c Connect System Call  – initiates a connection on a socket #include <sys/types.h> #include <sys/socket.h> int connect( int  sockfd ,  const struct sockaddr * serv_addr , socklen_t  addrlen ); Returns 0 on success sockfd : descriptor that must refer to a socket serv_addr : address to which we want to connect addrlen : length of  serv_addr
Programming TCP Client in C server = gethostbyname(argv[1]);  if (server == NULL) {  fprintf(stderr,&quot;ERROR, no such host&quot;); exit(0);  }  bzero((char *) &serv_addr, sizeof(serv_addr));  serv_addr.sin_family = AF_INET;  bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr , server->h_length);  serv_addr.sin_port = htons(portno);  if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)  error(&quot;ERROR connecting&quot;);  printf(&quot;Please enter the message: &quot;);  bzero(buffer,256);  fgets(buffer,255,stdin);  n = send(sockfd,buffer,strlen(buffer),0);  if (n < 0)  error(&quot;ERROR writing to socket&quot;);  bzero(buffer,256);  n = recv(sockfd,buffer,255,0);  if (n < 0)   error(&quot;ERROR reading from socket&quot;);  printf(&quot;%s&quot;,buffer);  close(sockfd);  return 0; }  Client.c Send System Call  – send a message to a socket #include <sys/types.h> #include <sys/socket.h> int send( int  s , const void * msg , size_t  len ,  int  flags ); Returns number of characters sent on success s : descriptor that must refer to a socket in connected state msg : data that we want to send len : length of data flags : use default 0. MSG_OOB, MSG_DONTWAIT
Programming TCP Client in C server = gethostbyname(argv[1]);  if (server == NULL) {  fprintf(stderr,&quot;ERROR, no such host&quot;); exit(0);  }  bzero((char *) &serv_addr, sizeof(serv_addr));  serv_addr.sin_family = AF_INET;  bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr , server->h_length);  serv_addr.sin_port = htons(portno);  if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)  error(&quot;ERROR connecting&quot;);  printf(&quot;Please enter the message: &quot;);  bzero(buffer,256);  fgets(buffer,255,stdin);  n = send(sockfd,buffer,strlen(buffer),0);  if (n < 0)  error(&quot;ERROR writing to socket&quot;);  bzero(buffer,256);  n = recv(sockfd,buffer,255,0);  if (n < 0)   error(&quot;ERROR reading from socket&quot;);  printf(&quot;%s&quot;,buffer);  close(sockfd);  return 0; }  Client.c Recv System Call  – receive a message from a socket #include <sys/types.h> #include <sys/socket.h> int recv( int  s , const void * buff , size_t  len ,  int  flags ); Returns number of bytes received on success s : descriptor that must refer to a socket in connected state buff : data that we want to receive  len : length of data flags : use default 0. MSG_OOB, MSG_DONTWAIT
Programming TCP Client in C server = gethostbyname(argv[1]);  if (server == NULL) {  fprintf(stderr,&quot;ERROR, no such host&quot;); exit(0);  }  bzero((char *) &serv_addr, sizeof(serv_addr));  serv_addr.sin_family = AF_INET;  bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr , server->h_length);  serv_addr.sin_port = htons(portno);  if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)  error(&quot;ERROR connecting&quot;);  printf(&quot;Please enter the message: &quot;);  bzero(buffer,256);  fgets(buffer,255,stdin);  n = send(sockfd,buffer,strlen(buffer),0);  if (n < 0)  error(&quot;ERROR writing to socket&quot;);  bzero(buffer,256);  n = recv(sockfd,buffer,255,0);  if (n < 0)   error(&quot;ERROR reading from socket&quot;);  printf(&quot;%s&quot;,buffer);  close(sockfd);  return 0; }  Client.c Close System Call  – close a socket descriptor #include <unistd.h> int close( int  s ); Returns 0 on success s : descriptor to be closed
Programming TCP Server in C #include <stdio.h>  #include <sys/types.h>  #include <sys/socket.h>  #include <netinet/in.h>  void error(char *msg){  perror(msg);  exit(0);} int main(int argc, char *argv[]){  int sockfd, newsockfd, portno, clilen;  char buffer[256];  struct sockaddr_in serv_addr, cli_addr;  int n;  if (argc < 2) { fprintf(stderr,&quot;ERROR, no port provided&quot;); exit(1); }  sockfd = socket(AF_INET, SOCK_STREAM, 0);  if (sockfd < 0) error(&quot;ERROR opening socket&quot;);  bzero((char *) &serv_addr, sizeof(serv_addr));  portno = atoi(argv[1]);  serv_addr.sin_family = AF_INET;  serv_addr.sin_addr.s_addr = INADDR_ANY;  serv_addr.sin_port = htons(portno);  Server.c
Programming TCP Server in C if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)  error(&quot;ERROR on binding&quot;);  listen(sockfd,5);  clilen = sizeof(cli_addr);  newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);  if (newsockfd < 0) error(&quot;ERROR on accept&quot;);  bzero(buffer,256);  n = recv(newsockfd,buffer,255,0);  if (n < 0) error(&quot;ERROR reading from socket&quot;);  printf(&quot;Here is the message: %s&quot;,buffer);  n = send(newsockfd,&quot;I got your message&quot;,18,0);  if (n < 0) error(&quot;ERROR writing to socket&quot;); close(newsockfd); close(sockfd);  return 0;  }  Server.c Bind System Call  – bind a name to a socket #include <sys/types.h> #include <sys/socket.h> int bind( int  sockfd ,  const struct sockaddr * serv_addr , socklen_t  addrlen ); Returns 0 on success sockfd : descriptor that must refer to a socket serv_addr : address to which we want to connect addrlen : length of  serv_addr
Programming TCP Server in C if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)  error(&quot;ERROR on binding&quot;);  listen(sockfd,5);  clilen = sizeof(cli_addr);  newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);  if (newsockfd < 0) error(&quot;ERROR on accept&quot;);  bzero(buffer,256);  n = recv(newsockfd,buffer,255,0);  if (n < 0) error(&quot;ERROR reading from socket&quot;);  printf(&quot;Here is the message: %s&quot;,buffer);  n = send(newsockfd,&quot;I got your message&quot;,18,0);  if (n < 0) error(&quot;ERROR writing to socket&quot;); close(newsockfd); close(sockfd);  return 0;  }  Server.c Listen System Call  – listen for connections on  a socket #include <sys/types.h> #include <sys/socket.h> int listen( int  s , int  backlog ); Returns 0 on success s : descriptor that must refer to a socket backlog : maximum length the queue for completely established sockets waiting to be accepted addrlen : length of  serv_addr
Programming TCP Server in C if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)  error(&quot;ERROR on binding&quot;);  listen(sockfd,5);  clilen = sizeof(cli_addr);  newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);  if (newsockfd < 0) error(&quot;ERROR on accept&quot;);  bzero(buffer,256);  n = recv(newsockfd,buffer,255,0);  if (n < 0) error(&quot;ERROR reading from socket&quot;);  printf(&quot;Here is the message: %s&quot;,buffer);  n = send(newsockfd,&quot;I got your message&quot;,18,0);  if (n < 0) error(&quot;ERROR writing to socket&quot;); close(newsockfd); close(sockfd);  return 0;  }  Server.c Accept System Call  – accepts a connection on a socket #include <sys/types.h> #include <sys/socket.h> int accept( int  sockfd ,  const struct sockaddr * addr , socklen_t  addrlen ); Returns a non-negative descriptor on success sockfd : descriptor that must refer to a socket addr : filled with address of connecting entity addrlen : length of  addr
Programming UDP Client in C ,[object Object],[object Object],[object Object],[object Object],[object Object],sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); len = sizeof(struct sockaddr_in); while (1) {  /* write */ n = sendto(sock,“Got your message&quot;,17, 0,(struct sockaddr *) &server, len);  f (n < 0) error(&quot;sendto&quot;);  /* read */ n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from, len);  if (n < 0) error(&quot;recvfrom&quot;); }
Programming UDP Server in C ,[object Object],[object Object],[object Object],[object Object],sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); len = sizeof(struct sockaddr_in); while (1) {  /* read */ n = recvfrom(sock,buf,1024,0,(struct sockaddr *)&from, len);  if (n < 0) error(&quot;recvfrom&quot;); /* write */ n = sendto(sock,&quot;Got your message&quot;,17, 0,(struct sockaddr *)&from, len);  f (n < 0) error(&quot;sendto&quot;);  }
Programming Client-Server in C ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],#include <winsock.h> … .. void main(int argc,char *argv[]){ WSADATA wsda; // if this doesn’t work // WSAData wsda; // then try this WSAStartup(0x0101,&wsda); … .. WSACleanup(); closesocket(sockfd); }
[object Object]
Programming TCP Client-Server in Java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Programming TCP Client-Server in Java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Programming TCP Client-Server in Java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Programming TCP Client-Server in Java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Programming TCP Client-Server in Java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Programming TCP Client-Server in Java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Programming UDP Client-Server in Java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Programming UDP Client-Server in Java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Programming UDP Client-Server in Java ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
References ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

More Related Content

What's hot

Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket ProgrammingVipin Yadav
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++Marco Izzotti
 
network programing lab file ,
network programing lab file ,network programing lab file ,
network programing lab file ,AAlha PaiKra
 
Maker All - Executive Presentation
Maker All - Executive PresentationMaker All - Executive Presentation
Maker All - Executive PresentationDiogoFalcao
 
focuslight-validator validate sinatra application - validation night at LINE ...
focuslight-validator validate sinatra application - validation night at LINE ...focuslight-validator validate sinatra application - validation night at LINE ...
focuslight-validator validate sinatra application - validation night at LINE ...Satoshi Suzuki
 
Si pp introduction_2
Si pp introduction_2Si pp introduction_2
Si pp introduction_2kamrandb2
 
CLI, the other SAPI
CLI, the other SAPICLI, the other SAPI
CLI, the other SAPICombell NV
 
Working with Bytecode
Working with BytecodeWorking with Bytecode
Working with BytecodeMarcus Denker
 
Gwt wouter
Gwt wouterGwt wouter
Gwt wouterWouter
 
HHVM on AArch64 - BUD17-400K1
HHVM on AArch64 - BUD17-400K1HHVM on AArch64 - BUD17-400K1
HHVM on AArch64 - BUD17-400K1Linaro
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101premrings
 
Java- Datagram Socket class & Datagram Packet class
Java- Datagram Socket class  & Datagram Packet classJava- Datagram Socket class  & Datagram Packet class
Java- Datagram Socket class & Datagram Packet classRuchi Maurya
 
Solved Practice Problems For the C++ Exams
Solved Practice Problems For the C++ ExamsSolved Practice Problems For the C++ Exams
Solved Practice Problems For the C++ ExamsMuhammadTalha436
 

What's hot (20)

Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
Tcp sockets
Tcp socketsTcp sockets
Tcp sockets
 
Practical basics on c++
Practical basics on c++Practical basics on c++
Practical basics on c++
 
network programing lab file ,
network programing lab file ,network programing lab file ,
network programing lab file ,
 
Maker All - Executive Presentation
Maker All - Executive PresentationMaker All - Executive Presentation
Maker All - Executive Presentation
 
focuslight-validator validate sinatra application - validation night at LINE ...
focuslight-validator validate sinatra application - validation night at LINE ...focuslight-validator validate sinatra application - validation night at LINE ...
focuslight-validator validate sinatra application - validation night at LINE ...
 
Si pp introduction_2
Si pp introduction_2Si pp introduction_2
Si pp introduction_2
 
Buffer OverFlow
Buffer OverFlowBuffer OverFlow
Buffer OverFlow
 
CLI, the other SAPI
CLI, the other SAPICLI, the other SAPI
CLI, the other SAPI
 
Working with Bytecode
Working with BytecodeWorking with Bytecode
Working with Bytecode
 
Oop Presentation
Oop PresentationOop Presentation
Oop Presentation
 
Gwt wouter
Gwt wouterGwt wouter
Gwt wouter
 
HHVM on AArch64 - BUD17-400K1
HHVM on AArch64 - BUD17-400K1HHVM on AArch64 - BUD17-400K1
HHVM on AArch64 - BUD17-400K1
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
Java- Datagram Socket class & Datagram Packet class
Java- Datagram Socket class  & Datagram Packet classJava- Datagram Socket class  & Datagram Packet class
Java- Datagram Socket class & Datagram Packet class
 
Solved Practice Problems For the C++ Exams
Solved Practice Problems For the C++ ExamsSolved Practice Problems For the C++ Exams
Solved Practice Problems For the C++ Exams
 
Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M31 - PEP 8Python Programming Essentials - M31 - PEP 8
Python Programming Essentials - M31 - PEP 8
 
Fidl analysis
Fidl analysisFidl analysis
Fidl analysis
 
C++ file
C++ fileC++ file
C++ file
 
Python Programming Essentials - M5 - Variables
Python Programming Essentials - M5 - VariablesPython Programming Essentials - M5 - Variables
Python Programming Essentials - M5 - Variables
 

Viewers also liked

Accessing Capital to Grow Your Business
Accessing Capital to Grow Your BusinessAccessing Capital to Grow Your Business
Accessing Capital to Grow Your BusinessKim Eberhardt
 
General Laws C. 93, § 70 Statutory Requirements And Practice Norms (Final)
General Laws C. 93, § 70 Statutory Requirements And Practice Norms (Final)General Laws C. 93, § 70 Statutory Requirements And Practice Norms (Final)
General Laws C. 93, § 70 Statutory Requirements And Practice Norms (Final)AdvogadaZuretti
 
Survive The Downturn Through Quality 050509
Survive The Downturn Through Quality 050509Survive The Downturn Through Quality 050509
Survive The Downturn Through Quality 050509sixsigmamike
 
Keeping Client Data Safe (Final)
Keeping Client Data Safe (Final)Keeping Client Data Safe (Final)
Keeping Client Data Safe (Final)AdvogadaZuretti
 
7 Tips: Using LinkedIn to Grow Your Business
7 Tips: Using LinkedIn to Grow Your Business7 Tips: Using LinkedIn to Grow Your Business
7 Tips: Using LinkedIn to Grow Your BusinessKim Eberhardt
 
Channels Of The Future Presentation May 6,2009
Channels Of The Future Presentation May 6,2009Channels Of The Future Presentation May 6,2009
Channels Of The Future Presentation May 6,2009CMR.bz
 
What You Need To Know About Closing Protection Letters (Final)
What You Need To Know About Closing Protection Letters (Final)What You Need To Know About Closing Protection Letters (Final)
What You Need To Know About Closing Protection Letters (Final)AdvogadaZuretti
 
Rapid Development Presentation David Pollock
Rapid Development Presentation   David PollockRapid Development Presentation   David Pollock
Rapid Development Presentation David Pollockdavidppollock
 
Build Your Practice, Stay Out Of Trouble Ten Tips For Real Estate Attorneys...
Build Your Practice, Stay Out Of Trouble   Ten Tips For Real Estate Attorneys...Build Your Practice, Stay Out Of Trouble   Ten Tips For Real Estate Attorneys...
Build Your Practice, Stay Out Of Trouble Ten Tips For Real Estate Attorneys...AdvogadaZuretti
 
Building DevOps with Beer & Whiteboards
Building DevOps with Beer & WhiteboardsBuilding DevOps with Beer & Whiteboards
Building DevOps with Beer & WhiteboardsJohn Martin
 
Keys to Improving Your Collections Process
Keys to Improving Your Collections ProcessKeys to Improving Your Collections Process
Keys to Improving Your Collections ProcessKim Eberhardt
 
Advanced Sockets Programming
Advanced Sockets ProgrammingAdvanced Sockets Programming
Advanced Sockets Programmingelliando dias
 

Viewers also liked (14)

Accessing Capital to Grow Your Business
Accessing Capital to Grow Your BusinessAccessing Capital to Grow Your Business
Accessing Capital to Grow Your Business
 
General Laws C. 93, § 70 Statutory Requirements And Practice Norms (Final)
General Laws C. 93, § 70 Statutory Requirements And Practice Norms (Final)General Laws C. 93, § 70 Statutory Requirements And Practice Norms (Final)
General Laws C. 93, § 70 Statutory Requirements And Practice Norms (Final)
 
Survive The Downturn Through Quality 050509
Survive The Downturn Through Quality 050509Survive The Downturn Through Quality 050509
Survive The Downturn Through Quality 050509
 
Keeping Client Data Safe (Final)
Keeping Client Data Safe (Final)Keeping Client Data Safe (Final)
Keeping Client Data Safe (Final)
 
Cash Is King
Cash Is KingCash Is King
Cash Is King
 
7 Tips: Using LinkedIn to Grow Your Business
7 Tips: Using LinkedIn to Grow Your Business7 Tips: Using LinkedIn to Grow Your Business
7 Tips: Using LinkedIn to Grow Your Business
 
Channels Of The Future Presentation May 6,2009
Channels Of The Future Presentation May 6,2009Channels Of The Future Presentation May 6,2009
Channels Of The Future Presentation May 6,2009
 
What You Need To Know About Closing Protection Letters (Final)
What You Need To Know About Closing Protection Letters (Final)What You Need To Know About Closing Protection Letters (Final)
What You Need To Know About Closing Protection Letters (Final)
 
Rapid Development Presentation David Pollock
Rapid Development Presentation   David PollockRapid Development Presentation   David Pollock
Rapid Development Presentation David Pollock
 
Build Your Practice, Stay Out Of Trouble Ten Tips For Real Estate Attorneys...
Build Your Practice, Stay Out Of Trouble   Ten Tips For Real Estate Attorneys...Build Your Practice, Stay Out Of Trouble   Ten Tips For Real Estate Attorneys...
Build Your Practice, Stay Out Of Trouble Ten Tips For Real Estate Attorneys...
 
Building DevOps with Beer & Whiteboards
Building DevOps with Beer & WhiteboardsBuilding DevOps with Beer & Whiteboards
Building DevOps with Beer & Whiteboards
 
Keys to Improving Your Collections Process
Keys to Improving Your Collections ProcessKeys to Improving Your Collections Process
Keys to Improving Your Collections Process
 
Advanced Sockets Programming
Advanced Sockets ProgrammingAdvanced Sockets Programming
Advanced Sockets Programming
 
Surv Thriv Sept2009
Surv Thriv Sept2009Surv Thriv Sept2009
Surv Thriv Sept2009
 

Similar to Socket Programming Tutorial 1227317798640739 8

Socket programming
Socket programmingSocket programming
Socket programmingAnurag Tomar
 
Networking lab
Networking labNetworking lab
Networking labRagu Ram
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in JavaTushar B Kute
 
Socket programming in C
Socket programming in CSocket programming in C
Socket programming in CDeepak Swain
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In JavaAnkur Agrawal
 
CSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docx
CSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docxCSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docx
CSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docxmydrynan
 
python programming
python programmingpython programming
python programmingkeerthikaA8
 
Remote Procedure Call
Remote Procedure CallRemote Procedure Call
Remote Procedure CallNadia Nahar
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in JavaTushar B Kute
 
Network Prog.ppt
Network Prog.pptNetwork Prog.ppt
Network Prog.pptEloOgardo
 

Similar to Socket Programming Tutorial 1227317798640739 8 (20)

Socket programming
Socket programmingSocket programming
Socket programming
 
Networking lab
Networking labNetworking lab
Networking lab
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
 
03 sockets
03 sockets03 sockets
03 sockets
 
Socket programming in C
Socket programming in CSocket programming in C
Socket programming in C
 
Networking & Socket Programming In Java
Networking & Socket Programming In JavaNetworking & Socket Programming In Java
Networking & Socket Programming In Java
 
CSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docx
CSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docxCSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docx
CSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docx
 
Socket System Calls
Socket System CallsSocket System Calls
Socket System Calls
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
python programming
python programmingpython programming
python programming
 
Socket programming
Socket programming Socket programming
Socket programming
 
Remote Procedure Call
Remote Procedure CallRemote Procedure Call
Remote Procedure Call
 
Npc08
Npc08Npc08
Npc08
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Sockets
Sockets Sockets
Sockets
 
Pemrograman Jaringan
Pemrograman JaringanPemrograman Jaringan
Pemrograman Jaringan
 
Network Prog.ppt
Network Prog.pptNetwork Prog.ppt
Network Prog.ppt
 
Net Programming.ppt
Net Programming.pptNet Programming.ppt
Net Programming.ppt
 
A.java
A.javaA.java
A.java
 
Pycon - Python for ethical hackers
Pycon - Python for ethical hackers Pycon - Python for ethical hackers
Pycon - Python for ethical hackers
 

Recently uploaded

How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17Celine George
 

Recently uploaded (20)

Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of EngineeringFaculty Profile prashantha K EEE dept Sri Sairam college of Engineering
Faculty Profile prashantha K EEE dept Sri Sairam college of Engineering
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17
 

Socket Programming Tutorial 1227317798640739 8

  • 1. Socket Programming Jignesh Patel Palanivel Rathinam connecting processes
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12. Programming TCP Client in C #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> void error(char *msg){ perror(msg); exit(0);} int main(int argc, char *argv[]){ int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; if (argc < 3) { fprintf(stderr,&quot;usage %s hostname port&quot;, argv[0]); exit(0); } portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sockfd < 0) error(&quot;ERROR opening socket&quot;); /* a structure to contain an internet address defined in the include file <netinet/in.h> */ struct sockaddr_in { short sin_family; /* should be AF_INET */ u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; /* not used, must be zero */ }; struct in_addr { unsigned long s_addr; }; Client.c
  • 13. Programming TCP Client in C #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> void error(char *msg){ perror(msg); exit(0);} int main(int argc, char *argv[]){ int sockfd, portno, n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; if (argc < 3) { fprintf(stderr,&quot;usage %s hostname port&quot;, argv[0]); exit(0); } portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sockfd < 0) error(&quot;ERROR opening socket&quot;); Client.c Socket System Call – create an end point for communication #include <sys/types.h> #include <sys/socket.h> int socket(int domain , int type , int protocol ); Returns a descriptor domain : selects protocol family e.g. PF_IPX, PF_X25, PF_APPLETALK type : specifies communication semantics e.g. SOCK_DGRAM, SOCK_RAW protocol : specifies a particular protocol to be used e.g. IPPROTO_UDP, IPPROTO_ICMP
  • 14. Programming TCP Client in C server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr,&quot;ERROR, no such host&quot;); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr , server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) error(&quot;ERROR connecting&quot;); printf(&quot;Please enter the message: &quot;); bzero(buffer,256); fgets(buffer,255,stdin); n = send(sockfd,buffer,strlen(buffer),0); if (n < 0) error(&quot;ERROR writing to socket&quot;); bzero(buffer,256); n = recv(sockfd,buffer,255,0); if (n < 0) error(&quot;ERROR reading from socket&quot;); printf(&quot;%s&quot;,buffer); close(sockfd); return 0; } Client.c Connect System Call – initiates a connection on a socket #include <sys/types.h> #include <sys/socket.h> int connect( int sockfd , const struct sockaddr * serv_addr , socklen_t addrlen ); Returns 0 on success sockfd : descriptor that must refer to a socket serv_addr : address to which we want to connect addrlen : length of serv_addr
  • 15. Programming TCP Client in C server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr,&quot;ERROR, no such host&quot;); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr , server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) error(&quot;ERROR connecting&quot;); printf(&quot;Please enter the message: &quot;); bzero(buffer,256); fgets(buffer,255,stdin); n = send(sockfd,buffer,strlen(buffer),0); if (n < 0) error(&quot;ERROR writing to socket&quot;); bzero(buffer,256); n = recv(sockfd,buffer,255,0); if (n < 0) error(&quot;ERROR reading from socket&quot;); printf(&quot;%s&quot;,buffer); close(sockfd); return 0; } Client.c Send System Call – send a message to a socket #include <sys/types.h> #include <sys/socket.h> int send( int s , const void * msg , size_t len , int flags ); Returns number of characters sent on success s : descriptor that must refer to a socket in connected state msg : data that we want to send len : length of data flags : use default 0. MSG_OOB, MSG_DONTWAIT
  • 16. Programming TCP Client in C server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr,&quot;ERROR, no such host&quot;); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr , server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) error(&quot;ERROR connecting&quot;); printf(&quot;Please enter the message: &quot;); bzero(buffer,256); fgets(buffer,255,stdin); n = send(sockfd,buffer,strlen(buffer),0); if (n < 0) error(&quot;ERROR writing to socket&quot;); bzero(buffer,256); n = recv(sockfd,buffer,255,0); if (n < 0) error(&quot;ERROR reading from socket&quot;); printf(&quot;%s&quot;,buffer); close(sockfd); return 0; } Client.c Recv System Call – receive a message from a socket #include <sys/types.h> #include <sys/socket.h> int recv( int s , const void * buff , size_t len , int flags ); Returns number of bytes received on success s : descriptor that must refer to a socket in connected state buff : data that we want to receive len : length of data flags : use default 0. MSG_OOB, MSG_DONTWAIT
  • 17. Programming TCP Client in C server = gethostbyname(argv[1]); if (server == NULL) { fprintf(stderr,&quot;ERROR, no such host&quot;); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr , server->h_length); serv_addr.sin_port = htons(portno); if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0) error(&quot;ERROR connecting&quot;); printf(&quot;Please enter the message: &quot;); bzero(buffer,256); fgets(buffer,255,stdin); n = send(sockfd,buffer,strlen(buffer),0); if (n < 0) error(&quot;ERROR writing to socket&quot;); bzero(buffer,256); n = recv(sockfd,buffer,255,0); if (n < 0) error(&quot;ERROR reading from socket&quot;); printf(&quot;%s&quot;,buffer); close(sockfd); return 0; } Client.c Close System Call – close a socket descriptor #include <unistd.h> int close( int s ); Returns 0 on success s : descriptor to be closed
  • 18. Programming TCP Server in C #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> void error(char *msg){ perror(msg); exit(0);} int main(int argc, char *argv[]){ int sockfd, newsockfd, portno, clilen; char buffer[256]; struct sockaddr_in serv_addr, cli_addr; int n; if (argc < 2) { fprintf(stderr,&quot;ERROR, no port provided&quot;); exit(1); } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error(&quot;ERROR opening socket&quot;); bzero((char *) &serv_addr, sizeof(serv_addr)); portno = atoi(argv[1]); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); Server.c
  • 19. Programming TCP Server in C if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error(&quot;ERROR on binding&quot;); listen(sockfd,5); clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error(&quot;ERROR on accept&quot;); bzero(buffer,256); n = recv(newsockfd,buffer,255,0); if (n < 0) error(&quot;ERROR reading from socket&quot;); printf(&quot;Here is the message: %s&quot;,buffer); n = send(newsockfd,&quot;I got your message&quot;,18,0); if (n < 0) error(&quot;ERROR writing to socket&quot;); close(newsockfd); close(sockfd); return 0; } Server.c Bind System Call – bind a name to a socket #include <sys/types.h> #include <sys/socket.h> int bind( int sockfd , const struct sockaddr * serv_addr , socklen_t addrlen ); Returns 0 on success sockfd : descriptor that must refer to a socket serv_addr : address to which we want to connect addrlen : length of serv_addr
  • 20. Programming TCP Server in C if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error(&quot;ERROR on binding&quot;); listen(sockfd,5); clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error(&quot;ERROR on accept&quot;); bzero(buffer,256); n = recv(newsockfd,buffer,255,0); if (n < 0) error(&quot;ERROR reading from socket&quot;); printf(&quot;Here is the message: %s&quot;,buffer); n = send(newsockfd,&quot;I got your message&quot;,18,0); if (n < 0) error(&quot;ERROR writing to socket&quot;); close(newsockfd); close(sockfd); return 0; } Server.c Listen System Call – listen for connections on a socket #include <sys/types.h> #include <sys/socket.h> int listen( int s , int backlog ); Returns 0 on success s : descriptor that must refer to a socket backlog : maximum length the queue for completely established sockets waiting to be accepted addrlen : length of serv_addr
  • 21. Programming TCP Server in C if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error(&quot;ERROR on binding&quot;); listen(sockfd,5); clilen = sizeof(cli_addr); newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error(&quot;ERROR on accept&quot;); bzero(buffer,256); n = recv(newsockfd,buffer,255,0); if (n < 0) error(&quot;ERROR reading from socket&quot;); printf(&quot;Here is the message: %s&quot;,buffer); n = send(newsockfd,&quot;I got your message&quot;,18,0); if (n < 0) error(&quot;ERROR writing to socket&quot;); close(newsockfd); close(sockfd); return 0; } Server.c Accept System Call – accepts a connection on a socket #include <sys/types.h> #include <sys/socket.h> int accept( int sockfd , const struct sockaddr * addr , socklen_t addrlen ); Returns a non-negative descriptor on success sockfd : descriptor that must refer to a socket addr : filled with address of connecting entity addrlen : length of addr
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.