使用UDP插座(C )错误接收数据
Wrong reception of data with UDP socket (C++)
早上好,
我需要建立UDP连接,以便将客户从客户端发送到服务器。我实现了此示例的连接启动表。
然后我定义了我的client.cc类:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include "udp_client_server.h"
#include "prova.h"
using udp_client_server::udp_client;
#define LDM_IP "127.0.0.1"
#define LDM_PORT 5445 //8000
using namespace std;
int main() {
cout << "!!!I'm a client!!!" << endl;
prova *ciao = new prova() ;
ciao->a = 86;
ciao->b = 8.1;
ciao->c = 90;
udp_client_server::udp_client *client = new udp_client(LDM_IP, LDM_PORT);
int b = client->send((char *)ciao,strlen((char *)ciao));
if (b != strlen((char *)ciao)){
std::cout<<"Packet corrupted "<< std::endl;
}
return 0;
}
然后the sender.cc类:
#include <iostream>
#include <string.h>
#include "udp_client_server.h"
#include "prova.h"
using udp_client_server::udp_server;
#define LDM_IP "127.0.0.1"
#define LDM_PORT 5445 //8000
using namespace std;
int main() {
cout << "!!!I'm a server!!!" << endl;
int size_max = 256;
udp_client_server::udp_server *server = new udp_server(LDM_IP, LDM_PORT);
char *buffer;
int gotMsg = server->recv(buffer,size_max);
prova *pippo = new prova();
pippo = (prova *)(buffer);
std::cout<< "result " << gotMsg <<std::endl;
std::cout<< "value " << pippo <<std::endl;
return 0;
}
建立了连接,但我没有得到我发送的内容。在每次运行时,我都会得到不同的错误值,而不是传输的值。
有人可以告诉我错误在哪里?预先感谢
send(.....,strlen((char *)ciao)不发送类
更改您的代码,发送结构变量。
尝试发送(..... sizeof(prova));
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 使用 char* 存储数据和产生的错误
- Static_cast转换为错误的数据类型,但结果仍然正确?
- 创建服务失败0x6f7存根收到错误数据
- 通过 host() 从 af::array 检索数据会导致错误的数据
- OpenCV C++ 3 维垫数据访问错误值
- 错误: 无效使用非静态数据成员"应用程序::应用程序构造函数"
- 错误 - 自定义数据类型作为有效负载,带有提升::几何
- C 如何忽略错误数据输入
- C ++编译指示OMP分割错误(数据竞争?)与数组
- C++ mac OS X El Capitan上的openmp,Valgrind错误(数据竞赛)
- 通过结构指针访问结构变量分配错误数据
- 我该怎么做 throw 来处理 C++ 中的错误数据类型
- C++:模拟餐饮公司计费程序-无法将错误数据输出到错误文件
- 使用boost::asio套接字接收错误数据
- 读取位图图像时获取错误数据
- 来自cudaMemcpy2D的错误数据
- VS2010 x64:64位平台VS2010中包含错误数据的对象数组
- c++ fstream输出错误数据