Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

client.c

By: freieschaf on May 6th, 2012  |  syntax: None  |  size: 1.72 KB  |  views: 31  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <sys/param.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <sys/time.h>
  6. #include <netdb.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <errno.h>
  12. #include <signal.h>
  13. #include <wait.h>
  14.  
  15. void main () {
  16.         int i, verbose = 0, port_number, rexmt;
  17.         char option, hostname[16];
  18.         /* Socket data */
  19.         int in_socket_id, socket_id, server_socket_sz;
  20.         struct sockaddr_in client_socket, server_socket, in_socket;
  21.         struct hostent *hostp;
  22.  
  23.         /* Transmission data */
  24.         char msg_buffer[1024], data_buffer[512], error[300], ack[4];
  25.         int offset = 0, bytes_rcvd, ack_recvd;
  26.         unsigned short block_number, opcode;
  27.  
  28.         if ((socket_id = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
  29.                         return;
  30.         }
  31.         in_socket.sin_family = AF_INET;
  32.         in_socket.sin_port = htons(0);
  33.         in_socket.sin_addr.s_addr = INADDR_ANY;
  34.  
  35.         if (bind(socket_id, (struct sockaddr *) &in_socket, sizeof(in_socket)) == -1) {
  36.                 return;
  37.         }
  38.  
  39.         if ((hostp = gethostbyname(hostname)) == 0) {
  40.                 return;
  41.         }
  42.         bzero((char *) &client_socket, sizeof(client_socket));
  43.         client_socket.sin_family = AF_INET;
  44.         client_socket.sin_port = htons(8724);
  45.         bcopy(hostp->h_addr, (char *) &client_socket.sin_addr, hostp->h_length);
  46.  
  47.         bzero(msg_buffer,sizeof(msg_buffer));
  48.         memcpy(msg_buffer,"mensaje\0",sizeof("mensaje\0"));
  49.  
  50.         socklen_t client_socket_len = sizeof(struct sockaddr_in);
  51.         if (sendto(socket_id, msg_buffer, sizeof(msg_buffer), 0, (struct sockaddr *) &client_socket, client_socket_len) < 0) {
  52.                 return;
  53.         }
  54.         bzero(msg_buffer,sizeof(msg_buffer));
  55.         if(recvfrom(socket_id, msg_buffer, sizeof(msg_buffer), 0, (struct sockaddr *) &client_socket, &client_socket_len) == -1) {
  56.                 return;
  57.         }
  58.         printf("%s\n", msg_buffer);
  59. }
clone this paste RAW Paste Data