Category Archives: C

Socket Programming in C

Receive full data with recv socket function in C

By | September 8, 2012

recv The recv function is used to receive data on a socket. For example here is the code to fetch the home page of www.msn.com /** Simple TCP client to fetch a web page */ #include<stdio.h> #include<string.h> //strlen #include<sys/socket.h> #include<arpa/inet.h> //inet_addr int main(int argc , char *argv[]) { int socket_desc; struct sockaddr_in server; char *message… Read More »

C program to get mac address from interface name on Linux

By | March 17, 2012

The mac address or the hardware address or the ethernet address of an interface is a 48 bit number that looks like this : 00:1c:c0:f8:79:ee The mac address of an interface can be found given its name. The function to use is ioctl. Code #include <stdio.h> //printf #include <string.h> //strncpy #include <sys/socket.h> #include <sys/ioctl.h> #include… Read More »

C code to perform IP whois

By | February 16, 2012

Theory The whois information of an ip address provides various details like its network, range, isp etc. This information is maintained by various regional registry servers. Read the wikipedia article on regional internet registries for more information. There are a total of 5 regional registries spanning various geographical regions of the world. For example if… Read More »

Handle multiple socket connections with fd_set and select on Linux

By | December 25, 2011

Handle multiple socket connections When writing server programs using sockets , it becomes necessary to handle multiple connections at a time , since a server needs to serve multiple clients. There are many ways to do so. On linux this can be done in various ways like forking , threading , select method etc. In… Read More »