SlideShare a Scribd company logo
1 of 4
Narayan Lal Menariya August 31,2018
Client – server communication using socket
When we use shared memory, messagequeueor pipes to achieveinter process
communication(IPC) than this can beachieved only and only if two process arerunning
on the samemachine. This kind of communication in which two processes arerunning
on samesystem and communicating with each other using IPC mechanismis termed
as Intra-Process Communication.
But assumethat if two process arerunning on two different machines than how they
will communicatewith each other. This problem can beresolved by Socket IPC.
Socket’s arenothing but kernel objects which act as an end point between two
machines. Do not assumethat socket will belikea wirewhich is used to achieveintra-
process or inter-process communication.
Sockets arespecial files which is thecombination of IP address plus Port number. for
exampleif your system’s IP address is : 10.0.5.15 and port number is 8000than socket
will be : 10.0.5.15:8000. By default sockets IPC is full duplex process and can beused to
achieve both intra-process and inter-processcommunication.
Socket Programming: In order to establish communication between two process using
socket they first need to follow somesteps as mention below.
pre-requirement:Pleasekindly go through theSocket system calls as i will not mention
return typeof system calls and data types. You can easily search them using command
“man socket”.
server steps:
1. A socket need to be creat using system call socket().
2. To achievecommunication between server and client , server need to be bind
with client using system call: bind().
3. Once binding is doneserver will listen client using listen() system call.
4. Now server needs to accept client request using accept() system call.
5. Now the connection is established so two way communicationca bestart. First
message will be send by client and server needsto send response.
client steps:
1. client will follow the same1,2 and 3rd steps.
2. now once binding is doneclient needs to connect with server using system call:
connect().
3. Now once theserver will accept it two way communicationcan beachieved.
Now here i am sharing my own written server-client codejust go through it and if find
any problem than feel free to ask.
Here note down that first message should be send by client.
server.c
1. /*
2. NarayanLal menariya
3. August31,2018
4. */
5.
6. #include<stdio.h>
7. #include<sys/types.h>
8. #include<sys/socket.h>
9. #include<netinet/in.h>
10. #include<netinet/ip.h>
11. #include<stdlib.h>
12. #include<arpa/inet.h>
13.
14. int main()
15. {
16. printf("InServern");
17.
18. //creatingsocket
19. int sock=socket(AF_INET,SOCK_STREAM,0);
20. if(sock==-1)
21. perror("SocketError");
22. else
23. {
24. //socketstructure isdefinedin#include<netinet/in.h>headerfile
25. struct sockaddr_insockname;
26. sockname.sin_family=AF_INET;
27. sockname.sin_port=htons(8000);
28. sockname.sin_addr.s_addr=inet_addr("127.0.0.1");
29.
30. //binding
31. int b=bind(sock,(structsockaddr*)(&sockname),sizeof(sockname));
32. if(b==-1)
33. perror("BindError");
34. else
35. {
36. //listening
37. listen(sock,1);
38. struct sockaddr_ina_sock;
39. int size=sizeof(a_sock);
40.
41. //accepting
42. int a=accept(sock,(structsockaddr*)(&a_sock),&size);
43. if(a==-1)
44. perror("AcceptError");
45. else
46. {
47. char buf[10];
48.
49. //readingfirstmessage of client
50. int r=read(a,buf,sizeof(buf));
51. buf[r]='0';
52. printf("ContentsfromClient:%sn",buf);
53.
54. //sendingresponse toclient.
55. write(a,"Helloclient",12);
56. }
57. }
58. }
59. return 0;
60. }
client.c
1. /*
2. NarayanLal Menariya
3. August31,2018
4.
5. */
6.
7. #include<stdio.h>
8. #include<sys/types.h>
9. #include<sys/socket.h>
10. #include<netinet/in.h>
11. #include<netinet/ip.h>
12.
13. int main()
14. {
15. printf("InsideClientn");
16.
17. //creatingsocket
18. int sock=socket(AF_INET,SOCK_STREAM,0);
19.
20. //definingsocketstrucutre member
21. struct sockaddr_insockname;
22. sockname.sin_family=AF_INET;
23. sockname.sin_port=htons(8000);
24. sockname.sin_addr.s_addr=inet_addr("127.0.0.1");
25.
26. //connecting
27. int cl=connect(sock,(structsockaddr*)(&sockname),sizeof(sockname));
28. if(cl==-1)
29. perror("Connect");
30. else
31. {
32. char buf[20];
33.
34. //sendingfirstmessage toserver
35. write(sock,"Helloserver",12);
36.
37. //readingserverrespone
38. int r=read(sock,buf,sizeof(buf));
39. buf[r]='0';
40. printf("ContentsfromServer:%sn",buf);
41. }
42. return 0;
43. }
Happy to help.

More Related Content

What's hot (20)

Design issues for the layers
Design issues for the layersDesign issues for the layers
Design issues for the layers
 
icmp , igmp
icmp , igmpicmp , igmp
icmp , igmp
 
Introduction to ARM LPC2148
Introduction to ARM LPC2148Introduction to ARM LPC2148
Introduction to ARM LPC2148
 
Mobile transportlayer
Mobile transportlayerMobile transportlayer
Mobile transportlayer
 
Digital clock
Digital clockDigital clock
Digital clock
 
FIVE PEN PC TECHNOLOGY
FIVE PEN PC TECHNOLOGYFIVE PEN PC TECHNOLOGY
FIVE PEN PC TECHNOLOGY
 
TCP/IP
TCP/IPTCP/IP
TCP/IP
 
Smart note taker
Smart note takerSmart note taker
Smart note taker
 
Wi-Fi Esp8266 nodemcu
Wi-Fi Esp8266 nodemcu Wi-Fi Esp8266 nodemcu
Wi-Fi Esp8266 nodemcu
 
File Transfer Protocol - FTP
File Transfer Protocol - FTPFile Transfer Protocol - FTP
File Transfer Protocol - FTP
 
5 Pen PC technology seminar report
5 Pen PC technology seminar report5 Pen PC technology seminar report
5 Pen PC technology seminar report
 
bluejacking.ppt
bluejacking.pptbluejacking.ppt
bluejacking.ppt
 
Weather Station Using IoT
Weather Station Using IoT Weather Station Using IoT
Weather Station Using IoT
 
Subnetting
SubnettingSubnetting
Subnetting
 
Blynk presentation
Blynk presentationBlynk presentation
Blynk presentation
 
Protocols in computer network
Protocols in computer network   Protocols in computer network
Protocols in computer network
 
IoT sensing and actuation
IoT sensing and actuationIoT sensing and actuation
IoT sensing and actuation
 
Digital clock
Digital clockDigital clock
Digital clock
 
Unguided media
Unguided mediaUnguided media
Unguided media
 
Java Ring
Java RingJava Ring
Java Ring
 

Similar to Client-Server Communication Using Sockets

Linux Inter Process Communication
Linux Inter Process CommunicationLinux Inter Process Communication
Linux Inter Process CommunicationAbhishek Sagar
 
TCP Sockets Tutor maXbox starter26
TCP Sockets Tutor maXbox starter26TCP Sockets Tutor maXbox starter26
TCP Sockets Tutor maXbox starter26Max Kleiner
 
Cse318,lab report
Cse318,lab reportCse318,lab report
Cse318,lab reportFarhadHimel
 
Question 1The OSI model has seven layers where each layer pe.docx
Question 1The OSI model has seven layers where each layer pe.docxQuestion 1The OSI model has seven layers where each layer pe.docx
Question 1The OSI model has seven layers where each layer pe.docxssuser774ad41
 
Design Presentation Distributed Monitoring tool
Design Presentation Distributed Monitoring toolDesign Presentation Distributed Monitoring tool
Design Presentation Distributed Monitoring toolanuj_rakheja
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project ReportKavita Sharma
 
Design an Implementation of A Messaging and Resource Sharing Software
Design an Implementation of A Messaging and Resource Sharing SoftwareDesign an Implementation of A Messaging and Resource Sharing Software
Design an Implementation of A Messaging and Resource Sharing Softwarenilabarai
 
An in-building multi-server cloud system based on shortest Path algorithm dep...
An in-building multi-server cloud system based on shortest Path algorithm dep...An in-building multi-server cloud system based on shortest Path algorithm dep...
An in-building multi-server cloud system based on shortest Path algorithm dep...IOSR Journals
 
Chapter 2B-Communication.ppt
Chapter 2B-Communication.pptChapter 2B-Communication.ppt
Chapter 2B-Communication.pptsirajmohammed35
 
A step on developing network monitoring tools
A step on developing network monitoring toolsA step on developing network monitoring tools
A step on developing network monitoring toolsAlexander Decker
 
Intranet Messaging Project Report -phpapp02
Intranet Messaging Project Report -phpapp02Intranet Messaging Project Report -phpapp02
Intranet Messaging Project Report -phpapp02dvicky12
 
The Network Ip Address Scheme
The Network Ip Address SchemeThe Network Ip Address Scheme
The Network Ip Address SchemeErin Rivera
 
Network and Internet Security.docx
Network and Internet Security.docxNetwork and Internet Security.docx
Network and Internet Security.docxstirlingvwriters
 
Report on routing interface configuration
Report on routing interface configurationReport on routing interface configuration
Report on routing interface configurationDebjyotiSaha9
 

Similar to Client-Server Communication Using Sockets (20)

Linux Inter Process Communication
Linux Inter Process CommunicationLinux Inter Process Communication
Linux Inter Process Communication
 
TCP Sockets Tutor maXbox starter26
TCP Sockets Tutor maXbox starter26TCP Sockets Tutor maXbox starter26
TCP Sockets Tutor maXbox starter26
 
Cse318,lab report
Cse318,lab reportCse318,lab report
Cse318,lab report
 
Question 1The OSI model has seven layers where each layer pe.docx
Question 1The OSI model has seven layers where each layer pe.docxQuestion 1The OSI model has seven layers where each layer pe.docx
Question 1The OSI model has seven layers where each layer pe.docx
 
Design Presentation Distributed Monitoring tool
Design Presentation Distributed Monitoring toolDesign Presentation Distributed Monitoring tool
Design Presentation Distributed Monitoring tool
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
 
Design an Implementation of A Messaging and Resource Sharing Software
Design an Implementation of A Messaging and Resource Sharing SoftwareDesign an Implementation of A Messaging and Resource Sharing Software
Design an Implementation of A Messaging and Resource Sharing Software
 
An in-building multi-server cloud system based on shortest Path algorithm dep...
An in-building multi-server cloud system based on shortest Path algorithm dep...An in-building multi-server cloud system based on shortest Path algorithm dep...
An in-building multi-server cloud system based on shortest Path algorithm dep...
 
H017113842
H017113842H017113842
H017113842
 
NP-lab-manual.docx
NP-lab-manual.docxNP-lab-manual.docx
NP-lab-manual.docx
 
Chapter 2B-Communication.ppt
Chapter 2B-Communication.pptChapter 2B-Communication.ppt
Chapter 2B-Communication.ppt
 
A step on developing network monitoring tools
A step on developing network monitoring toolsA step on developing network monitoring tools
A step on developing network monitoring tools
 
NP-lab-manual (1).pdf
NP-lab-manual (1).pdfNP-lab-manual (1).pdf
NP-lab-manual (1).pdf
 
NP-lab-manual.pdf
NP-lab-manual.pdfNP-lab-manual.pdf
NP-lab-manual.pdf
 
internet basics
internet basics internet basics
internet basics
 
Intranet Messaging Project Report -phpapp02
Intranet Messaging Project Report -phpapp02Intranet Messaging Project Report -phpapp02
Intranet Messaging Project Report -phpapp02
 
The Network Ip Address Scheme
The Network Ip Address SchemeThe Network Ip Address Scheme
The Network Ip Address Scheme
 
Network and Internet Security.docx
Network and Internet Security.docxNetwork and Internet Security.docx
Network and Internet Security.docx
 
Intake 37 12
Intake 37 12Intake 37 12
Intake 37 12
 
Report on routing interface configuration
Report on routing interface configurationReport on routing interface configuration
Report on routing interface configuration
 

More from NarayanlalMenariya

More from NarayanlalMenariya (16)

Updated CV
Updated CVUpdated CV
Updated CV
 
Curriculum Vitae
Curriculum VitaeCurriculum Vitae
Curriculum Vitae
 
Face Detection And Tracking
Face Detection And TrackingFace Detection And Tracking
Face Detection And Tracking
 
Resume for fresher
Resume for fresherResume for fresher
Resume for fresher
 
C++ Programs
C++ ProgramsC++ Programs
C++ Programs
 
Character recognition
Character recognitionCharacter recognition
Character recognition
 
GUI based calculator using MATLAB
GUI based calculator using MATLABGUI based calculator using MATLAB
GUI based calculator using MATLAB
 
Steganography
SteganographySteganography
Steganography
 
Message queue and shared memory
Message queue and shared memoryMessage queue and shared memory
Message queue and shared memory
 
Synchronization of shared memory using semaphores
Synchronization of shared memory using semaphoresSynchronization of shared memory using semaphores
Synchronization of shared memory using semaphores
 
Home automation using MATLAB image processing
Home automation using MATLAB image processingHome automation using MATLAB image processing
Home automation using MATLAB image processing
 
Simplified Experimental Determination of Line Transient Immunity of Linear Re...
Simplified Experimental Determination of Line Transient Immunity of Linear Re...Simplified Experimental Determination of Line Transient Immunity of Linear Re...
Simplified Experimental Determination of Line Transient Immunity of Linear Re...
 
SMART E-TOLL SYSTEM
SMART E-TOLL SYSTEMSMART E-TOLL SYSTEM
SMART E-TOLL SYSTEM
 
Voice From Deep Of Heart
Voice From Deep Of HeartVoice From Deep Of Heart
Voice From Deep Of Heart
 
Lidar and sensing
Lidar and sensingLidar and sensing
Lidar and sensing
 
A chip to protect IOT
A chip to protect IOTA chip to protect IOT
A chip to protect IOT
 

Recently uploaded

Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 

Recently uploaded (20)

Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 

Client-Server Communication Using Sockets

  • 1. Narayan Lal Menariya August 31,2018 Client – server communication using socket When we use shared memory, messagequeueor pipes to achieveinter process communication(IPC) than this can beachieved only and only if two process arerunning on the samemachine. This kind of communication in which two processes arerunning on samesystem and communicating with each other using IPC mechanismis termed as Intra-Process Communication. But assumethat if two process arerunning on two different machines than how they will communicatewith each other. This problem can beresolved by Socket IPC. Socket’s arenothing but kernel objects which act as an end point between two machines. Do not assumethat socket will belikea wirewhich is used to achieveintra- process or inter-process communication. Sockets arespecial files which is thecombination of IP address plus Port number. for exampleif your system’s IP address is : 10.0.5.15 and port number is 8000than socket will be : 10.0.5.15:8000. By default sockets IPC is full duplex process and can beused to achieve both intra-process and inter-processcommunication. Socket Programming: In order to establish communication between two process using socket they first need to follow somesteps as mention below. pre-requirement:Pleasekindly go through theSocket system calls as i will not mention return typeof system calls and data types. You can easily search them using command “man socket”. server steps: 1. A socket need to be creat using system call socket(). 2. To achievecommunication between server and client , server need to be bind with client using system call: bind(). 3. Once binding is doneserver will listen client using listen() system call. 4. Now server needs to accept client request using accept() system call. 5. Now the connection is established so two way communicationca bestart. First message will be send by client and server needsto send response. client steps: 1. client will follow the same1,2 and 3rd steps. 2. now once binding is doneclient needs to connect with server using system call: connect(). 3. Now once theserver will accept it two way communicationcan beachieved. Now here i am sharing my own written server-client codejust go through it and if find any problem than feel free to ask. Here note down that first message should be send by client.
  • 2. server.c 1. /* 2. NarayanLal menariya 3. August31,2018 4. */ 5. 6. #include<stdio.h> 7. #include<sys/types.h> 8. #include<sys/socket.h> 9. #include<netinet/in.h> 10. #include<netinet/ip.h> 11. #include<stdlib.h> 12. #include<arpa/inet.h> 13. 14. int main() 15. { 16. printf("InServern"); 17. 18. //creatingsocket 19. int sock=socket(AF_INET,SOCK_STREAM,0); 20. if(sock==-1) 21. perror("SocketError"); 22. else 23. { 24. //socketstructure isdefinedin#include<netinet/in.h>headerfile 25. struct sockaddr_insockname; 26. sockname.sin_family=AF_INET; 27. sockname.sin_port=htons(8000); 28. sockname.sin_addr.s_addr=inet_addr("127.0.0.1"); 29. 30. //binding 31. int b=bind(sock,(structsockaddr*)(&sockname),sizeof(sockname)); 32. if(b==-1) 33. perror("BindError"); 34. else 35. { 36. //listening 37. listen(sock,1); 38. struct sockaddr_ina_sock; 39. int size=sizeof(a_sock); 40. 41. //accepting 42. int a=accept(sock,(structsockaddr*)(&a_sock),&size); 43. if(a==-1) 44. perror("AcceptError"); 45. else 46. { 47. char buf[10]; 48. 49. //readingfirstmessage of client
  • 3. 50. int r=read(a,buf,sizeof(buf)); 51. buf[r]='0'; 52. printf("ContentsfromClient:%sn",buf); 53. 54. //sendingresponse toclient. 55. write(a,"Helloclient",12); 56. } 57. } 58. } 59. return 0; 60. } client.c 1. /* 2. NarayanLal Menariya 3. August31,2018 4. 5. */ 6. 7. #include<stdio.h> 8. #include<sys/types.h> 9. #include<sys/socket.h> 10. #include<netinet/in.h> 11. #include<netinet/ip.h> 12. 13. int main() 14. { 15. printf("InsideClientn"); 16. 17. //creatingsocket 18. int sock=socket(AF_INET,SOCK_STREAM,0); 19. 20. //definingsocketstrucutre member 21. struct sockaddr_insockname; 22. sockname.sin_family=AF_INET; 23. sockname.sin_port=htons(8000); 24. sockname.sin_addr.s_addr=inet_addr("127.0.0.1"); 25. 26. //connecting 27. int cl=connect(sock,(structsockaddr*)(&sockname),sizeof(sockname)); 28. if(cl==-1) 29. perror("Connect"); 30. else 31. { 32. char buf[20]; 33. 34. //sendingfirstmessage toserver 35. write(sock,"Helloserver",12); 36. 37. //readingserverrespone 38. int r=read(sock,buf,sizeof(buf));