C++ Matlab 2012到Visual Studio 2013 UDP通信

C++ Matlab 2012到Visual Studio 2013 UDP通信,c++,matlab,sockets,visual-studio-2013,C++,Matlab,Sockets,Visual Studio 2013,我需要建立Matlab版本2012和 Visual Studio 2013版,采用UDP协议 Matlab安装的计算机将成为我的服务器 Visual Studio 2013安装的计算机将是我的客户端 基本操作 我必须从MATLAB连续发送文本文件,并在Visual Studio 2013中接收 操作系统 Matlab操作系统:Mac OSX 10.10和Visual Sudio 2013操作系统:Windows 10 我将在它们之间使用UDP协议,并将整数值从MATLAB发送到visua

我需要建立Matlab版本2012和 Visual Studio 2013版,采用UDP协议


Matlab安装的计算机将成为我的服务器

Visual Studio 2013安装的计算机将是我的客户端


基本操作 我必须从MATLAB连续发送文本文件,并在Visual Studio 2013中接收


操作系统 Matlab操作系统:Mac OSX 10.10和Visual Sudio 2013操作系统:Windows 10


我将在它们之间使用UDP协议,并将整数值从MATLAB发送到visualstudio


我尝试了这种通信,在两台计算机之间,它们都安装了Visual Studio,我成功地在它们之间发送了字节


不幸的是,我无法在MATLAB和Visual Studio之间建立通信

客户端代码
#定义WIN32_LEAN_和_MEAN
#包括
#包括
#包括
#包括
#包括
//需要链接到Ws2_32.lib、Mswsock.lib和Advapi32.lib
#pragma注释(lib,“Ws2_32.lib”)
#pragma注释(lib,“Mswsock.lib”)
#pragma注释(lib,“AdvApi32.lib”)
#定义默认值\u BUFLEN 512
#定义默认_端口“27015”
内部cdecl主(内部argc,字符**argv)
{
WSADATA WSADATA;
SOCKET ConnectSocket=无效的_套接字;
struct addrinfo*result=NULL,
*ptr=NULL,
提示;
char*sendbuf=“(!)你好,我是客户联想Z570”;
char recvbuf[默认值];
国际结果;
int recvbuflen=默认值;
//验证参数
如果(argc!=2){
printf(“\n\n\t\t\t页面:%s服务器名\n\n\n\t\t\t”,argv[0]);
系统(“暂停”);
返回1;
}
//初始化Winsock
iResult=WSAStartup(MAKEWORD(2,2)和wsaData);
如果(iResult!=0){
printf(“\n\n\t\t\t启动失败,错误:%d\n\n\n\t\t”,iResult);
系统(“暂停”);
返回1;
}
零内存(&提示,sizeof(提示));
hits.ai_family=AF_unsec;
hits.ai_socktype=SOCK_流;
hits.ai_protocol=IPPROTO_TCP;
//解析服务器地址和端口
iResult=getaddrinfo(argv[1],默认端口、提示和结果);
如果(iResult!=0){
printf(“\n\n\t\t\tGetAddressInfo失败,错误:%d\n\n\n\t\t\t”,iResult);
WSACleanup();
系统(“暂停”);
返回1;
}
//尝试连接到某个地址,直到连接成功
for(ptr=result;ptr!=NULL;ptr=ptr->ai_next){
//创建用于连接到服务器的套接字
连接插座=插座(ptr->ai_系列,ptr->ai_插座类型,
ptr->ai_协议);
if(ConnectSocket==无效的_套接字){
printf(“\n\n\t\t\t套接字失败,错误:%ld\n\n\n\t\t\t”,WSAGetLastError());
WSACleanup();
系统(“暂停”);
返回1;
}
//连接到服务器。
iResult=connect(ConnectSocket,ptr->ai_addr,(int)ptr->ai_addrlen);
if(iResult==SOCKET\u错误){
闭合插座(连接插座);
ConnectSocket=无效的_套接字;
继续;
}
打破
}
freeaddrinfo(结果);
if(ConnectSocket==无效的_套接字){
printf(“\n\n\t\t\t可连接到服务器!\n\n\n\t\t\t”);
WSACleanup();
系统(“暂停”);
返回1;
}
//发送初始缓冲区
iResult=send(ConnectSocket,sendbuf,(int)strlen(sendbuf),0);
if(iResult==SOCKET\u错误){
printf(“\n\n\t\t\t发送失败,错误:%d\n\n\n\t\t\t”,WSAGetLastError());
闭合插座(连接插座);
WSACleanup();
系统(“暂停”);
返回1;
}
printf(“\n\n\t\t发送的字节数:%ld\n\n\n\t\t\t”,iResult);
//由于不再发送数据,请关闭连接
iResult=关机(ConnectSocket,SD_发送);
if(iResult==SOCKET\u错误){
printf(“\n\n\t\t\t关机失败,错误:%d\n\n\n\t\t\t”,WSAGetLastError());
闭合插座(连接插座);
WSACleanup();
系统(“暂停”);
返回1;
}
//接收,直到对等方关闭连接
做{
iResult=recv(ConnectSocket,recvbuf,recvbuflen,0);
如果(iResult>0){
printf(“\n\n\t\t收到的字节数:%d\n\n\n\t\t\t”,iResult);
printf(“\n\n\t\t\t收到的数据包的内容:\n\n\n\t\t\t”);
对于(int i=0;i0);
//清理
闭合插座(连接插座);
WSACleanup();
系统(“暂停”);
返回0;
}
关于使用代码 我在Microsoft网站上找到了此代码。在启动代码之前,我设置了服务器的IP地址

错误 代码告诉我错误“无法连接服务器!”

你能帮帮我吗


任何想法都将不胜感激。谢谢。

我希望下面的例子能帮助任何愿意学习udp发送的人。以下代码可用于以太网和wifi连接

该代码连接到发送器的27015端口。例如,您希望连接8888端口,然后更改

无符号短端口=27015;至无符号短端口=8888

#ifndef UNICODE
#define UNICODE
#endif

#define WIN32_LEAN_AND_MEAN

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

// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")

int main()
{

int iResult = 0;

WSADATA wsaData;

SOCKET RecvSocket;
sockaddr_in RecvAddr;

unsigned short Port = 27015;

char RecvBuf[1024];
int BufLen = 1024;

sockaddr_in SenderAddr;
int SenderAddrSize = sizeof (SenderAddr);

//-----------------------------------------------
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
    wprintf(L"WSAStartup failed with error %d\n", iResult);
    return 1;
}
//-----------------------------------------------
// Create a receiver socket to receive datagrams
RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (RecvSocket == INVALID_SOCKET) {
    wprintf(L"socket failed with error %d\n", WSAGetLastError());
    return 1;
}
//-----------------------------------------------
// Bind the socket to any address and the specified port.
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);

iResult = bind(RecvSocket, (SOCKADDR *) & RecvAddr, sizeof (RecvAddr));
if (iResult != 0) {
    wprintf(L"bind failed with error %d\n", WSAGetLastError());
    return 1;
}
//-----------------------------------------------
// Call the recvfrom function to receive datagrams
// on the bound socket.
wprintf(L"Receiving datagrams...\n");
iResult = recvfrom(RecvSocket,
                   RecvBuf, BufLen, 0, (SOCKADDR *) & SenderAddr,  &SenderAddrSize);
if (iResult == SOCKET_ERROR) {
    wprintf(L"recvfrom failed with error %d\n", WSAGetLastError());
}

//-----------------------------------------------
// Close the socket when finished receiving datagrams
wprintf(L"Finished receiving. Closing socket.\n");
iResult = closesocket(RecvSocket);
if (iResult == SOCKET_ERROR) {
    wprintf(L"closesocket failed with error %d\n", WSAGetLastError());
    return 1;
}

//-----------------------------------------------
// Clean up and exit.
wprintf(L"Exiting.\n");
WSACleanup();
return 0;
}  
#ifndef UNICODE
#定义UNICODE
#恩迪夫
#定义WIN32_精益_和_平均值
#包括
#包括
#包括
//链接到ws2_32.lib
#pragma注释(lib,“Ws2_32.lib”)
int main()
{
int-iResult=0;
WSADATA WSADATA;
插座插座;
RecvAddr中的sockaddr_;
无符号短端口=27015;
char RecvBuf[1024];
int-BufLen=1024;
SenderAddr中的sockaddr_;
int SenderAddrSize=sizeof(SenderAddr);
//-----------------------------------------------
//初始化Winsock
iResult=WSAStartup(MAKEWORD(2,2)和wsaData);
如果(iResult!=无错误){
wprintf(L“WSAStartup失败,错误为%d\n”,iResult);
返回1;
}
//-----------------------------------------------
//创建接收器套接字以接收数据报
RecvSocket=插座(AF INET,插座DGR
#ifndef UNICODE
#define UNICODE
#endif

#define WIN32_LEAN_AND_MEAN

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

// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")

int main()
{

int iResult = 0;

WSADATA wsaData;

SOCKET RecvSocket;
sockaddr_in RecvAddr;

unsigned short Port = 27015;

char RecvBuf[1024];
int BufLen = 1024;

sockaddr_in SenderAddr;
int SenderAddrSize = sizeof (SenderAddr);

//-----------------------------------------------
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
    wprintf(L"WSAStartup failed with error %d\n", iResult);
    return 1;
}
//-----------------------------------------------
// Create a receiver socket to receive datagrams
RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (RecvSocket == INVALID_SOCKET) {
    wprintf(L"socket failed with error %d\n", WSAGetLastError());
    return 1;
}
//-----------------------------------------------
// Bind the socket to any address and the specified port.
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);

iResult = bind(RecvSocket, (SOCKADDR *) & RecvAddr, sizeof (RecvAddr));
if (iResult != 0) {
    wprintf(L"bind failed with error %d\n", WSAGetLastError());
    return 1;
}
//-----------------------------------------------
// Call the recvfrom function to receive datagrams
// on the bound socket.
wprintf(L"Receiving datagrams...\n");
iResult = recvfrom(RecvSocket,
                   RecvBuf, BufLen, 0, (SOCKADDR *) & SenderAddr,  &SenderAddrSize);
if (iResult == SOCKET_ERROR) {
    wprintf(L"recvfrom failed with error %d\n", WSAGetLastError());
}

//-----------------------------------------------
// Close the socket when finished receiving datagrams
wprintf(L"Finished receiving. Closing socket.\n");
iResult = closesocket(RecvSocket);
if (iResult == SOCKET_ERROR) {
    wprintf(L"closesocket failed with error %d\n", WSAGetLastError());
    return 1;
}

//-----------------------------------------------
// Clean up and exit.
wprintf(L"Exiting.\n");
WSACleanup();
return 0;
}