Difference between revisions of "Example socket"

From wiki.emacinc.com
Jump to: navigation, search
(Created page with "{{todo|Review(12.31.13-14:56->JG+)|Jgreene|project=oe 4,oe 5,jg,md,Review}} This is a guide to the <code>socket</code> C example project included in the EMAC OE SDK. This app...")
 
Line 2: Line 2:
 
This is a guide to the <code>socket</code> C example project included in the EMAC OE SDK.
 
This is a guide to the <code>socket</code> C example project included in the EMAC OE SDK.
  
This application sets up and tests asynchronous IO on a serial port using '''poll()''' and '''fasync()'''.
+
This application sets up host and client sockets (assumedly on two different machines) and tests the connection.
  
 
The <code>socket</code> project builds one executable: <code>socket</code>.
 
The <code>socket</code> project builds one executable: <code>socket</code>.
Line 27: Line 27:
  
 
===Hardware Requirements===
 
===Hardware Requirements===
 +
 +
The hello C example project will run just fine on any system for which it can be compiled.
 +
 +
===Using <code>socket</code>===
 +
 +
Get 2 machines. We're going to refer to our machines as ''MACHINE0'' and ''MACHINE1''.<br \>
 +
Compile and upload <code>socket</code> to each of them.<br \>
 +
Connect them both to the same ethernet network.<br \>
 +
 +
On MACHINE0 run the <code>socket</code> program like this:
 +
som9g45:/tmp# ./socket -s 9999
 +
Note that we specified '''-s''', thus configuring MACHINE0 as a server; and ''9999'' is just an arbitrarily chosen (and assumed to be free) port address on the host machine.
 +
 +
Now MACHINE0 is the server and it's waiting for a connection...
 +
 +
On MACHINE1 run the <code>socket</code> program like this:
 +
som9g20:/tmp# ./socket -c 10.0.2.204 9999
 +
Note that we specified '''-C''', thus configuring MACHINE1 as a client. 10.0.2.204 is the host machine's address. ''9999'' is that port address on the host machine.
 +
 +
...MACHINE1 connects to MACHINE0. MACHINE0 sends a message to MACHINE1 "Message from the socket server". MACHINE1 receives the message and sends it right back again to check the accuracy of the transmission, and we see that the messages match so the transmission was successful.
 +
 +
'''On MACHINE0 you will see something like this:'''
 +
<syntaxhighlight lang="console">
 +
som9g45:/tmp# ./socket -s 9999
 +
 +
Starting server
 +
Making socket
 +
Binding to port 9999opened socket as fd (3) on port (9999) for stream i/o
 +
Server
 +
                        sin_family        = 2
 +
                        sin_addr.s_addr  = 0
 +
                        sin_port          = 9999
 +
 +
Making a listen queue of 5 elements
 +
Waiting for a connection
 +
 +
Got a connection
 +
Sending "Message from the socket server" to client
 +
The messages match
 +
Closing the socket
 +
Waiting for a connection
 +
</syntaxhighlight">
 +
 +
'''On MACHINE1 you will see something like this:'''
 +
<syntaxhighlight lang="console">
 +
root@som9g20:/tmp# ./socket -c 10.0.2.204 9999
 +
 +
Making a socket
 +
Connecting to 10.0.2.204 on port 9999
 +
Received "Message from the socket server" from server
 +
 +
Writing "Message from the socket server" to server
 +
Closing socket
 +
</syntaxhighlight">

Revision as of 14:56, 2 January 2014

TODO: {{#todo:Review(12.31.13-14:56->JG+)|Jgreene|oe 4,oe 5,jg,md,Review}}

This is a guide to the socket C example project included in the EMAC OE SDK.

This application sets up host and client sockets (assumedly on two different machines) and tests the connection.

The socket project builds one executable: socket.

Opening, Building and Uploading the Project Files

1. Open the C/C++ editing perspective.

stub

2. Open the project files.

stub

3. Build the project.

stub

4. Upload the executables to the target system.

stub

Usage and Behavior

Hardware Requirements

The hello C example project will run just fine on any system for which it can be compiled.

Using socket

Get 2 machines. We're going to refer to our machines as MACHINE0 and MACHINE1.
Compile and upload socket to each of them.
Connect them both to the same ethernet network.

On MACHINE0 run the socket program like this:

som9g45:/tmp# ./socket -s 9999

Note that we specified -s, thus configuring MACHINE0 as a server; and 9999 is just an arbitrarily chosen (and assumed to be free) port address on the host machine.

Now MACHINE0 is the server and it's waiting for a connection...

On MACHINE1 run the socket program like this:

som9g20:/tmp# ./socket -c 10.0.2.204 9999

Note that we specified -C, thus configuring MACHINE1 as a client. 10.0.2.204 is the host machine's address. 9999 is that port address on the host machine.

...MACHINE1 connects to MACHINE0. MACHINE0 sends a message to MACHINE1 "Message from the socket server". MACHINE1 receives the message and sends it right back again to check the accuracy of the transmission, and we see that the messages match so the transmission was successful.

On MACHINE0 you will see something like this: <syntaxhighlight lang="console"> som9g45:/tmp# ./socket -s 9999

Starting server Making socket Binding to port 9999opened socket as fd (3) on port (9999) for stream i/o Server

                       sin_family        = 2
                       sin_addr.s_addr   = 0
                       sin_port          = 9999

Making a listen queue of 5 elements Waiting for a connection

Got a connection Sending "Message from the socket server" to client The messages match Closing the socket Waiting for a connection </syntaxhighlight">

On MACHINE1 you will see something like this: <syntaxhighlight lang="console"> root@som9g20:/tmp# ./socket -c 10.0.2.204 9999

Making a socket Connecting to 10.0.2.204 on port 9999 Received "Message from the socket server" from server

Writing "Message from the socket server" to server Closing socket </syntaxhighlight">