Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************MIROIR*****************************************/
- /*****************************************************************************************/
- int main(int argc, char * argv[]) {
- // port client
- unsigned short portMiroirTCP;
- unsigned short portTestUDP;
- // socket client
- int sockTCP_RDV,sockTCP_Service,sockUDP;
- struct sockaddr_in addr_TestTCP,addr_TestUDP;
- struct sockaddr_in addr_MiroirTCP;
- struct hostent * hostMiroir,* hostTest;
- // Declaration des buffers
- int buf[1];
- /* Creation de la socketTCP*/
- if ((sockTCP_RDV = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
- perror("socket");
- exit(EXIT_FAILURE);
- }
- // adresse IP Test
- if ((hostTest = gethostbyname(argv[1])) == NULL) {
- perror("gethostbyname");
- exit(EXIT_FAILURE);
- }
- // adresse IP miroir
- if ((hostMiroir = gethostbyname("localhost")) == NULL) {
- perror("gethostbyname");
- exit(EXIT_FAILURE);
- }
- /*Preparation de l'adresse local*/
- portMiroirTCP = (unsigned short) 5000;
- addr_MiroirTCP.sin_family = AF_INET;
- addr_MiroirTCP.sin_port = htons(portMiroirTCP);
- bcopy(hostMiroir->h_addr, &addr_MiroirTCP.sin_addr, hostMiroir->h_length);
- /*Attachement de la socket */
- int lg_addr_MiroirTCP = sizeof(addr_MiroirTCP);
- if((bind(sockTCP_RDV,(struct sockaddr *)&addr_MiroirTCP,lg_addr_MiroirTCP))== -1){
- perror("bind");
- exit(-1);
- }
- /* OUVERTURE DU SERVICE*/
- if(listen(sockTCP_RDV,10)== -1){
- perror("listen");
- exit(-1);
- }
- /* Attente du client */
- socklen_t lg_addr_TestTCP=sizeof(addr_MiroirTCP);
- sockTCP_Service=accept(sockTCP_RDV,(struct sockaddr *)&addr_TestTCP,(socklen_t *)&lg_addr_TestTCP);
- if(sockTCP_Service==-1 && errno==EINTR){
- }
- if(sockTCP_Service == -1){
- perror("accept");
- exit(-1);
- }
- printf("Testeur connecte\n");
- fflush(stdout);
- if(read(sockTCP_Service, buf,sizeof(int))<0){
- perror("read");
- exit(-1);
- }
- printf("N = %d\n",buf[0]);
- /* Creation de la socketUDP*/
- if ( (sockUDP = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
- perror("socket creation failed");
- exit(EXIT_FAILURE);
- }
- /*Preparation de l'adresse local*/
- portTestUDP = (unsigned short) 4000;
- addr_TestUDP.sin_family = AF_INET;
- addr_TestUDP.sin_port = htons(portTestUDP);
- bcopy(hostTest->h_addr, &addr_TestUDP.sin_addr, hostTest->h_length);
- int lg_addr_TestUDP= sizeof(addr_TestUDP);
- int envoye,i;
- for(i=0;i<buf[0];i++){
- if((envoye=sendto(sockUDP,&i,sizeof(int),0,(const struct sockaddr *)&addr_TestUDP,lg_addr_TestUDP)) != sizeof(int)){
- perror("sendto");
- exit(-1);
- }
- printf("message %d envoye\n",i);
- }
- char fin[4]="FIN";
- ssize_t wr;
- if ((wr = write(sockTCP_Service, fin, strlen(fin)+1)) != strlen(fin)+1) {
- perror("write");
- exit(EXIT_FAILURE);
- }
- printf("FIN envoye\n");
- close(sockTCP_RDV);
- close(sockTCP_Service);
- close(sockUDP);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement