Recent Notes

Displaying keyword search results 1 - 10
Created by magnum on October 08, 2014 11:29:18    Last update: October 08, 2014 11:29:18
Example C code to set the "Type Of Service" (TOS) field, lifted from http://stackoverflow.com/questions/11429403/how-to-set-message-priority-using-tos . #include <string.h> #include <netdb.h> i...
Created by voodoo on March 24, 2013 13:44:47    Last update: March 29, 2013 13:08:31
Use getpwnam group of functions. Example code: #include <sys/types.h> #include <pwd.h> #inc... For gid, use getgrnam
Created by magnum on November 03, 2012 08:57:35    Last update: November 03, 2012 08:57:35
Code snippet to check user name and password in C in Linux #include <sys/types.h> #include <pwd.h> #inc...
Created by magnum on October 22, 2012 20:03:05    Last update: October 22, 2012 20:03:05
First, the test command that sleeps random number of seconds ( sleeper.sh ): #!/bin/bash stime=$[$RANDOM % 20] sleep $sti... As comparison, synchronous pipe code: #include <sys/wait.h> #include <stdio.h> #in... Asynchronous pipe code: #include <sys/wait.h> #include <stdio.h> #in...
Created by magnum on October 22, 2012 15:46:44    Last update: October 22, 2012 15:46:44
Example code from the Linux Programmer's Manual: #include <stdio.h> #include <stdlib.h> #incl...
Created by magnum on September 11, 2012 12:38:00    Last update: September 11, 2012 12:38:00
From bind man page: SYNOPSIS #include <sys/types.h> ... The bind call assigns an address and a port to a socket. That's it. There's no mention of client or server, so it can be used on a client socket or a server socket. But it's necessary for a server socket, otherwise clients do not know how to contact the server. For a client socket, the call is optional. When a client connects to a server or sends a message for the first time, a dynamic port is assigned to the client if there's no port assigned to it. If a port is bind beforehand, there's no dynamic assignment. Test code: int print_sock_info(int sockfd) { struct so... Prints: Local addr: 0.0.0.0, port: 0 Local addr: 0.0.0....
Created by magnum on September 11, 2012 12:10:03    Last update: September 11, 2012 12:10:46
Sample code for UDP client and server in C. The server simply echos back the client message. The client stays in "receive" loop to demonstrate the connection-less nature of the UDP protocol. Server code: #include <stdio.h> #include <stdlib.h> #incl... Client code: #include <stdio.h> #include <stdlib.h> #incl... Try it out: Start the server with: ./udpserver 8888 Send message to server: ./udpclient localhost 8888 "Hi, it's me! " Server console displays: Received from 127.0.0.1:41776: Hi, it's me! UDP is connectionless. Send a message from a second client to the first client: $ ./udpclient localhost 41776 "From client 2" First client console displays: $ ./udpclient localhost 8888 "Hi, it's me! " Re... It should be noted that the distinction between client and server is blurry. The only major...
Created by voodoo on August 07, 2012 12:02:33    Last update: August 07, 2012 12:02:33
This code snippet: #include <time.h> #include <stdio.h> int... generates warning at line 6: tt.c: In function ‘main’: tt.c:6:15: warning: i... The problem was that the prototype for strptime was not included from time.h . Define _GNU_SOURCE or _XOPEN_SOURCE to get rid of the warning: $ gcc -D_XOPEN_SOURCE -o tt tt.c
Created by magnum on February 22, 2012 16:04:38    Last update: February 22, 2012 16:04:38
A simple single threaded echo server: #include <stdio.h> #include <stdlib.h> #incl...
Created by magnum on October 09, 2011 19:53:26    Last update: October 09, 2011 19:53:50
#include <stdio.h> #include <stdlib.h> #incl... UNIXguide.net explains this option well: This socket option tells the kernel that even if this port is busy (in the TIME_WAIT state), go ahead and reuse it anyway. If it is busy, but with another state, you will still get an address already in use error. It is useful if your server has been shut down, and then restarted right away while sockets are still active on its port. You should be aware that if any unexpected data comes in, it may confuse your server, but while this is possible, it is not likely. It has been pointed out that "A socket is a 5 tuple (proto, local addr, local port, remote addr, remote port). SO_REUSEADDR just says that you...
Previous  1 2 Next