Thursday, January 30, 2014

Understanding Socket Programming Part 1

What is a Socket?

  • Allows to communicate between two process on the same or different machines.
  • A way to talk to other computers using standard Unix file descriptors(Unix File Descriptors- click here)

Assume a situation in which a client want to connect to a server. The client knows the hostname of the machine that runs the server and also the port number at which the server listens for connection.

A server is a process which does some function on request from a client. Most of the application level protocols like FTP, SMTP and POP3 make use of Sockets to establish connection between client and server and then for exchanging data.




Client sent a connection request to server: The client tries to rendezvous with the server on the server's machine and port.

Server accepts connection: The server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client







Definition

socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent.


Links that could help you:

http://programmers.stackexchange.com/questions/171734/difference-between-a-socket-and-a-port
http://www.tutorialspoint.com/unix_sockets/what_is_socket.htm


Tags:-client, server, socket, programming,file descriptor

Monday, January 13, 2014

How to redirect output of a running process to a file and how to overwrite a file

I am writing this when i had a need to redirect the output of a running process to a file. For example in the case of output redirection

 airodump-ng mon0 > file.txt  

this won't work. The reason behind it is that usually a process may send output to standard output or standard error. In general case former is for information and latter is for errors. In certain cases both may get mixed up. In the above case standard error is used and we need to pipe it to the file.
It can be done by

 airodump-ng mon0 > file.txt 2>&1  

This says to send standard ouptut to file file.txt and reroute 2 (which is file id for standard error) into 1 (file id for standard output).
Over writing

By default when a running process is piped to a file, the output gets appended at each instant when a new output (stdout or stderr) is generated. But if we want to overwrite the previous output we can use,  the following code to do so

  airodump-ng mon0 &> file.txt 2>&1   

Tags:- output-redirection,piping, bash, shell, process, overwriting,file-overwriting

Sunday, January 5, 2014

Running another Process inside the C program

There is a function called system( ) to run a process inside a c program.
Protoype:  int system(const char * command)
System( ) executes a command specified in command by calling /bin/sh - c command and returns after the command has been completed.
It returns -1 if there is an error.

example:
system("/etc/init.d/networking stop");

Tags:-system, process,command