Want more features on Pastebin? Sign Up, it's FREE!
Guest

server.c

By: freieschaf on May 6th, 2012  |  syntax: None  |  size: 1.40 KB  |  views: 19  |  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, offset = 0, port_number, verbose = 0;
  17.         char option;
  18.  
  19.         int socket_id, chld_socket_id, client_socket_sz, chld_socket_sz, dgram_length, ack_recvd;
  20.         struct sockaddr_in server_socket, client_socket, chld_socket;
  21.  
  22.         char dgram_buffer[1024], msg_buffer[1024], data_buffer[512];
  23.         unsigned short op_code, block_number = 0;
  24.         char filename[512], mode[16], error[300];
  25.  
  26.         if ((socket_id = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
  27.                 return;
  28.         }
  29.  
  30.         bzero(&server_socket, sizeof(server_socket));
  31.         server_socket.sin_family = AF_INET;
  32.         server_socket.sin_addr.s_addr = INADDR_ANY;
  33.         server_socket.sin_port = htons(8724);
  34.  
  35.         if (bind(socket_id, (struct sockaddr *) &server_socket, sizeof(server_socket)) == -1) {
  36.                 return;
  37.         }
  38.  
  39.         bzero(dgram_buffer, 1024);
  40.         client_socket_sz = sizeof(client_socket);
  41.         if((dgram_length = recvfrom(socket_id, dgram_buffer, 1024, 0, (struct sockaddr *) &client_socket, (socklen_t *) &client_socket_sz)) == -1) {
  42.  
  43.                 return;
  44.         }
  45.         if (sendto(socket_id, msg_buffer, offset, 0, (struct sockaddr *) &client_socket, (socklen_t) client_socket_sz) < 0) {
  46.  
  47.                 return;
  48.         }
  49.         printf("%s\n", msg_buffer);
  50. }
clone this paste RAW Paste Data