Programming
How to resolve “Address already in use” error with bind system call
Jul 26th
To get rid of “Address already in use” error with bind system call, SO_REUSEADDR socket option should be set before invoking bind. This makes operating system to allow the socket to bind if there is no active socket bound to the requested address and port. Add the following code to the code snippet provided in “Find peer IP address and port number using getpeername” article.
/* create a socket */
sd = socket(AF_INET,SOCK_STREAM,0);
opt = 1;
if(setsockopt (sd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt)) < 0) {
perror("setsockopt");
exit(1);
}
...
Find local IP address and local port number using getsockname
Jul 26th
The getsockname function can be used to find the local port used by a given socket. It is valid to use this function only if the socket is already using a local port number. i.e A server socket which invoked bind system call with zero as port number. A client socket which has established connection to a server. In either case operating system selects an unused port number automatically. Similarly this function can also be used on UDP sockets.
The following code snippet explains how to use getsockname to retrieve the local port number assigned by operating system.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include More >
Find peer IP address and port number using getpeername
Jul 26th
The getpeername function can be used to retrieve the peer IP address and port number on a given socket. For this to work, the socket should have a valid TCP connection established. Typically this is used by server to find the IP address and port number of client.
In the following code snippet, a TCP server binds to port number 5555 and waits for clients. It retrieves client’s IP address and port number using getpeername if a client is connected.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/un.h>
int main()
{
struct sockaddr_in sin; More > Print first N characters of a string using printf
Jul 26th
#include <stdio.h>
int main(int argc, char *argv[])
{
char *p = "My Name is Neo";
printf("First 5 bytes of string is '%.5s'\n", p);
return 0;
}
Result
[neo@techpulp ex]# gcc example.c -o example [neo@techpulp ex]# ./example First 5 bytes of string is 'My Na' [neo@techpulp ex]#
Stack of a Running Process in Unix/Linux
Jul 26th
The command pstack can be used to view the stack trace of a running process.
Let us select a running process.
[neo@techpulp ~]# ps -e | grep konqueror 22937 pts/5 00:00:17 konqueror
Run pstack command with PID of the process.
[neo@techpulp ~]# pstack 22937 22937: konqueror (No symbols found) 0x001fb402: ???? (89c6ed8, 2, bfa39b08, 3812df0, 89c6ed8, 3c21362) 0x0325074b: ???? (89c6ed8, 8a00908) 0x03250656: ???? (89c6ed8, 3c372ec) 0x03237a59: ???? (bfa39c20, bfa39d48, bfa39de8, bfa39d1c, 0, bfa39fb4) + 4c0 0x03bb1d0d: ???? (1, bfa3a094, bfa3a09c, 0, bfa3a094, 1) + 40 0x00248de6: ???? (80485ec, 1, bfa3a094, 8048600, 8048650, 21ff2d) + 405c5f78 [neo@techpulp ~]#
There is an alternate way to use gdb and attach More >
ELF Binary Optimization for size
Jul 26th
The following steps help in reducing the size of a Linux application ELF binary. These operations can be used for application binary as well as shared library.
Use compiler optimization flags
-Os (optimize for size)
-O2
While linking, try to use following options. But do not use ‘-s’ option for kernel modules.
# ld -r -s --discard-local --discard-all testApp.o -o testApp # ld -r -s --discard-local --discard-all testLib1.o testLib2.o -o testLib.so # ld -r --discard-local --discard-all testMod1.o testMod2.o -o testMod
Strip the binary including unnecessary sections
# strip --remove-section=.note --remove-section=.comment testApp # strip --remove-section=.note --remove-section=.comment testLib.so
Parse words in a string
Jul 26th
This function requires an array of character pointers words of size maxWords. Note that the input string line supplied to this function will be modified (precisely placing NULL termination character at each word boundary). Alternately the function can be modified to duplicate strings using strdup to avoid modification of input string line. This function returns the number of words parsed. The return value will never exceed maxWords.
int parseWords(char *line, char *words[], int maxWords)
{
char *p;
int wordCount;
p = line;
wordCount = 0;
while(wordCount < maxWords) {
while(*p && isblank(*p)) p++;
if(!p[0]) return wordCount;
words[wordCount] = p;
wordCount++;
while(*p && !isblank(*p)) p++; More > 

Recent Comments