Advertisement
Raviqa

sisiServer.c

Apr 7th, 2020
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.60 KB | None | 0 0
  1. // Nama : Raviqa Sandra Putri
  2. // Nim  : 1856401009
  3.  
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netdb.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <fcntl.h>
  11. #include <unistd.h>
  12. #include <netinet/in.h>
  13. #include <arpa/inet.h>
  14.  
  15. #define MAXBUF 1024
  16.  
  17. int main(int argc, char *argv[]){
  18.     int udpSocket;
  19.     int returnStatus = 0, returnStatus1 = 0;
  20.     int addrlen = 0;
  21.     struct sockaddr_in udpServer, udpClient;
  22.     char buf[MAXBUF];
  23.     char buf1[MAXBUF];
  24.  
  25.     if(argc < 2){
  26.         fprintf(stderr, "Usage : %s <port>\n", argv[0]);
  27.         exit(1);
  28.     }
  29.  
  30.     udpSocket = socket(AF_INET, SOCK_DGRAM,0);
  31.  
  32.     if(udpSocket == -1) {
  33.         fprintf(stderr, "Could not create a socket !\n");
  34.         exit(1);
  35.     }else{
  36.         printf("Socket Create\n");
  37.     }
  38.  
  39.     udpServer.sin_family = AF_INET;
  40.     udpServer.sin_addr.s_addr = htonl(INADDR_ANY);
  41.  
  42.     udpServer.sin_port = htons(atoi(argv[1]));
  43.     returnStatus = bind(udpSocket, (struct sockaddr*)&udpServer, sizeof(udpServer));
  44.  
  45.     if(returnStatus == 0){
  46.         fprintf(stderr, "Bind Complate\n");
  47.     } else {
  48.         fprintf(stderr, "Could not bind to address!\n");
  49.         close(udpSocket);
  50.         exit(1);
  51.     }
  52.  
  53.     while(1){
  54.     addrlen = sizeof(udpClient);
  55.     returnStatus = recvfrom(udpSocket, buf, MAXBUF,0,(struct sockaddr*)&udpClient, &addrlen);
  56.     returnStatus1 = recvfrom(udpSocket, buf1, MAXBUF,0,(struct sockaddr*)&udpClient, &addrlen);
  57.     if(returnStatus == -1 && returnStatus1 == -1){
  58.         fprintf(stderr, "Could not receive message!\n");
  59.     }
  60.     else {
  61.         printf("Received Username : %s\n",buf);
  62.         printf("Received Password : %s\n",buf1);
  63.         //printf("login Berhasil!\n");
  64.         if(strcmp(buf, "auliya") == 0 && strcmp(buf1, "rahmi") == 0){
  65.             strcpy(buf, "Login Berhasil\n");
  66.             returnStatus = sendto(udpSocket, buf, strlen(buf)+1, 0, (struct sockaddr*)&udpClient, sizeof(udpClient));
  67.             if(returnStatus == -1){
  68.                 fprintf(stderr, "Could not sent confirmation!\n");
  69.             }
  70.             else {
  71.             printf("confirmation success sent.\n");
  72.             }
  73.         }
  74.         else {
  75.         strcpy(buf, "Gagal connect to server");
  76.         returnStatus = sendto(udpSocket, buf, strlen(buf)+1, 0, (struct sockaddr*)&udpClient, sizeof(udpClient));
  77.         if(returnStatus == -1 ){
  78.             fprintf(stderr, "Could not send confirmation!\n");
  79.         }
  80.         else{
  81.             printf("confirmation Failure sent.\n");
  82.  
  83.         }
  84.     }
  85.  
  86. }
  87.     }
  88.     close(udpSocket);
  89.     return 0;
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement