Difference between revisions of "Example socket"

From wiki.emacinc.com
Jump to: navigation, search
(Using socket)
Line 31: Line 31:
  
 
===Using <code>socket</code>===
 
===Using <code>socket</code>===
 +
 +
We create a ''server'' and a ''client''. Upon activation the server waits for a connection. Upon activation the client connects with the server. When the server detects the client's connection it sends a message to the client. Then the client sends that message right back to the server. The server compares the message it sent to the client with the message it just got from the client. If they match then we have a successful transmission.
 +
 +
===Usage Example. Two machines===
  
 
Get 2 machines. We're going to refer to our machines as ''MACHINE0'' and ''MACHINE1''.<br \>
 
Get 2 machines. We're going to refer to our machines as ''MACHINE0'' and ''MACHINE1''.<br \>

Revision as of 12:51, 3 January 2014

TODO: {{#todo:Review(01.02.14-15:25->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 demonstrates how to set up sockets for host and client nodes on an ethernet network.

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 socket C example project will run just fine on any system for which it can be compiled.

Using socket

We create a server and a client. Upon activation the server waits for a connection. Upon activation the client connects with the server. When the server detects the client's connection it sends a message to the client. Then the client sends that message right back to the server. The server compares the message it sent to the client with the message it just got from the client. If they match then we have a successful transmission.

Usage Example. Two machines

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.

So now MACHINE0 has been configured as a 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 (this will need to be determined. Use minicom or something similar). 9999 is the 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
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

On MACHINE1 you will see something like this:

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

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

Summary

The socket C example project demonstrates how to set up host and client node sockets on an ethernet network.