Sunday, 15 May 2011

IMPORTANT FUNCTIONS

I will mention some of the important functions used in socket programming and which header files to include in your program:

socket()
 #include <sys/types.h>
 #include <sys/socket.h>

 int socket(int domain,int type,int protocol);


 Let's see the arguments:

  domain -> you can set "AF_INET" (set AF_INET to use ARPA internet protocols)
  or "AF_UNIX" if you want to create sockets for inside comunication.
  Those two are the most used, but don't think that there are just
  those. There are more I just don't mention them.
  type -> here you put the kind of socket you want (Stream or Datagram)
  If you want Stream Socket the type must be SOCK_STREAM
  If you want Datagram Socket the type must be SOCK_DGRAM
  protocol -> you can just set protocol to 0


 socket() gives you a socket descriptor that you can use in later system calls or
 it gives you -1 on error (this is usefull for error checking routines).

bind()


 #include <sys/types.h>
 #include <sys/socket.h>

 int bind(int fd, struct sockaddr *my_addr,int addrlen);


 Let's see the arguments:

  fd -> is the socket file descriptor returned by socket() call
  my_addr -> is a pointer to struct sockaddr
  addrlen -> set it to sizeof(struct sockaddr)

 bind() is used when you care about your local port (usually when you use listen() )
 and its function is to associate a socket with a port (on your machine). It returns
 -1 on error.

 You can put your IP address and your port automatically:

  server.sin_port = 0; /* bind() will choose a random port*/
  server.sin_addr.s_addr = INADDR_ANY; /* puts server's IP automatically */


 An important aspect about ports and bind() is that all ports bellow 1024 are reserved.
 You can set a port above 1024 and bellow 65535 (unless the ones being used by other
 programs).

connect()

 #include <sys/types.h>
 #include <sys/socket.h>

 int connect(int fd, struct sockaddr *serv_addr, int addrlen);

 Let's see the arguments:

  fd -> is the socket file descriptor returned by socket() call
  serv_addr -> is a pointer to struct sockaddr that contains destination IP
  address and port
  addrlen -> set it to sizeof(struct sockaddr)

 connect() is used to connect to an IP address on a defined port. It returns -1 on
 error.


 listen()
 #include <sys/types.h>
 #include <sys/socket.h>

 int listen(int fd,int backlog);

 Let's see the arguments:

  fd -> is the socket file descriptor returned by socket() call
  backlog -> is the number of allowed connections

 listen() is used if you're waiting for incoming connections, this is, if you want
 someone to connect to your machine. Before calling listen(), you must call bind()
 or you'll be listening on a random port =) After calling listen() you must call
 accept() in order to accept incoming connection. Resuming, the sequence of system calls
 is:

  1. socket()
  2. bind()
  3. listen()
  4. accept() /* In the next section I'll explain how to use accept() */

 As all functions above described, listen() returns -1 on error.


accept()

 #include <sys/socket.h>

 int accept(int fd, void *addr, int *addrlen);

 Let's see the arguments:

  fd -> is the socket file descriptor returned by listen() call
  addr -> is a pointer to struct sockaddr_in where you can determine which host
  is calling you from which port
  addrlen -> set it to sizeof(struct sockaddr_in) before its address is passed
  to accept()

 send()

  int send(int fd,const void *msg,int len,int flags);

 Let's see the arguments:

  fd -> is the socket descriptor where you want to send data to
  msg -> is a pointer to the data you want to send
  len -> is the length of the data you want to send (in bytes)
  flags -> set it to 0


 This function is used to send data over stream sockets or CONNECTED datagram sockets.
 If you want to send data over UNCONNECTED datagram sockets you must use sendto().

 send() returns the number of bytes sent out and it will return -1 on error.


recv()

 int recv(int fd, void *buf, int len, unsigned int flags);

 Let's see the arguments:

  fd -> is the socket descriptor to read from
  buf -> is the buffer to read the information into
  len -> is the maximum length of the buffer
  flags -> set it to 0


 As I said above about send(), this function is used to send data over stream sockets or
 CONNECTED datagram sockets. If you want to send data over UNCONNECTED datagram sockets
 you must use recvfrom().

 recv() returns the number of bytes read into the buffer and it'll return -1 on error.


sendto()
 
 int sendto(int fd,const void *msg, int len, unsigned int flags,
  const struct sockaddr *to, int tolen);

 Let's see the arguments:

  fd -> the same as send()
  msg -> the same as send()
  len -> the same as send()
  flags -> the same as send()
  to -> is a pointer to struct sockaddr
  tolen -> set it to sizeof(struct sockaddr)

 As you can see, sendto() is just like send(). It has only two more arguments : "to"
 and "tolen" =)

 sendto() is used for UNCONNECTED datagram sockets and it returns the number of bytes
 sent out and it will return -1 on error.


recvfrom()

 int recvfrom(int fd,void *buf, int len, unsigned int flags
  struct sockaddr *from, int *fromlen);

 Let's see the arguments:

  fd -> the same as recv()
  buf -> the same as recv()
  len -> the same as recv()
  flags -> the same as recv()
  from -> is a pointer to struct sockaddr
  fromlen -> is a pointer to a local int that should be initialised
  to sizeof(struct sockaddr)

 recvfrom() returns the number of bytes received and it'll return -1 on error.


  close()

 close(fd);

 close() is used to close the connection on your socket descriptor. If you call close(),
 it won't be no more writes or reads and if someone tries to read/write will receive an
 error.


shutdown()

 int shutdown(int fd, int how);

 Let's see the arguments:

  fd -> is the socket file descriptor you want to shutdown
  how -> you put one of those numbers:
 
  0 -> receives disallowed
  1 -> sends disallowed
  2 -> sends and receives disallowed

 When how is set to 2, it's the same thing as close().

 shutdown() returns 0 on success and -1 on error.


 gethostname()

 #include <unistd.h>

 int gethostname(char *hostname, size_t size);

 Let's see the arguments:

  hostname -> is a pointer to an array that contains hostname
  size -> length of the hostname array (in bytes)


 gethostname() is used to get the name of the local machine.

No comments:

Post a Comment