SHARE
TWEET

Untitled

a guest Nov 22nd, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <arpa/inet.h>
  8. #include <netinet/in.h>
  9.  
  10. struct packet {
  11.     char oper[5];
  12.     int int1;
  13.     int int2;
  14. };
  15.  
  16. struct resultpacket {
  17.     int success;
  18.     int result;
  19. };
  20.  
  21. // Driver code
  22. int main() {
  23.     int sockfd;
  24.     char buffer[10];
  25.     char *hello = "Hello from server";
  26.     struct sockaddr_in servaddr, cliaddr;
  27.    
  28.     // Creating socket file descriptor
  29.     if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
  30.         perror("socket creation failed");
  31.         exit(EXIT_FAILURE);
  32.     }
  33.    
  34.     memset(&servaddr, 0, sizeof(servaddr));
  35.     memset(&cliaddr, 0, sizeof(cliaddr));
  36.    
  37.     // Filling server information
  38.     servaddr.sin_family = AF_INET; // IPv4
  39.     servaddr.sin_addr.s_addr = INADDR_ANY;
  40.     servaddr.sin_port = htons(8080);
  41.    
  42.     // Bind the socket with the server address
  43.     if ( bind(sockfd, (const struct sockaddr *)&servaddr,
  44.             sizeof(servaddr)) < 0 )
  45.     {
  46.         perror("bind failed");
  47.         exit(EXIT_FAILURE);
  48.     }
  49.    
  50.     int len, n;
  51.     n = recvfrom(sockfd, (char *)buffer, 10,
  52.                 MSG_WAITALL, ( struct sockaddr *) &cliaddr,
  53.                 &len);
  54.     buffer[n] = '\0';
  55.     printf("Client : %s\n", buffer);
  56.     sendto(sockfd, (const char *)hello, strlen(hello),
  57.         MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
  58.             len);
  59.     printf("Hello message sent.\n");
  60.    
  61.     return 0;
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top