SlideShare une entreprise Scribd logo
1  sur  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]

Contenu connexe

Tendances

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_2
kamrandb2
 
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
Ruchi Maurya
 

Tendances (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
 

En vedette

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
 
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 Business
Kim Eberhardt
 
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 Pollock
davidppollock
 
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
 
Advanced Sockets Programming
Advanced Sockets ProgrammingAdvanced Sockets Programming
Advanced Sockets Programming
elliando dias
 

En vedette (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
 

Similaire à Socket Programming Tutorial 1227317798640739 8

Socket programming in C
Socket programming in CSocket programming in C
Socket programming in C
Deepak Swain
 
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
mydrynan
 

Similaire à 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
 

Dernier

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Dernier (20)

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
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.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
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

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.