Reputation: 165
I am trying to send out data in UDP format (without handing any error - just opening the socket and send data). The code used is as below:
#include<iostream>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
int main()
{
int sockfd;
sockfd = socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in client;
unsigned char* buffer = new unsigned char[1206]();
for(auto i=0; i<1206; i++){
buffer[i] = static_cast<unsigned char>(i);
}
client.sin_family = AF_INET;
client.sin_port = htons(5005);
client.sin_addr.s_addr = inet_addr("127.0.0.1");
socklen_t m = sizeof(client);
while(true){
sendto(sockfd,buffer,1206 ,0,(struct sockaddr *)&client,m);
cout<<"\nMessage sent...!! \n";
}
}
However, this is not being recognized as UDP protocol. When tried to capture it using wireshark, I get following result. (1206 bytes of buffered data as expected but 44 bytes of header which should be 42). What is the problem with the code?
Upvotes: 0
Views: 338
Reputation: 5486
Your code works just fine (tested on CentOS 7 with gcc 4.8.5). The capture method you are using is removing the Ethernet, IP and UDP layers, leaving you only with the payload.
Upvotes: 1