Showing posts with label IPC - Socket : Advance Concept Sample Codes. Show all posts
Showing posts with label IPC - Socket : Advance Concept Sample Codes. Show all posts

Saturday, February 18, 2012

struct iovec example


writev example c


Monday, September 19, 2011

stream tcp server c


Wednesday, September 14, 2011

socket bind listen accept example code


Receiving with the SIGURG Signal example code


sending out of band data example code


Saturday, September 10, 2011

udp broadcast client example in c




The general steps used by the client program are these:
  1. The socket is created (lines 68 to 70).
  2. The broadcast address is formed (lines 75 to 83).
  3. The SO_REUSEADDR option is enabled (lines 89 to 96).
  4. Bind the broadcast address to the current socket (lines 101 to 106).
  5. Begin a broadcast receiving loop (line 108).
  6. Receive a broadcast (lines 112 to 120).
  7. Write the broadcast information to the standard output (lines 122 to 125).
  8. Repeat step 5. 
Pay special attention to step 4. To receive the broadcast, there has to be a client program that has this address bound to the socket. This identifies the client program as the intended recipient of the messages. 

There is a problem with this approach, however. If one client program binds this address, then no others on the same host will be able to bind that address. This would defeat the purpose of broadcasting. Enabling SO_REUSEADDR allows multiple client programs to receive from the same broadcast address, on the same host. 

To compile the demonstration client program, you can use the following command: 

Take this mkaddr.c file for compiling the code

Output

[root@rhel54x64 socket]# gcc -c -D_GNU_SOURCE -Wall -Wreturn-type -g udp-broadcast-client.c
[root@rhel54x64 socket]# gcc -c -D_GNU_SOURCE -Wall -Wreturn-type -g mkaddr.c
[root@rhel54x64 socket]# gcc -g udp-broadcast-client.o mkaddr.o -o udp-broadcast-client

Here is the complete udp broadcast client-server application

Friday, September 9, 2011

udp broadcast server c


 

Here is the step by step code flow of the abover udp server: The server shown in Listing above is functionally divided into the following sections: 
  1. The table of stock market indexes is declared in lines 27 to 39. Four indexes are defined with starting values and a crude form of volatility value, which is used for the simulation.  
  2. The function initialize() in lines 44 to 58 is called once to initialize for the simulation.  
  3. The function gen_quote() is called to randomly change the simulated value of a randomly selected stock market index (lines 63 to 77).  
  4. The main() program, which forms the basis of the server, is contained within lines 92 to 222. 
The basic operation of this stock market server is as follows:
  1. Default addresses are declared in lines 106 and 107. These are used when no command-line arguments are supplied. 
  2. If two command-line arguments are supplied, then the server address takes the address from the second argument (line 114).
  3. If one or more command-line arguments are supplied, then the broadcast argument is taken from argument one (line 118).
  4. The server address is formed (lines 123 to 132).
  5. The broadcast address is formed (lines 137 to 146).
  6. A socket is created (lines 151 to 153). 
  7. The SO_BROADCAST option is enabled on the socket (lines 158 to 165).
  8. The server address is bound (lines 172 to 177). 
  9. The stock market indices are initialized (line 182).
  10. The server loop begins in line 184. 
Compiling the above server:

Take this version of mkaddr.c

[root@rhel54x64 socket]# gcc -c -D_GNU_SOURCE -Wall -Wreturn-type -g udp-broadcast-server.c
[root@rhel54x64 socket]# gcc -c -D_GNU_SOURCE -Wall -Wreturn-type -g mkaddr.c 
[root@rhel54x64 socket]# gcc -g udp-broadcast-server.o mkaddr.o -o udp-broadcast-server

Here is the complete udp broadcast client-server application

mkaddr c code


There is another version of mkaddr.c subroutine which is using inet_addr. The problem with inet_addr(3) is that it returns the value INADDR_NONE when an address is invalid. When the IP address 255.255.255.255 is converted to a 32-bit value, its return value is identical to the constant INADDR_NONE. Consequently, it becomes impossible to distinguish between a bad input IP address, and the general broadcast address. Use of the inet_aton(3) function avoids this ambiguity.
Here is the complete udp broadcast client-server application

Tuesday, September 6, 2011

getsockopt so_type example c code

Monday, September 5, 2011

setsockopt example c code


getsockopt example c code


See Also

Other Socket Options

Wednesday, August 31, 2011

RPN main server code

Previous to this I have presented the RPN calculator code Now here is the server code to work with that. Below I am also presenting how to work with the RPN Server also. First let us present the server code, if you feel any difficulty in understanding the code post me I will give you the explanations step by step


Trying out the RPN Server

Here are all the files

rpneng.c
mkaddr.c

To compile all the related source modules for the RPN server, you can perform the following make command:

Output
gcc -c -D_GNU_SOURCE -Wall -Wreturn-type rpnsrv.c
gcc -c -D_GNU_SOURCE -Wall -Wreturn-type rpneng.c
gcc -c -D_GNU_SOURCE -Wall -Wreturn-type mkaddr.c
gcc rpnsrv.o rpneng.o mkaddr.o -o rpnsrv -lgmp

After the executable rpnsrv for the server has been created, you can start the server as follows:

Output
$ ./rpnsrv &
[1] 13321
$

In the output shown, the server was started with a process ID of 13321, and run in the background. To keep things simple at this point, you'll just use the telnet command to try out the server. The
next chapter will fully outline this server's functions. For now, just try some simple tests.

CAUTION
The server presented is not a production-grade server. Some forms of incorrect input can provoke the server to abort. The RPN calculator computes based upon numbers that are pushed onto the stack. To perform the add operation, for example, requires at least two numbers to exist on the stack. To push a number onto the stack, you will enter a line as follows:

#:970976453

After you press Enter, the server will respond with something like this:
0:

This tells you that the number has been stacked at the bottom of the stack (entry number zero). To stack another number, simply do the same, as follows:

#:2636364
The server will respond with
1:

This indicates that the number 2636364 was stacked at position 1, although the original number 970976453 still sits at the bottom of the stack at position 0. You can list the current contents of
the stack by entering the following:

dump

The following example shows what the session and its output might look like this:
Output

$ telnet localhost 9090
Trying 127.0.0.1 . . .
Connected to localhost.
Escape character is '^]'.
#:970976453
0:
#:2636364
1:
dump
1:2636364
0:970976453
E:end of stack dump

To perform a binary operation, you simply enter the name of the operation or its symbol. For example, to add these numbers, you would just enter the + character and press return. The session
repeated without entering the dump command would appear as follows if the + operation was performed, and then followed by the = operation:

Output
$ telnet localhost 9090
Trying 127.0.0.1 . . .
Connected to localhost.
Escape character is '^]'.
#:970976453
0:
#:2636364
1:
+
0:
=
0:973612817
^]
telnet> c
Connection closed.
$

The + operation caused the two stacked numbers to be added together, and the result replaced the two original values. The = operator here pops the result off the stack and displays it for you. To exit the server, type CTRL+] and you will be prompted with the prompt:

telnet>

From there, enter a c to indicate that you want the session closed, and press Enter. To terminate the server, just use the kill command.
Take a few minutes now to have some fun with the new RPN calculating server program. Restart the server, and see whether you can figure out how to compute the equation (3 + 2) * (2 + 4) using the
calculating server just presented.

The RPN Calculator Engine C Code

The RPN calculator engine code will be presented next. It is not expected that you will understand all of the GMP function calls because they have not been presented. However, if you have Red Hat Enterprise Linux installed, you can find out more about the GMP calls by performing the following command:

$ info GMP

This will bring up the info viewer with documentation about the GMP function library. Listing 10.5 lists the RPN calculator code. Note especially the standard I/O calls in the functions rpn_dump() and rpn_process().


How the server works from the client side, will be examined after the remainder of the server code is presented. Listing 10.6 shows the remainder of the server source code. This represents the main program segment of the server.

mkaddr C Subroutine for socket