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

Requirement specification (SRS)
Requirement specification (SRS)Requirement specification (SRS)
Requirement specification (SRS)kunj desai
 
INSTRUCTION LEVEL PARALLALISM
INSTRUCTION LEVEL PARALLALISMINSTRUCTION LEVEL PARALLALISM
INSTRUCTION LEVEL PARALLALISMKamran Ashraf
 
Os rtos.ppt
Os rtos.pptOs rtos.ppt
Os rtos.pptrahul km
 
Formal Verification
Formal VerificationFormal Verification
Formal VerificationIlia Levin
 
Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...
Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...
Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...tanujaparihar
 
05 pig user defined functions (udfs)
05 pig user defined functions (udfs)05 pig user defined functions (udfs)
05 pig user defined functions (udfs)Subhas Kumar Ghosh
 
Advanced Processor Power Point Presentation
Advanced Processor  Power Point  PresentationAdvanced Processor  Power Point  Presentation
Advanced Processor Power Point PresentationPrashantYadav931011
 
IO and file systems
IO and file systems IO and file systems
IO and file systems EktaVaswani2
 
Software components
Software componentsSoftware components
Software componentsAmir_Mukhtar
 
Comparison of the Waterfall, Spiral, and Prototype SDLC Models
Comparison of the Waterfall, Spiral, and Prototype SDLC ModelsComparison of the Waterfall, Spiral, and Prototype SDLC Models
Comparison of the Waterfall, Spiral, and Prototype SDLC ModelsTeresa Rothaar
 
Unit 3 file management
Unit 3 file managementUnit 3 file management
Unit 3 file managementKalai Selvi
 
Лекция 2: Оптимизация ветвлений и циклов (Branch prediction and loops optimiz...
Лекция 2: Оптимизация ветвлений и циклов (Branch prediction and loops optimiz...Лекция 2: Оптимизация ветвлений и циклов (Branch prediction and loops optimiz...
Лекция 2: Оптимизация ветвлений и циклов (Branch prediction and loops optimiz...Mikhail Kurnosov
 
Software life cycle comparison
Software life cycle comparisonSoftware life cycle comparison
Software life cycle comparisonSuvek Shakya
 
Recipe Database Project Management
Recipe Database Project ManagementRecipe Database Project Management
Recipe Database Project Managementformalforker
 
Multiprocessor architecture
Multiprocessor architectureMultiprocessor architecture
Multiprocessor architectureArpan Baishya
 

What's hot (20)

Fragmentaton
Fragmentaton Fragmentaton
Fragmentaton
 
Requirement specification (SRS)
Requirement specification (SRS)Requirement specification (SRS)
Requirement specification (SRS)
 
INSTRUCTION LEVEL PARALLALISM
INSTRUCTION LEVEL PARALLALISMINSTRUCTION LEVEL PARALLALISM
INSTRUCTION LEVEL PARALLALISM
 
Assembly Language
Assembly LanguageAssembly Language
Assembly Language
 
Os rtos.ppt
Os rtos.pptOs rtos.ppt
Os rtos.ppt
 
Formal Verification
Formal VerificationFormal Verification
Formal Verification
 
Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...
Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...
Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...
 
05 pig user defined functions (udfs)
05 pig user defined functions (udfs)05 pig user defined functions (udfs)
05 pig user defined functions (udfs)
 
CPU Architecture
CPU ArchitectureCPU Architecture
CPU Architecture
 
Advanced Processor Power Point Presentation
Advanced Processor  Power Point  PresentationAdvanced Processor  Power Point  Presentation
Advanced Processor Power Point Presentation
 
IO and file systems
IO and file systems IO and file systems
IO and file systems
 
Software components
Software componentsSoftware components
Software components
 
Comparison of the Waterfall, Spiral, and Prototype SDLC Models
Comparison of the Waterfall, Spiral, and Prototype SDLC ModelsComparison of the Waterfall, Spiral, and Prototype SDLC Models
Comparison of the Waterfall, Spiral, and Prototype SDLC Models
 
OpTask
OpTaskOpTask
OpTask
 
Unit 3 file management
Unit 3 file managementUnit 3 file management
Unit 3 file management
 
Лекция 2: Оптимизация ветвлений и циклов (Branch prediction and loops optimiz...
Лекция 2: Оптимизация ветвлений и циклов (Branch prediction and loops optimiz...Лекция 2: Оптимизация ветвлений и циклов (Branch prediction and loops optimiz...
Лекция 2: Оптимизация ветвлений и циклов (Branch prediction and loops optimiz...
 
Software life cycle comparison
Software life cycle comparisonSoftware life cycle comparison
Software life cycle comparison
 
Recipe Database Project Management
Recipe Database Project ManagementRecipe Database Project Management
Recipe Database Project Management
 
Multiprocessor architecture
Multiprocessor architectureMultiprocessor architecture
Multiprocessor architecture
 
Secondary Memory
Secondary MemorySecondary Memory
Secondary Memory
 

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

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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
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
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 

Recently uploaded (20)

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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
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
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

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));