SlideShare a Scribd company logo
1 of 7
Download to read offline
Tp Socket C
Tcp
Serveur :
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
int main(){
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
SOCKET sock;
sock=socket(AF_INET,SOCK_STREAM,0);
printf("n creation de sock serv");
SOCKADDR_IN addrS;
addrS.sin_family=AF_INET;
addrS.sin_port=htons(1050);
Réaliser par: Anas AOUINA
addrS.sin_addr.s_addr=htonl(INADDR_ANY);
printf(" n remplissage de sock_in");
bind(sock,(SOCKADDR*)&addrS,sizeof(addrS));
printf("n association d'info");
listen(sock,0);
printf("n mettre en etat d'ecoute");
SOCKET so_client;
printf("n declaration de sock client");
SOCKADDR_IN client;
int taille=sizeof(client);
so_client=accept(sock,(SOCKADDR*)&client,&taille);
printf("n acceptation");
char buffer[100];
int n;
strcpy(buffer,"Bonjour n");
send(so_client,buffer,sizeof(buffer),0);
printf("n send");
recv(so_client,buffer,sizeof(buffer),0);
n=strlen(buffer);
buffer[n]='0';
printf("n le mssg recu %s n",buffer);
closesocket(so_client);
closesocket(sock);
}
Client :
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
int main(){
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
SOCKET sock_cl;
sock_cl=socket(AF_INET,SOCK_STREAM,0);
printf("n creation de sock serv");
SOCKADDR_IN addrC;
addrC.sin_family=AF_INET;
addrC.sin_port=htons(1050);
addrC.sin_addr.s_addr=htonl(INADDR_ANY);
printf("n remplissage de sock_in");
connect(sock_cl,(SOCKADDR*)&addrC,sizeof(addrC));
printf("n bien connecter");
char buffer[100];
int n;
recv(sock_cl,buffer,sizeof(buffer),0);
n=strlen(buffer);
buffer[n]='0';
printf("n le mssg recu %s n",buffer);
strcpy(buffer," n hello n");
send(sock_cl,buffer,sizeof(buffer),0);
printf("send");
closesocket(sock_cl);
}
Udp
Serveur :
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
int main(){
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
/*la declaration de socket serveur */
SOCKET sock_serv;
/*la creation de socket serveur */
sock_serv = socket(AF_INET, SOCK_DGRAM, 0);
/*declaration et initialisation de la structure SOCKADDR_IN */
SOCKADDR_IN addrS;
addrS.sin_family = AF_INET;
addrS.sin_port = htons(1050);
addrS.sin_addr.s_addr = htonl(INADDR_ANY);
/* Bind socket */
if (bind(sock_serv, (SOCKADDR *)&addrS, sizeof(addrS)) ==
SOCKET_ERROR) {
printf("Error binding socketn");
return 1;
}
/* Preparer buffer et socket */
char buffer[100];
int recv_len;
SOCKADDR_IN client_addr;
int client_addr_len = sizeof(client_addr);
memset(buffer, 0, sizeof(buffer));
/* attend le message de client */
if ((recv_len = recvfrom(sock_serv, buffer, sizeof(buffer), 0,
(SOCKADDR *)&client_addr, (socklen_t *)&client_addr_len)) ==
SOCKET_ERROR) {
printf("Error receiving messagen");
return 1;
}
/* afficher le message recu message */
printf("Received message from client: %sn", buffer);
/* envoie de reponse au client */
char response[100] = "Hello ";
sendto(sock_serv, response, strlen(response), 0, (SOCKADDR
*)&client_addr, sizeof(client_addr));
/* fermer socket */
closesocket(sock_serv);
return 0;
}Client :
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
int main(){
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
/*la declaration de socket serveur */
SOCKET sock_cl;
/*la creation de socket serveur */
sock_cl=socket(AF_INET,SOCK_DGRAM,0);
/*declaration et initialisation de la structure SOCKADDR_IN */
SOCKADDR_IN addrC;
addrC.sin_family=AF_INET;
addrC.sin_port=htons(1050);
addrC.sin_addr.s_addr=INADDR_ANY;
/envoie du mssg au serveur/
char buffer[100];
strcpy(buffer," n hello ");
printf("le mssg envoi est :%s n",buffer);
sendto(sock_cl,buffer,sizeof(buffer),0,(SOCKADDR*)&addrC,sizeof(addrC)
);
/fermeture de socket/
close(sock_cl);
}

More Related Content

Similar to tp socket en C.pdf

INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptsenthilnathans25
 
#include stdio.h#include systypes.h#include syssocket.h
#include stdio.h#include systypes.h#include syssocket.h#include stdio.h#include systypes.h#include syssocket.h
#include stdio.h#include systypes.h#include syssocket.hSilvaGraf83
 
#include stdio.h#include systypes.h#include syssocket.h
#include stdio.h#include systypes.h#include syssocket.h#include stdio.h#include systypes.h#include syssocket.h
#include stdio.h#include systypes.h#include syssocket.hMoseStaton39
 
[4] 아두이노와 인터넷
[4] 아두이노와 인터넷[4] 아두이노와 인터넷
[4] 아두이노와 인터넷Chiwon Song
 
Dns server clients (actual program)
Dns server clients (actual program)Dns server clients (actual program)
Dns server clients (actual program)Youssef Dirani
 
Dns server clients (actual program)
Dns server clients (actual program)Dns server clients (actual program)
Dns server clients (actual program)Youssef Dirani
 
Socket programming
Socket programmingSocket programming
Socket programmingAnurag Tomar
 
Gaztea Tech Robotica 2016
Gaztea Tech Robotica 2016Gaztea Tech Robotica 2016
Gaztea Tech Robotica 2016Svet Ivantchev
 
Socket Programming Intro.pptx
Socket  Programming Intro.pptxSocket  Programming Intro.pptx
Socket Programming Intro.pptxssuserc4a497
 
Socket Programming Tutorial 1227317798640739 8
Socket Programming Tutorial 1227317798640739 8Socket Programming Tutorial 1227317798640739 8
Socket Programming Tutorial 1227317798640739 8shanmuga priya
 
Socket Programming Tutorial
Socket Programming TutorialSocket Programming Tutorial
Socket Programming TutorialJignesh Patel
 
Microcontroladores: programas de CCS Compiler.docx
Microcontroladores: programas de CCS Compiler.docxMicrocontroladores: programas de CCS Compiler.docx
Microcontroladores: programas de CCS Compiler.docxSANTIAGO PABLO ALBERTO
 

Similar to tp socket en C.pdf (20)

UDP.yash
UDP.yashUDP.yash
UDP.yash
 
sockets_intro.ppt
sockets_intro.pptsockets_intro.ppt
sockets_intro.ppt
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
 
#include stdio.h#include systypes.h#include syssocket.h
#include stdio.h#include systypes.h#include syssocket.h#include stdio.h#include systypes.h#include syssocket.h
#include stdio.h#include systypes.h#include syssocket.h
 
#include stdio.h#include systypes.h#include syssocket.h
#include stdio.h#include systypes.h#include syssocket.h#include stdio.h#include systypes.h#include syssocket.h
#include stdio.h#include systypes.h#include syssocket.h
 
Sockets
SocketsSockets
Sockets
 
[4] 아두이노와 인터넷
[4] 아두이노와 인터넷[4] 아두이노와 인터넷
[4] 아두이노와 인터넷
 
Dns server clients (actual program)
Dns server clients (actual program)Dns server clients (actual program)
Dns server clients (actual program)
 
Dns server clients (actual program)
Dns server clients (actual program)Dns server clients (actual program)
Dns server clients (actual program)
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
Npc14
Npc14Npc14
Npc14
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Socket programming in c
Socket programming in cSocket programming in c
Socket programming in c
 
Gaztea Tech Robotica 2016
Gaztea Tech Robotica 2016Gaztea Tech Robotica 2016
Gaztea Tech Robotica 2016
 
Socket Programming Intro.pptx
Socket  Programming Intro.pptxSocket  Programming Intro.pptx
Socket Programming Intro.pptx
 
Computer networkppt4577
Computer networkppt4577Computer networkppt4577
Computer networkppt4577
 
Socket Programming Tutorial 1227317798640739 8
Socket Programming Tutorial 1227317798640739 8Socket Programming Tutorial 1227317798640739 8
Socket Programming Tutorial 1227317798640739 8
 
Socket Programming Tutorial
Socket Programming TutorialSocket Programming Tutorial
Socket Programming Tutorial
 
Sysprog17
Sysprog17Sysprog17
Sysprog17
 
Microcontroladores: programas de CCS Compiler.docx
Microcontroladores: programas de CCS Compiler.docxMicrocontroladores: programas de CCS Compiler.docx
Microcontroladores: programas de CCS Compiler.docx
 

Recently uploaded

Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 

Recently uploaded (20)

Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 

tp socket en C.pdf

  • 1. Tp Socket C Tcp Serveur : #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <string.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define closesocket(s) close(s) int main(){ typedef int SOCKET; typedef struct sockaddr_in SOCKADDR_IN; typedef struct sockaddr SOCKADDR; SOCKET sock; sock=socket(AF_INET,SOCK_STREAM,0); printf("n creation de sock serv"); SOCKADDR_IN addrS; addrS.sin_family=AF_INET; addrS.sin_port=htons(1050); Réaliser par: Anas AOUINA
  • 2. addrS.sin_addr.s_addr=htonl(INADDR_ANY); printf(" n remplissage de sock_in"); bind(sock,(SOCKADDR*)&addrS,sizeof(addrS)); printf("n association d'info"); listen(sock,0); printf("n mettre en etat d'ecoute"); SOCKET so_client; printf("n declaration de sock client"); SOCKADDR_IN client; int taille=sizeof(client); so_client=accept(sock,(SOCKADDR*)&client,&taille); printf("n acceptation"); char buffer[100]; int n; strcpy(buffer,"Bonjour n"); send(so_client,buffer,sizeof(buffer),0); printf("n send"); recv(so_client,buffer,sizeof(buffer),0); n=strlen(buffer); buffer[n]='0'; printf("n le mssg recu %s n",buffer); closesocket(so_client);
  • 3. closesocket(sock); } Client : #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <stdio.h> #include <string.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define closesocket(s) close(s) int main(){ typedef int SOCKET; typedef struct sockaddr_in SOCKADDR_IN; typedef struct sockaddr SOCKADDR; SOCKET sock_cl; sock_cl=socket(AF_INET,SOCK_STREAM,0); printf("n creation de sock serv"); SOCKADDR_IN addrC; addrC.sin_family=AF_INET; addrC.sin_port=htons(1050); addrC.sin_addr.s_addr=htonl(INADDR_ANY); printf("n remplissage de sock_in");
  • 4. connect(sock_cl,(SOCKADDR*)&addrC,sizeof(addrC)); printf("n bien connecter"); char buffer[100]; int n; recv(sock_cl,buffer,sizeof(buffer),0); n=strlen(buffer); buffer[n]='0'; printf("n le mssg recu %s n",buffer); strcpy(buffer," n hello n"); send(sock_cl,buffer,sizeof(buffer),0); printf("send"); closesocket(sock_cl); } Udp Serveur : #include <arpa/inet.h> #include <unistd.h> #include <stdio.h> #include <string.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define closesocket(s) close(s) int main(){
  • 5. typedef int SOCKET; typedef struct sockaddr_in SOCKADDR_IN; typedef struct sockaddr SOCKADDR; /*la declaration de socket serveur */ SOCKET sock_serv; /*la creation de socket serveur */ sock_serv = socket(AF_INET, SOCK_DGRAM, 0); /*declaration et initialisation de la structure SOCKADDR_IN */ SOCKADDR_IN addrS; addrS.sin_family = AF_INET; addrS.sin_port = htons(1050); addrS.sin_addr.s_addr = htonl(INADDR_ANY); /* Bind socket */ if (bind(sock_serv, (SOCKADDR *)&addrS, sizeof(addrS)) == SOCKET_ERROR) { printf("Error binding socketn"); return 1; } /* Preparer buffer et socket */ char buffer[100]; int recv_len; SOCKADDR_IN client_addr; int client_addr_len = sizeof(client_addr); memset(buffer, 0, sizeof(buffer)); /* attend le message de client */ if ((recv_len = recvfrom(sock_serv, buffer, sizeof(buffer), 0, (SOCKADDR *)&client_addr, (socklen_t *)&client_addr_len)) == SOCKET_ERROR) {
  • 6. printf("Error receiving messagen"); return 1; } /* afficher le message recu message */ printf("Received message from client: %sn", buffer); /* envoie de reponse au client */ char response[100] = "Hello "; sendto(sock_serv, response, strlen(response), 0, (SOCKADDR *)&client_addr, sizeof(client_addr)); /* fermer socket */ closesocket(sock_serv); return 0; }Client : #include <arpa/inet.h> #include <unistd.h> #include <stdio.h> #include <string.h> #define INVALID_SOCKET -1 #define SOCKET_ERROR -1 #define closesocket(s) close(s) int main(){ typedef int SOCKET; typedef struct sockaddr_in SOCKADDR_IN; typedef struct sockaddr SOCKADDR; /*la declaration de socket serveur */ SOCKET sock_cl;
  • 7. /*la creation de socket serveur */ sock_cl=socket(AF_INET,SOCK_DGRAM,0); /*declaration et initialisation de la structure SOCKADDR_IN */ SOCKADDR_IN addrC; addrC.sin_family=AF_INET; addrC.sin_port=htons(1050); addrC.sin_addr.s_addr=INADDR_ANY; /envoie du mssg au serveur/ char buffer[100]; strcpy(buffer," n hello "); printf("le mssg envoi est :%s n",buffer); sendto(sock_cl,buffer,sizeof(buffer),0,(SOCKADDR*)&addrC,sizeof(addrC) ); /fermeture de socket/ close(sock_cl); }