Example socket

From wiki.emacinc.com
Revision as of 15:10, 2 January 2014 by Jgreene (talk | contribs) (Using socket)
Jump to: navigation, search
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.


2. Open the project files.


3. Build the project.


4. Upload the executables to the target system.


Usage and Behavior

Hardware Requirements

The socket 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 the socket executable 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. 9999 is an arbitrarily chosen (and assumed to be free) port address on the host machine.

...MACHINE0 has been configured as a server and now it's waiting for a connection...

On MACHINE1 run the socket program like this:

som9g20:/tmp# ./socket -c 9999

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

On MACHINE0 you will see something like this:

som9g45:/tmp# ./socket -s 9999

Starting server
Making socket
Binding to port 9999opened socket as fd (3) on port (9999) for stream i/o
                        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

On MACHINE1 you will see something like this:

root@som9g20:/tmp# ./socket -c 9999

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

Writing "Message from the socket server" to server
Closing socket

This is what happened: MACHINE0 (the server) was set as a server and waited for a connection. MACHINE1 (the client) connected to MACHINE0. MACHINE0 sent a message to MACHINE1: "Message from the socket server". MACHINE1 received the message and sent it right back again to check the accuracy of the transmission. The messages match so the transmission was successful.