Advertisement
heavenriver

socket.c

Nov 27th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. /* basic socket operations, UNIX-native */
  2.  
  3.  /* pointer arithmetics:
  4.   *
  5.   * push(pointer, v) INDICATES *(pointer++) = v
  6.   * pop(pointer) INDICATES *(--pointer)
  7.   * (*++v)[0] INDICATES (**++v)
  8.   *           INDICATES first char in v
  9.   *           INDICATES name of string/vector v
  10.   * likewise, *v[0] INDICATES **v
  11.   *       and *v[n] INDICATES **(v + n)
  12.   * returntype (*funct)(args) INDICATES a function funct with arguments args which returns...
  13.   * char **argv INDICATES pointer to char pointer
  14.   * int(*v)[len] INDICATES pointer "v" to a vector of "len" int elements
  15.   * int *v[len] INDICATES vector "v" of "len" pointers to int elements
  16.   * void *funct() INDICATES function "funct" that returns a pointer-to-void
  17.   * void (*funct)() INDICATES pointer to a function "funct" that returns void
  18.   *
  19.   */
  20.  
  21.  /* useful characters: [] # */
  22.  
  23.  # include <stdio.h>
  24.  # include <stdlib.h> // for exit
  25.  # include <sys/types.h>
  26.  # include <sys/socket.h>
  27.  # include <netinet/in.h>
  28.  # include <netdb.h>
  29.  # include <fcntl.h> // for O_RDWR
  30.  
  31.  # define BUFFER 256
  32.  # define exception(x) { puts(x); exit(1); }
  33.  # define h_addr h_addr_list[0]
  34.  
  35. struct socket_in {
  36.    short family; // domain, e.g. AF_INET
  37.    unsigned short port;
  38.    struct in_addr addr; // IP number
  39.    char zero[8]; // unused bytes
  40.  };
  41.  
  42.  int main(int argc, char * argv[])
  43.     {
  44.      /* creation of a TCP socket */
  45.      int sock;
  46.      struct socket_in mysock;
  47.      sock = socket(AF_INET, SOCK_STREAM, 0); // Internet, TCP, type 0 (default)
  48.      mysock.family = AF_INET;
  49.      mysock.port = 1999;
  50.      mysock.addr.s_addr = INADDR_ANY; // can receive data from any address or socket
  51.      
  52.      
  53.      /* binding */
  54.      /* SOCK and MYSOCK will be used without being reinstanced */
  55.      char c;
  56.      bind(sock, &mysock, sizeof(mysock)); // binds sock and mysock
  57.      if(fork() != 0) close(sock); // in parent process: closes socket
  58.      else // in child process: uses socket
  59.     {
  60.      while(read(0, &c, 1) == -1); // waits until there is no more data to read
  61.      close(sock);
  62.     }
  63.      
  64.      
  65.      /* accept */
  66.      /* SOCK will be used without being reinstanced */
  67.      int sock_acc;
  68.      struct sockaddr address;
  69.      int addrlen;
  70.      sock_acc = accept(sock, &address, &addrlen); // accepts communication from sock
  71.      close(sock);
  72.      close(sock_acc);
  73.      
  74.      
  75.      /* connect */
  76.      /* SOCK and MYSOCK will be used without being reinstanced */
  77.      int length, success;
  78.      struct hostent * hp; // used in gethostbyname() to save the return value
  79.      hp = gethostbyname("conde.dis.uniroma1.it");
  80.      memcpy(&mysock.addr, hp->h_addr, 4); // copying from address to hp.address ???
  81.      success = connect(sock, &mysock, sizeof(mysock)); // attempts connection
  82.      if(success == -1) exception("Connection error\n");
  83.      close(sock);
  84.      
  85.      return 0;
  86.     }
  87.  
  88.  // the program segfaults, why?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement