Sunday, April 15, 2012

non-blocking tcp socket





Enable/Disable Non-Blocking Mode
Once a socket has been created using the socket() call, it may be set to non-blocking as follows:
    #include "sys/ioctl.h"
    int on =1;
    int off =0;

    //Enable non-blocking
    ioctl (mySocket, FIONBIO, &(on));
    //Disable non-blocking
    ioctl (mySocket, FIONBIO, (char *) &(off));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
...
...
...
FD_ZERO(&read_mask);
FD_ZERO(&write_mask);
FD_SET(accept_socket, &read_mask);
FD_SET(accept_socket, &write_mask);
/* handle receiving the packet */
select_return = select(accept_socket, &read_mask, (fd_set *)0, (fd_set *)0, &tv);
if(select_return < 0) /* [timeout=0, -1= ERROR] is returned */
{
    printf("recv: select functions returned -1 error value\n");
}
else if(select_return == 0)
{
    printf("recv: select functions returned 0 timeout value\n");
}
else
{
    printf("recv: select functions returned a positive value\n");
     
    if(FD_ISSET(accept_socket, &read_mask))
    {
        /*  HAND SHAKE AS A START */
        rcv_len = recv(accept_socket, myDmsg, MSG_SIZE, 0);
                 
        if( rcv_len < 0)
        {
            strcpy(myDmsg, "Nothing received");
            isConnected = FALSE;
        }
        else if(rcv_len >= 0)
        {
            strcpy(myDmsg, "Received something2...");
        }
    }
}
/* handle sending the packet */
select_return = select(accept_socket+1, (fd_set *)0, &write_mask, (fd_set *)0, &tv);
if(select_return < 0) /* [timeout=0, -1= ERROR] is returned */
{
    printf("send: select functions returned -1 error value\n");
}
else if(select_return == 0)
{
    printf("send: select functions returned 0 timeout value\n");
}
else
{
    printf("send: select functions returned a positive value\n");
             
    if( send(accept_socket, myDmsg, strlen(myDmsg), 0) < 0)
    {
        /* an error occurred. Handle it appropriately (TODO) */
    }
}
...
...
...


tutorials: 0 1

Wednesday, April 11, 2012

int* p vs int *p declaration




There is a difference to the reader (human), but there's none to the compiler.

  • int* p
    • widely used by C++ programmers
    • int* p, q wrongly implies that both p and q are pointers (leading to a preference for declaring this on two lines, which also improves readability when there are assignments, and makes it easier to quickly cut/paste or comment specific lines/variables)
    • int* p visually separates the type from the identifier
    • *p unambiguously indicates a dereference (assuming you put spaces around your binaryoperator* ala 2 * 3)
    • in C++ ...&x is clearly taking an address while ...& x must be declaring a reference variable, and ... & ... is the bitwise operator
  • int *p
    • widely used by C programmers
    • int *p, q clearly reflects p being a pointer and q not being.
    • int *p visually confuses the type with the identifier
    • visually indistinguishable from a pointer dereference (for better or worse)


They are the same thing it comes down to preference in the end.
The bonus of the latter is that you can more sensibly define multiple pointers on one line ... ie
int *foo, *bar, *cow;
creates 3 pointers where as the following:
int* foo, bar, cow;
creates 1 pointer and 2 ints. Personally I prefer int* as it shows its a pointer to an integer and I define all my variables on seperate lines. Some will disagree with me, some will agree with me. Do whatever you prefer and, more than anything, BE CONSISTENT!

Tuesday, April 10, 2012

line discipline





How to use a custom line discipline

Kernel code, as stated, can register a new line discipline with the tty subsystem, and this is also available to modularized code. You could, therefore, write your own line discipline and register it. Each line discipline is identified by a number, and a symbolic name for it is available, as common with C programming. Assigned numbers are given a name byinclude/asm/termios.h.
The default tty line discipline is identified by a number of N_TTY, PPP uses N_PPP and so on. Unfortunately, no line discipline numbers have currently been reserved for ``local use'', so you can only experiment with the numbers that are not used on your system. Actually, no official driver currently used N_MOUSE, so this is a good bet for your custom line discipline.
In order to activate the N_MOUSE line discipline, the user space program must use this code:
    #include 

    int i = N_MOUSE;
    ioctl(fd, TIOCSETD, &i);



Labels