SHARE
TWEET

Untitled

a guest Dec 12th, 2018 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "stdio.h"
  2. #include "string.h"
  3. #include "sys/types.h"
  4. #include "sys/socket.h"
  5. #include "stdlib.h"
  6. #include "arpa/inet.h"
  7.  
  8. #define PORT 42120
  9. #define IP "127.0.0.1"
  10.  
  11. int main(int argc, char const *argv[])
  12. {
  13.     struct sockaddr_in serwer =
  14.     {
  15.         .sin_family = AF_INET,
  16.         .sin_port = htons( PORT )
  17.     };
  18.  
  19.     int soc;
  20.     char buffer[ 4096 ] = { };
  21.     inet_pton( AF_INET, IP, & serwer.sin_addr );
  22.     soc = socket(AF_INET, SOCK_DGRAM, 0);
  23.    
  24.     socklen_t len = sizeof( serwer );
  25.     bind( soc,( struct sockaddr * ) & serwer, len );
  26.  
  27.     while(1)
  28.     {
  29.         struct sockaddr_in client = {};
  30.         memset(buffer, 0, sizeof( buffer ));
  31.         //printf("OczekujÄ™ na poĹ‚Ä…czenie...\n");
  32.         recvfrom( soc, buffer, sizeof( buffer ), 0,( struct sockaddr * ) & client, & len );
  33.         printf("Message %s \n", buffer);
  34.     shutdown( soc, SHUT_RDWR );
  35.     //memset(buffer, 0, sizeof( buffer ));
  36.     return 0;
  37.        
  38.     }
  39.     //shutdown( soc, SHUT_RDWR );
  40.     //return 0;
  41. }
  42.  
  43. _____________________________________________
  44.  
  45.  
  46.  
  47. #include "stdio.h"
  48. #include "string.h"
  49. #include "sys/types.h"
  50. #include "sys/socket.h"
  51. #include "stdlib.h"
  52. #include "arpa/inet.h"
  53.  
  54. #define PORT 42120
  55. #define IP "127.0.0.1"
  56.  
  57. int main(int argc, char const *argv[])
  58. {
  59.     struct sockaddr_in serwer =
  60.     {
  61.         .sin_family = AF_INET,
  62.         .sin_port = htons( PORT )
  63.     };
  64.  
  65.     int soc;
  66.     char buffer[ 4096 ];
  67.     inet_pton( AF_INET, IP, & serwer.sin_addr );
  68.         soc = socket(AF_INET, SOCK_DGRAM, 0);
  69.    
  70.     printf("message: ");
  71.     fgets(buffer, sizeof(buffer), stdin);
  72.    
  73.     socklen_t len = sizeof( serwer );
  74.     bind( soc,( struct sockaddr * ) & serwer, len );
  75.     sendto( soc, buffer, strlen( buffer ), 0,( struct sockaddr * ) & serwer, len );
  76.  
  77.     shutdown( soc, SHUT_RDWR );
  78.     return 0;
  79.    
  80. }
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