gpt4 book ai didi

c++ - 使用 UDP LabVIEW 与 UDP c++ 套接字通信

转载 作者:太空宇宙 更新时间:2023-11-04 13:06:27 25 4
gpt4 key购买 nike

我必须集成 LabVIEW 和 C++ 代码,这些代码最终将在 FRC 机器人的板外处理器上运行。

我几个月前才开始用 C++ 编程,所以我对用 C++ 做任何太复杂的事情没有什么经验。

我决定通过 UDP 通信集成 LabVIEW 和 C++(我选择 UDP 而不是 TCP,因为我尝试了 TCP,但它对我的目的产生了太多延迟)。我编写了我的程序 C++ 客户端 UDP 程序,但是当谈到在 LabVIEW 中编写我的 UDP 程序时,我感到困惑。

在 C++ 中,客户端和服务器 UDP 程序之间似乎有明显的区别。在 C++ 中,客户端似乎尝试连接到服务器并且服务器响应。然而,在LabVIEW中,服务器似乎是由谁先发送来决定的。

下面是我的 C++ 代码以及我尝试使用 LabVIEW 程序但失败的图片。感谢您提供的任何帮助(如果您能告诉我如何使用 dll 执行此操作,这将非常有帮助,因为我找不到任何好的帮助)。

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <iostream>
#include <Windows.h>

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

using namespace std;

#define Input_PORT "0914"
#define Output_PORT "152120"
#define General_PORT "444"

int main(int argc, char* argv[]) {

WSADATA wsaData; //data for winsock

int iResult; //result of intelizing winsock

// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
printf("WSA Intelized: %d\n", iResult);

system("pause");

//Creating Socket
struct addrinfo *result = NULL, *ptr = NULL, hints;

ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

// Resolve the server address and port
iResult = getaddrinfo(argv[1], General_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return 1;
}

SOCKET ConnectSocket = INVALID_SOCKET; //Create Connecting Socket

// Attempt to connect to the first address returned by
// the call to getaddrinfo
ptr = result;

// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);


if (ConnectSocket == INVALID_SOCKET) {
printf("Error at socket(): %ld\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}

printf("Socket Created\n");
system("pause");

// Connect to server.
iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
}

// Should really try the next address returned by getaddrinfo
// if the connect call failed
// But for this simple example we just free the resources
// returned by getaddrinfo and print an error message

freeaddrinfo(result);

if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}

printf("Connected\n");
system("pause");

return 0;
}

LabVIEW Block Diagram

最佳答案

一般的UDP协议(protocol)是没有“连接”的,是无连接的。一个数据包被发送到一个 IP 地址和一个端口,并且没有监督是否收到该数据包。

双方都可以监听一个 UDP 端口,也可以将数据发送到网络中的另一个 UDP 端口/IP 地址。如果您调用一侧服务器(因为它正在响应命令)或客户端(因为它正在发送命令并且正在等待对它的响应)只是由您的应用程序定义。

UDP packet包含“目标端口号”和“源端口号”。接收方监听特定端口(对于发送方而言是“目标端口”),发送方将数据发送到该端口。接收方通过发送方的“源端口号”获取另一方正在监听它想要发回内容的情况的信息。

我发现那里有一些关于“winsock 中的 UDP 套接字编程”的有用信息,我将其用于我的测试程序(我现在无法根据我的声誉状态发布更多链接,所以请自行添加 http://):www.binarytides.com/udp-socket-programming-in-winsock

#include "stdafx.h"

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

#pragma comment(lib,"ws2_32.lib") // Winsock Library

#define REMOTE_SERVER_NAME "127.0.0.1" // IP address of udp remote server
#define REMOTE_SERVER_PORT 61557 // UDP port of the remote server which is listening
#define LOCAL_RCV_BUF_LEN 20 // max. length of receiving buffer
#define LOCAL_CMD_MSG_LEN 10 // max. length of command to send

int main(void)
{
struct sockaddr_in sockaddr_in1; // the SOCKADDR_IN structure specifies a transport address and port for the AF_INET address family.
int socket1; // descriptor referencing the socket
int sockaddr_in1_len = sizeof(sockaddr_in1);
char cmd_message[LOCAL_CMD_MSG_LEN]; // command message, read from input keys, send to the UDP server
char rcv_buf[LOCAL_RCV_BUF_LEN]; // receive buffer to store received data
WSADATA wsdata1; // contains information about the Windows Sockets implementation

// initialise winsock
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsdata1) != 0)
{
printf("Failed. Error Code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
printf("Initialised.\n");

// create socket
if ((socket1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == SOCKET_ERROR)
{
printf("socket() failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}

// setup address structure
memset((char *)&sockaddr_in1, 0, sizeof(sockaddr_in1));
sockaddr_in1.sin_family = AF_INET;
sockaddr_in1.sin_port = htons(REMOTE_SERVER_PORT);
inet_pton(AF_INET, REMOTE_SERVER_NAME, &sockaddr_in1.sin_addr.S_un.S_addr);

// start communication
while (1)
{
printf("Enter command (d=date, t=time) : ");
gets_s(cmd_message, sizeof(cmd_message) - 1);

// send the message
if (sendto(socket1, cmd_message, strlen(cmd_message), 0, (struct sockaddr *) &sockaddr_in1, sizeof(sockaddr_in1)) == SOCKET_ERROR)
{
printf("sendto() failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}

// receive a reply and print it
memset(rcv_buf, '\0', sizeof(rcv_buf)); // clear the buffer by filling null, it might have previously received data
// try to receive some data, this is a blocking call
if (recvfrom(socket1, rcv_buf, sizeof(rcv_buf), 0, (struct sockaddr *) &sockaddr_in1, &sockaddr_in1_len) == SOCKET_ERROR)
{
printf("recvfrom() failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}

puts(rcv_buf);
}

closesocket(socket1);
WSACleanup();

return 0;
}

LabVIEW 有四个在此上下文中很重要的函数:

  • UDP Open(获取端口监听)
  • UDP Read(从“UDP Open”定义的端口读取数据,提供接收包的远程端口以发送回数据)
  • UDP 写入(获取要发送的数据以及远程端口)
  • UDP关闭

LabVIEW Code

如果需要,您可以从以下目的地下载我的测试源(与上述链接相同):dl.dropboxusercontent.com/u/16833149/Simple%20UDP%20-%20Command%20Receiver.7z

关于c++ - 使用 UDP LabVIEW 与 UDP c++ 套接字通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42104527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com