SlideShare una empresa de Scribd logo
1 de 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]

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Destacado

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
 

Destacado (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 a 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 a 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
 

Último

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 

Último (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

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.