Hi,
I need to replicate with an Arduino Uno Rev3 and an AZDelivery W5100 Ethernet Shield the following code that work with VS2022 on Windows 10:
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
//#pragma comment(lib, "ws2_32.lib") // Link with ws2_32.lib
int main(int argc, char **argv)
{
WSADATA wsaData;
const char *hostname = "barco";
struct hostent *host_entry;
char ip_buffer[INET_ADDRSTRLEN];
int port = 43748;
int sock, send_result, rc;
struct sockaddr_in server_addr;
if (argc != 2)
{ printf("SendBarco usage: SendBarco \"string\"\n");
return 1;
}
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{ printf("SendBarco: WSAStartup failed with error: %d\n", WSAGetLastError());
return 1;
}
// Retrieve host information
host_entry = gethostbyname(hostname);
if (host_entry == NULL)
{ printf("SendBarco: gethostbyname failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Convert the address from binary to text
inet_ntop(AF_INET, host_entry->h_addr_list[0], ip_buffer, sizeof(ip_buffer));
//printf("IP address of %s: %s\n", hostname, ip_buffer);
// Create a socket
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET)
{ printf("Socket creation failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Set up the server address struct
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr.s_addr = inet_addr(ip_buffer);
// Connect to the server
if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) == SOCKET_ERROR)
{ printf("Connection failed with error: %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
return 1;
}
// Send the message
usleep(500000); // closesocket() too fast prevents working
send_result = send(sock, argv[1], (int)strlen(argv[1]), 0);
usleep(500000); // closesocket() too fast prevents working
if (send_result == SOCKET_ERROR)
{ printf("Send failed with error: %d\n", WSAGetLastError());
rc = 1;
}
else rc = 0;
// Receive response from server
/* char buffer[1024];
int recv_result;
recv_result = recv(sock, buffer, sizeof(buffer) - 1, 0);
if (recv_result == SOCKET_ERROR)
printf("Receive failed with error: %d\n", WSAGetLastError());
else
{ buffer[recv_result] = '\0'; // Null-terminate the received data
printf("Received: %s\n", buffer);
} */
// Cleanup
closesocket(sock);
WSACleanup();
return 0;
}
The purpose is to send an arbitrary string (it would not be main() with the Arduino but a subroutine) to a Barco projector.
That would be triggered by an IR signal, that part already works using the IRLib-master library.
Stuff could be hardcoded, no need like above to retrieve the IP address from the hostname, unless it's easy.
I have installed the following libraries:
WIZ_Ethernet_Library-IDE1.6.x-master
WIZ_Ethernet_Library-master
W5200-Ethernet-Shield-master
I don't know how to start and what to use for that kind of code.
Any suggestions?