Skip to main content

How to send and receive data over Ethernet

Ethernet is a physical and data link layer technology for local area networks (LANs). Ethernet was invented by engineer Robert Metcalfe.

When first widely deployed in the 1980s, Ethernet supported a maximum theoretical data rate of 10 megabits per second (Mbps). Later, so-called "Fast Ethernet" standards increased this maximum data rate to 100 Mbps. Gigabit Ethernet technology further extends peak performance up to 1000 Mbps, and 10 Gigabit Ethernet technology also exists.

Higher level network protocols like Internet Protocol (IP) use Ethernet as their transmission medium. Data travels over Ethernet inside protocol units called frames. The run length of individual Ethernet cable is limited to roughly 100 meters, but Ethernet networks can be easily extended to link entire schools or office buildings using network bridge devices.

Program for Client

Steps to run the demo

  • Connect Ethernet cable to X17 on Colibri evaluation Board V3.1A.
  • Create a new VC++ project by following this tutorial up to step 9 and copy the code as mention below.
  • Change IP address and port number in code according to your host (Server) IP address and port number.
  • Build and deploy solution.
  • Connect host (PC) on same network and run TCP client/server simulator like Hercules, in server mode.
  • Navigate to Program Files folder on Toradex module and run executable file.

Source code


#include <windows.h>
#include <stdio.h>
#include <winsock2.h>

#pragma comment(lib, "Ws2.lib")

/// Set IP address and port number according to server IP address and port number
#define SERVER_IP_ADDRESS "10.18.1.105"
#define PORT_NUMBER 23
#define BUFFER_SIZE 255

int wmain()
{
DWORD choice = 0;
BOOL loopMenu = TRUE;
WSADATA ws; ///< Structure to contain information about the Windows Socket implementation
SOCKET commSocket;
int retVal = 0;
char sendBuffer[BUFFER_SIZE] = {0};
char recvBuffer[BUFFER_SIZE] = {0};
struct sockaddr_in serverinfo;

retVal = WSAStartup(0x0101, &ws); ///< Initialize ws2.dll (library used for socket programming)
if (retVal != 0) ///< If WSAStartup failed to initialize
{
printf("WSAStartup failed with error: %d\n", WSAGetLastError());
exit(1);
}

commSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); ///< Socket creation
if (commSocket == INVALID_SOCKET)
{
printf("\nSocket creation failed with error code : %d", WSAGetLastError());
WSACleanup();
exit(1);
}

/// Socket binding
serverinfo.sin_family = AF_INET; ///< TCP/UDP socket
serverinfo.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS); ///< IP Address of Server
serverinfo.sin_port = htons(PORT_NUMBER); ///< Port number used for communication

retVal = connect(commSocket, (LPSOCKADDR)&serverinfo, sizeof(struct sockaddr)); ///< Connect to Server
if (retVal == SOCKET_ERROR)
{
printf("\nCould not connect to Server with error code : %d", WSAGetLastError());
WSACleanup();
return FALSE;
}

printf("Demo program to send and receive data over LAN, Toradex Module as client\n");
while (loopMenu == TRUE)
{
printf("\n1. Send\n2. Receive\n3. Exit\n");
scanf_s("%d", &choice);
switch(choice)
{
case 1:
printf("\nSend = ");
scanf_s("%s", sendBuffer);
retVal = send(commSocket, sendBuffer, strlen(sendBuffer), 0);
if (retVal == SOCKET_ERROR)
{
printf("\nCould not send message to Server with error code : %d", WSAGetLastError());
}
break;
case 2:
printf("\nEntering Listen Mode \n");
retVal = recv(commSocket, recvBuffer, BUFFER_SIZE, 0); ///< Receive from server
if (retVal == SOCKET_ERROR)
{
printf("\nCould not receive from Server with error code : %d", WSAGetLastError());
}
else
{
printf("\nMessage received from server : %s", recvBuffer);
memset(&recvBuffer[0], 0, sizeof(recvBuffer));
}
break;
case 3:
loopMenu = FALSE;
break;
default:
printf("\nInvalid Choice");
break;
}
}
closesocket(commSocket);
WSACleanup(); ///< Releasing ws2.dll
return TRUE;
}

Download project for client

You can download demo source code from here.

Program for Server

Steps to run the demo

  • Connect Ethernet cable to X17 on Colibri Evaluation Board V3.1A.
  • Create a new VC++ project by following this tutorial up to step 9 and copy the code as mention below.
  • Change IP address and Port number in code according to your Toradex module.
  • Build and deploy solution.
  • Connect Host (PC) on same network and run TCP client/server simulator like Hercules.
  • Navigate to Program Files folder on Toradex module and run executable file.
  • On host computer run TCP client simulator (e.g Hercules).

Make sure to run the application on module first before connecting to TCP server from client (e.g Hercules on host computer).

Source code


#include <windows.h>
#include <stdio.h>
#include <winsock2.h>

#pragma comment(lib, "Ws2.lib")

/// Set device IP address and port number
#define DEVICE_IP_ADDRESS "10.18.0.133"
#define PORT_NUMBER 23
#define BUFFER_SIZE 255

int wmain()
{
WSADATA ws; ///< Structure to contain information about the Windows Socket implementation
DWORD retVal = 0;
DWORD choice = 0;
BOOL loopCounter = TRUE;
SOCKET listeningSocket;
SOCKET commsocket;
CHAR sendbuffer[BUFFER_SIZE] = {0};
CHAR recvbuffer[BUFFER_SIZE] = {0};
struct sockaddr_in serverinfo;

printf("Demo Program to send and receive data over LAN, Toradex Module as server\n");
retVal = WSAStartup(0x0101, &ws); ///< Initiating use of ws2.dll
if (retVal != 0) ///< If WSAStartup failed to initialize
{
printf("WSAStartup failed with error code: %d\n", retVal);
WSACleanup();
exit(1);
}

listeningSocket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); ///< Socket creation
if (listeningSocket == INVALID_SOCKET)
{
printf("\nSocket creation failed with error code : %d", WSAGetLastError());
WSACleanup();
exit(1);
}

/// Socket binding
serverinfo.sin_family = AF_INET; ///< TCP/UDP socket
serverinfo.sin_addr.s_addr = inet_addr(DEVICE_IP_ADDRESS); ///< IP Address of Toradex module
serverinfo.sin_port = htons(PORT_NUMBER); ///< Port number: used for communication

retVal = bind(listeningSocket, (SOCKADDR *)&serverinfo, sizeof(struct sockaddr));
if (retVal == SOCKET_ERROR)
{
printf("\nSocket binding failed with error code : %d", WSAGetLastError());
WSACleanup();
exit(1);
}

retVal = listen(listeningSocket, 1); ///< Listen for 1 socket only
if (retVal == SOCKET_ERROR)
{
printf("\nSocket listening failed with error code : %d", WSAGetLastError());
WSACleanup();
exit(1);
}

printf("\nWaiting for client to respond....");
commsocket = accept(listeningSocket, NULL, NULL); ///< Waiting for request from client
if (commsocket == INVALID_SOCKET)
{
printf("\nConnection failed with error code : %d", WSAGetLastError());
WSACleanup();
exit(1);
}

while (loopCounter == TRUE)
{
printf("\n1. Send\n2. Receive\n3. Exit\n");
scanf_s("%d", &choice);
switch(choice)
{
case 1:
printf("Send Message = ");
scanf_s("%s", sendbuffer);
retVal = send(commsocket, sendbuffer, strlen(sendbuffer), 0); ///< Sending to client
if (retVal == SOCKET_ERROR)
{
printf("\nFailed to receive from client with error code : %d", WSAGetLastError());
}
break;
case 2:
printf("\nListening socket...");
retVal = recv(commsocket, recvbuffer, BUFFER_SIZE, 0); ///< Receiving from client
recvbuffer[retVal] = 0;
if (retVal == SOCKET_ERROR)
{
printf("\nFailed to receive from client with error code : %d", WSAGetLastError());
}
else
{
printf("\nMessage from Client : %s", recvbuffer);
}
break;
case 3:
loopCounter = FALSE;
break;
default:
printf("Invalid choice \n");
break;
}
}
closesocket(commsocket); ///< Closing communication socket
closesocket(listeningSocket); ///< Closing listening socket
WSACleanup(); ///< Releasing ws2.dll
return TRUE;
}

Download project for server

You can download demo source code from here.

Multi-Thread demo (Client)

Steps to run

  • Connect Ethernet cable to X17 on Colibri Evaluation Board V3.1A.
  • Create a new VC++ project by following this tutorial up to step 9 and copy the code as mention below.
  • Change IP address and port number in code according to your host (Server) IP address and port number.
  • Build and deploy solution.
  • Connect host (PC) on same network and run TCP client/server simulator like Hercules, in server mode and open port.
  • Navigate to Program Files folder on Toradex module and run executable file.
  • Once connected acknowledgement message will appear on console of client.
  • Write anything on console and press ENTER to send data.
  • Send data from server in parallel.
  • Press CTRL+Z and ENTER to exit from program.

Source code


#include <windows.h>
#include <stdio.h>
#include <winsock2.h>

#pragma comment(lib, "Ws2.lib")

/// Set IP address and port number according to server IP address and port number
#define SERVER_IP_ADDRESS "10.18.0.100"
#define PORT_NUMBER 23
#define BUFFER_SIZE 255

#define ASCII_CTRLZ "26" ///< ASCII of CTRT+Z or EOF to exit from code.

HANDLE hCom; ///< Handle for thread
SOCKET commSocket; ///< Structure to contain SOCKET configuration
HANDLE myEvent; ///< Handle to Event declared

DWORD retVal;
CHAR sendBuffer[BUFFER_SIZE] = {0};


/// Thread for Sending data
DWORD SendThreadProc(LPVOID hCom)
{
while(1)
{
WaitForSingleObject(myEvent, INFINITE);
retVal = send(commSocket, sendBuffer, strlen(sendBuffer), 0);
if (retVal == SOCKET_ERROR)
{
printf("\nCould not send message to Server with error code : %d", WSAGetLastError());
CloseHandle(myEvent);
return 0;
}
sendBuffer[0] = '\0';
}
}

/// Thread for receiving data
DWORD ReceiveThreadProc(LPVOID hCom)
{
char recvBuffer[BUFFER_SIZE] = {0};

while(1)
{
retVal = recv(commSocket, recvBuffer, BUFFER_SIZE, 0); ///< Receive from server
if (retVal == SOCKET_ERROR)
{
printf("\nCould not receive from Server with error code : %d", WSAGetLastError());
return 0;
//break;
}
else if(retVal == 0)
{
break;
}
else
{
printf("Data Received: %s\n", recvBuffer);
//break;
}
}
}

int wmain()
{
DWORD choice = 0;
DWORD oldMode = 0;
BOOL loopMenu = TRUE;
WSADATA ws; ///< Structure to contain information about the Windows Socket implementation
struct sockaddr_in serverinfo;

myEvent = CreateEventW(NULL, FALSE, FALSE, NULL); ///< Creating an Event to enter data on console

retVal = WSAStartup(0x0101, &ws); ///< Initialize ws2.dll (library used for socket programming)
if (retVal != 0) ///< If WSAStartup failed to initialize
{
printf("WSAStartup failed with error: %d\n", WSAGetLastError());
exit(1);
}

commSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); ///< Socket creation
if (commSocket == INVALID_SOCKET)
{
printf("\nSocket creation failed with error code : %d", WSAGetLastError());
WSACleanup();
exit(1);
}

/// Socket binding
serverinfo.sin_family = AF_INET; ///< TCP/UDP socket
serverinfo.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS); ///< IP Address of Server
serverinfo.sin_port = htons(PORT_NUMBER); ///< Port number used for communication

retVal = connect(commSocket, (LPSOCKADDR)&serverinfo, sizeof(struct sockaddr)); ///< Connect to Server
if (retVal == SOCKET_ERROR)
{
printf("\nCould not connect to Server with error code : %d", WSAGetLastError());
WSACleanup();
return FALSE;
}
printf("Demo program to send and receive data over LAN using Multi Threading,\nToradex Module as client"
"\n*****PRESS CTRL + Z and ENTER to EXIT*****\n");

CreateThread(NULL, 0, SendThreadProc, hCom, 0, NULL); ///< Thread declaration to Send Data
CreateThread(NULL, 0, ReceiveThreadProc, hCom, 0, NULL); ///< Thread declaration to Receive Data

while(!feof(stdin))
{
scanf_s("%s", sendBuffer);
if(sendBuffer == ASCII_CTRLZ)
{
break;
}
else
{
if(!SetEvent(myEvent)) ///< Setting Event Flag
{
printf("Event not Set: %d", GetLastError());
break;
}
}
}
closesocket(commSocket);
WSACleanup(); ///< Releasing ws2.dll
return TRUE;
}

Download project

You can download demo source code from here.



Send Feedback!