Browsing articles tagged with

c

Disable output buffering on stdout in c

Like other languages output buffering takes place even in C. Output buffering will cause output of a program to appear in chunks after long intervals, instead of each line appearing the moment it is output. To disable output buffering a very strange piece of code has to be written which looks like this //disable output buffering setvbuf(stdout, NULL, _IONBF, 0); printf('Hello'); Or call the ...

C program to get mac address from interface name on Linux

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 <net/if.h> //ifreq #include <unistd.h> //close int main() { int fd; struct ifreq ifr; char *iface = "eth0"; unsigned char *mac; fd = socket(AF_INET, SOCK_DGRAM, ...

C program to get ip address from interface name on Linux

Interface name is something like "eth0" and the ip address of the interface can be retrieved using the ioctl function. Here is a simple piece of code that demonstrates how : Code #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <netinet/in.h> #include <net/if.h> #include <unistd.h> #include <arpa/inet.h> int main() { int fd; struct ifreq ifr; char iface = "eth0"; fd = socket(AF_INET, SOCK_DGRAM, 0); //Type of address to retrieve - IPv4 IP address ifr.ifr_addr.sa_family = AF_INET; //Copy ...

Get time difference in microtime in C

The function gettimeofday can be used to retrieve the total number of seconds and balance microseconds elapsed since EPOCH. Structure timeval looks like this : struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ }; On Linux it can be done like ...

Data type u_int8_t , u_int16_t , u_int32_t on windows

On Linux the header file sys/types.h provides the fixed sized integer data types like these : u_int8_t; u_int16_t; u_int32_t; On windows the header file stdint.h provides similar ones but with slightly different names , uint8_t etc. So if you are writing cross platform code you can get the same ones on windows as in a manner shown below : #if defined(_WIN32) #include <stdint.h> typedef uint8_t u_int8_t; typedef uint16_t ...

C code to perform IP whois

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 an ip address is allocated to some ...

Function pread for windows

The pread function is used to read N bytes from a stream/file from a given offset point. It is natively present on linux/unix but not on windows. However its easy to implement it on windows using the lseek and read functions. Here is an example #include <io.h> #include <stdio.h> int pread(unsigned int fd, char *buf, size_t count, int offset) { if (_lseek(fd, offset, SEEK_SET) != ...

Handle multiple socket connections with fd_set and select on Linux

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 this tutorial we shall use the select method ...

C program to get a domain’s whois information using sockets on Linux

Whois A whois client is a program that will simply fetch the whois information for a domain/ip address from the whois servers. The code over here works according to the algorithm discussed here. Code /* * @brief * Whois client program * * @details * This program shall perform whois for a domain and get you the whois data of that domain ...

str_replace for C

Php has a useful function called str_replace which can search and replace a certain string in another big string. However there is no such function in C. So I wrote up one for myself. Here is the code. /* * Search and replace a string with another string , in a string * */ char *str_replace(char *search , char *replace , char ...

Pages:123»


Connect with us