Difference between revisions of "Example socket"

From wiki.emacinc.com
Jump to: navigation, search
(Using socket)
 
(36 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{todo|Review(12.31.13-14:56->JG+)|Jgreene|project=oe 4,oe 5,jg,md,Review}}
+
{{todo|SEOKWREV (01.03.14-12:37->JG+);(01.03.14-14:00->MD+);(01.03.14-15:35->KY+);(04.07.14-10:10->BS+);(04.10.14-11:30->BS+)|Jgreene|project=oe 4,oe 5,jg,md,SEOKWREV,ky,bs}}
 +
 
 +
{{#seo:
 +
|title=Example socket
 +
|titlemode=append
 +
|keywords=Example socket,Socket Server,Ethernet Network,Socket Client
 +
|description=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 is a guide to the <code>socket</code> 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.
+
This application demonstrates how to set up sockets for host and client nodes on an ethernet network.
  
 
The <code>socket</code> project builds one executable: <code>socket</code>.
 
The <code>socket</code> project builds one executable: <code>socket</code>.
 
 
== Opening, Building and Uploading the Project Files ==
 
== Opening, Building and Uploading the Project Files ==
  
<big>1. Open the C/C++ editing perspective.</big>
+
For information on opening the project from within Eclipse, please see [[Importing the EMAC OE SDK Projects with Eclipse]]. Then, follow [[Using the EMAC OE SDK Projects with Eclipse]] for information on how to build, upload and execute the example.
  
stub
+
Alternatively, the <code>Makefile</code> can be used with the <code>make</code> command from the commandline to build and upload the example.  For information on this method, please see [[Using EMAC OE SDK Example Projects]].
  
<big>2. Open the project files.</big>
+
====EMAC SDK 5.X====
  
stub
+
For information on opening the project from within QtCreator, please see [[Getting_Started_With_Qt_Creator#Adding_Source_Files | QtCreator: Adding Source Files]].  Then, follow [[Getting Started With Qt Creator]] for information on how to build, upload and execute the example.
  
<big>3. Build the project.</big>
+
Alternatively, the <code>CMakefile.txt</code> can be used with the <code>cmake</code> command from the commandline to build and upload the example.  For information on this method, please see [[Getting_Started_with_the_EMAC_OE_SDK#Target_Machine_Compiling | Getting Started with the EMAC OE SDK]].
 
 
stub
 
 
 
<big>4. Upload the executables to the target system.</big>
 
 
 
stub
 
  
 +
Code can be found at http://git.emacinc.com/OE/example-projects
 
==Usage and Behavior==
 
==Usage and Behavior==
  
Line 32: Line 33:
 
===Using <code>socket</code>===
 
===Using <code>socket</code>===
  
Get 2 machines. We're going to refer to our machines as ''MACHINE0'' and ''MACHINE1''.<br \>
+
./socket -sc [ADDRESS] PORT
 +
 
 +
;-s:Create a ''server'', specify the PORT to listen to. 
 +
;-c:Create a ''client'', specify the server's ADDRESS and PORT.
 +
 
 +
For our demonstration 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 performed a successful transmission.
 +
 
 +
===Usage Example. One machine===
 +
 
 +
We're going to create a ''server'' and ''client'' on the same machine. The machine will talk to itself.
 +
 
 +
Create the server
 +
 
 +
'''Note''' We're using port ''9999''. It's arbitrarily chosen and assumed to be free.
 +
 
 +
<syntaxhighlight lang="text">
 +
root@som9g20:/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
 +
</syntaxhighlight>
 +
 
 +
...and it's waiting for a connection. Note that you can't do anything else in this console because it's occupied with the <code>socket</code> program. So open another console and, if necessary, log-on.
 +
 
 +
Create the client.
 +
 
 +
'''Note''' We're using port ''9999'', the same port for which the server is configured; and the localhost address: 127.0.0.1
 +
 
 +
<syntaxhighlight lang="text">
 +
root@som9g20:/tmp# ./socket -c 127.0.0.1 9999
 +
 
 +
Making a socket
 +
Connecting to 127.0.0.1 on port 9999
 +
Received "Message from the socket server" from server
 +
 
 +
Writing "Message from the socket server" to server
 +
Closing socket
 +
</syntaxhighlight>
 +
 
 +
This is what happened: The server started up and waited for a connection. Then we started the client and it immediately connected to the server. The server noticed that connection and sent a message to the client: "Message from the socket server". The client received the message and sent it right back to the server. The server compared the two messages to check the accuracy of the transmission. The messages matched so the transmission was successful.
 +
 
 +
===Usage Example. Two machines===
 +
 
 +
Get 2 machines.  In this example, a SoM9g20 and a SoM 9g45 are used. These machines will be referred to as ''MACHINE0'' and ''MACHINE1''.<br \>
 
Compile and upload the <code>socket</code> executable to each of them.<br \>
 
Compile and upload the <code>socket</code> executable to each of them.<br \>
 
Connect them both to the same ethernet network.<br \>
 
Connect them both to the same ethernet network.<br \>
  
On MACHINE0 run the <code>socket</code> program like this:
+
'''On MACHINE0 run the <code>socket</code> program.'''
 
  som9g45:/tmp# ./socket -s 9999
 
  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.
+
Note that '''-s''' was specified, thus configuring MACHINE0 as a server. ''9999'' is 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...
+
MACHINE0 has now been configured as a server and is waiting for a connection.
  
On MACHINE1 run the <code>socket</code> program like this:
+
'''On MACHINE1, run the <code>socket</code> program.'''
 
  som9g20:/tmp# ./socket -c 10.0.2.204 9999
 
  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.
+
Note that the '''-c''' argument was specified, 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 a similar terminal program). ''9999'' is the 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:'''
+
'''On MACHINE0, you will see something like this:'''
<syntaxhighlight lang="console">
+
<syntaxhighlight lang="text">
 
som9g45:/tmp# ./socket -s 9999
 
som9g45:/tmp# ./socket -s 9999
  
Line 68: Line 118:
 
Closing the socket
 
Closing the socket
 
Waiting for a connection
 
Waiting for a connection
</syntaxhighlight">
+
</syntaxhighlight>
  
'''On MACHINE1 you will see something like this:'''
+
'''On MACHINE1, you will see something like this:'''
<syntaxhighlight lang="console">
+
<syntaxhighlight lang="text">
 
root@som9g20:/tmp# ./socket -c 10.0.2.204 9999
 
root@som9g20:/tmp# ./socket -c 10.0.2.204 9999
  
Line 80: Line 130:
 
Writing "Message from the socket server" to server
 
Writing "Message from the socket server" to server
 
Closing socket
 
Closing socket
</syntaxhighlight">
+
</syntaxhighlight>
 +
 
 +
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 <code>socket</code> C example project demonstrates how to set up host and client node sockets on an ethernet network.

Latest revision as of 13:22, 30 November 2022

TODO: {{#todo:SEOKWREV (01.03.14-12:37->JG+);(01.03.14-14:00->MD+);(01.03.14-15:35->KY+);(04.07.14-10:10->BS+);(04.10.14-11:30->BS+)|Jgreene|oe 4,oe 5,jg,md,SEOKWREV,ky,bs}}

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

For information on opening the project from within Eclipse, please see Importing the EMAC OE SDK Projects with Eclipse. Then, follow Using the EMAC OE SDK Projects with Eclipse for information on how to build, upload and execute the example.

Alternatively, the Makefile can be used with the make command from the commandline to build and upload the example. For information on this method, please see Using EMAC OE SDK Example Projects.

EMAC SDK 5.X

For information on opening the project from within QtCreator, please see QtCreator: Adding Source Files. Then, follow Getting Started With Qt Creator for information on how to build, upload and execute the example.

Alternatively, the CMakefile.txt can be used with the cmake command from the commandline to build and upload the example. For information on this method, please see Getting Started with the EMAC OE SDK.

Code can be found at http://git.emacinc.com/OE/example-projects

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

./socket -sc [ADDRESS] PORT
-s
Create a server, specify the PORT to listen to.
-c
Create a client, specify the server's ADDRESS and PORT.

For our demonstration 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 performed a successful transmission.

Usage Example. One machine

We're going to create a server and client on the same machine. The machine will talk to itself.

Create the server

Note We're using port 9999. It's arbitrarily chosen and assumed to be free.

root@som9g20:/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

...and it's waiting for a connection. Note that you can't do anything else in this console because it's occupied with the socket program. So open another console and, if necessary, log-on.

Create the client.

Note We're using port 9999, the same port for which the server is configured; and the localhost address: 127.0.0.1

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

Making a socket
Connecting to 127.0.0.1 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: The server started up and waited for a connection. Then we started the client and it immediately connected to the server. The server noticed that connection and sent a message to the client: "Message from the socket server". The client received the message and sent it right back to the server. The server compared the two messages to check the accuracy of the transmission. The messages matched so the transmission was successful.

Usage Example. Two machines

Get 2 machines. In this example, a SoM9g20 and a SoM 9g45 are used. These machines will be referred to 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.

som9g45:/tmp# ./socket -s 9999

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

MACHINE0 has now been configured as a server and is waiting for a connection.

On MACHINE1, run the socket program.

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

Note that the -c argument was specified, 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 a similar terminal program). 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.