I am working on a C++ UDP program, that sends a string to another client and should receive an answer.
Sending works fine, but i cant receive any packets. I looked with wireshark and my computer receives the packet at the right port and from the right IP, but my program seems to ignore them.
Do you have any idea?
int startWinsock(void);
int main()
{
long receive;
SOCKET sock;
char buffer[256];
SOCKADDR_IN si_me;
SOCKADDR_IN si_other;
///////////// Start Winsock ///////////////
receive = startWinsock();
if (receive != 0)
{
printf("Error: startWinsock, error code: %d\n", receive);
return 1;
}
else
{
printf("Winsock started!\n");
}
//////////// Create UDP Socket //////////////
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == INVALID_SOCKET)
{
printf("Fehler: Socket could not be created, errorcode: %d\n", WSAGetLastError());
return 1;
}
else
{
printf("UDP Socket created!\n");
}
si_me.sin_family = AF_INET;
si_me.sin_port = htons(1198);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
si_other.sin_family = AF_INET;
si_other.sin_port = htons(2000);
si_other.sin_addr.s_addr = inet_addr("10.2.134.10");
receive = connect(sock, (SOCKADDR*)&si_other, sizeof(SOCKADDR));
if (receive == SOCKET_ERROR)
{
cout << "Error : Connection Failed, Errorcode: " << WSAGetLastError() << endl;
}
else
{
cout << "Connected to" << si_other.sin_addr.s_addr << endl;
}
static int timeout = 500;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
// char broadcast = 1;
// setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast));
while (1)
{
printf("Insert Text: ");
gets(buffer);
//rc = sendto(s, buf, strlen(buf), 0, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN));
receive = send(sock, buffer, strlen(buffer), 0);
if (receive == SOCKET_ERROR)
{
//printf("error: sendto, error code: %d\n",WSAGetLastError());
printf("Error: send, error code: %d\n", WSAGetLastError());
//return 1;
}
else
{
printf("%d bytes sent!\n", receive);
}
static int timeout = 500;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
int wait = 0;
while (wait == 0)
{
//rc = recvfrom(s, buf, 256, 0, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN));
receive = recv(sock, buffer, sizeof(buffer), 0);
if (receive == SOCKET_ERROR)
{
//printf("Fehler: recvfrom, fehler code: %d\n",WSAGetLastError());
printf("Fehler: recv, fehler code: %d\n", WSAGetLastError());
//return 1;
}
else
{
wait = 1;
printf("%d bytes received!\n", receive);
buffer[receive] = '\0';
printf("Received: %s\n", buffer);
}
}
}
getchar();
return 0;
}
int startWinsock(void)
{
WSADATA wsa;
return WSAStartup(MAKEWORD(2, 0), &wsa);
}
To make it so your code works nearly as-is by sending to itself, do the following:
change the "me" port to match "other"... si_me.sin_port = htons( 2000 );
bind to it... bind( sock, (SOCKADDR*)&si_me, sizeof( SOCKADDR ) ); just before connect
As UDP is Datagram-Oriented and connectionless, you need to use recvfrom/sento instead of recv/send. Also the receivetimeout should be set with at timeval.
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval));
Related
I have a 10g NIC and a UDP stream of data about 3.8Gb/s. When I send packets (from another process) I lose packets on the receive side.
My CPU is at +/- 20% and the card is connected to a x8 PCIe. Jumbo packets set to 9014 Bytes (the UDP stream is very big). SO_RCVBUF set to 1073741824 (less then this number I get packet loss) and BUFLEN is 8192.
Receive function:
char buf[BUFLEN];
while (1)
{
fflush(stdout);
memset(buf, '\0', BUFLEN);
if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
{
printf(" recvfrom() failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
...Parsing packet...
}
Set socket function:
void set_sock(SOCKET *s, struct sockaddr_in *server, struct sockaddr_in *si_other)
{
int slen, recv_len;
char buf[BUFLEN];
WSADATA wsa;
slen = sizeof(si_other);
//Initialise winsock
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("Failed.Error Code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
if (LOBYTE(wsa.wVersion) != 2 || HIBYTE(wsa.wVersion) != 2) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
printf("Could not find a usable version of Winsock.dll\n");
WSACleanup();
exit(EXIT_FAILURE);
}
else
printf("The Winsock 2.2 dll was found okay\n");
//Create a socket
if ((*s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == INVALID_SOCKET)
{
printf(" Could not create socket : %d", WSAGetLastError());
}
printf(" Socket created.\n");
//Prepare the sockaddr_in structure
server->sin_family = AF_INET;
server->sin_addr.s_addr = INADDR_ANY;
server->sin_port = htons(PORT);
int res;
int bufferSize = 1073741824;
int bufferSizeLen = sizeof(bufferSize);
res = setsockopt(*s, SOL_SOCKET, SO_RCVBUF, (char *)&bufferSize, bufferSizeLen);
if (res == SOCKET_ERROR) {
printf("setsockopt for failed with error: %u\n", WSAGetLastError());
}
else
printf("Set SO_RCVBUF buffer: %d\n", bufferSize);
// If iMode != 0, non-blocking mode is enabled.
u_long iMode = 0;
res = ioctlsocket(*s, FIONBIO, &iMode);
if (res != NO_ERROR)
{
printf("ioctlsocket failed with error: %ld\n", res);
}
//Bind
if (bind(*s, (struct sockaddr *)server, sizeof(*server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d", WSAGetLastError());
exit(EXIT_FAILURE);
}
else
{
puts("Bind done\n");
}
}
What else can I configure or do to prevent the packet loss when transmitting packets?
I have a device broadcasting over ethernet, using the code below this successfully receives the packets. The device also unicasts the same packet over WiFi but this is not received (Disabling all other interfaces except WiFi in windows I get a timeout error). If I change:
address.sin_addr.s_addr = htonl(INADDR_ANY);
to
address.sin_addr.s_addr = inet_addr("192.168.0.13");
Which is the address of the wifi interface it does receive the packets. The packets over both ethernet and WiFi can be seen in WireShark, the only difference between them are the destination addresses (broadcast and unicast) and the ttl.
Here is the code:
int main()
{
int iResult;
char recv_buf[4000];
int opt = 1;
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
printf("WSAStartup failed with error: %d\n", err);
return 1;
}
SOCKET handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
int return_code = 0;
return_code = setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char *>(&opt), sizeof(opt));
std::cout<<"socket option 1 return:" << return_code << std::endl;
DWORD tv = 100;
return_code = setsockopt(handle, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<char *>(&tv), sizeof(tv));
std::cout << "socket option 2 return:" << return_code<< std::endl;
sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(3000);
address.sin_addr.s_addr = htonl(INADDR_ANY);
//address.sin_addr.s_addr = inet_addr("192.168.0.13"); //this works
memset(&address.sin_zero, 0, sizeof(address.sin_zero));
std::cout << "bind return:" << bind(handle, (const sockaddr *)&address, sizeof(sockaddr_in)) << std::endl;
// Receive data until the server closes the connection
do {
Sleep(5000);
iResult = recv(handle, recv_buf, 4000, 0);
if (iResult > 0)
printf("Bytes received: %d\n", iResult);
else if (iResult == 0)
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());
} while (true);
return 0;
}
Why when run the program and send data to server return this errorrecv failed: Transport endpoint is not connected or don't show server accepted just show the message of send data function in client
server.cpp:
int main() {
char packet[30];
char buffer[20] = "I got your message";
int conn_sock, comm_sock, n, m;
struct sockaddr_in server_addr, client_addr;
if ((conn_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Couldn't create socket");
exit(1);
}
cout << "Already create socket!!!\n" << endl;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(0);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(&server_addr, 0, sizeof(server_addr));
if (bind(conn_sock, (struct sockaddr *) &server_addr, sizeof(server_addr))
== -1) {
perror("couldn't bind");
exit(1);
}
if (listen(conn_sock, 10) == -1) {
perror("couldn't listen");
exit(1);
}
cout << "Listening For Connection...\r" << endl;
socklen_t len = sizeof(server_addr);
if (getsockname(conn_sock, (struct sockaddr *) &server_addr, &len) == -1)
perror("getsockname");
else
printf("port number %d\n", ntohs(server_addr.sin_port));
while (1) {
memset(&client_addr, 0, sizeof(client_addr));
if ((comm_sock = accept(conn_sock, (struct sockaddr *) &client_addr,
(socklen_t *) &client_addr)) == -1) {
perror("couldn't accept\n");
continue;
}
cout << "accepted" << endl;
bzero(packet, 10);
m = recv(conn_sock, packet, 10, 0);
if (m < 0) {
perror("recv failed");
exit(1);
}
cout<<"recieved"<<endl;
/* Write a response to the client */
n = send(conn_sock, buffer, sizeof(buffer), 0);
if (n < 0) {
perror("ERROR send to client");
exit(1);
}
close(n);
close(m);
close(comm_sock);
}
close(conn_sock);
return 0;
}
cilent.cpp:
#define MYPORT 51833
namespace personalization {
bool client::conn() {
//create socket if it is not already created
if (sock == -1) {
//Create socket
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1) {
perror("Could not create socket");
}
cout << "Socket created" << endl;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(MYPORT);
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(sock , (struct sockaddr *)&server_addr , sizeof(server_addr)) < 0)
{
perror("connect failed. Error");
return false;
}
cout<<"Connected\n";
return true;
close(sock);
}
bool client::send_data() {
//Send some data
if( send(sock , packet , sizeof( packet ) , 0) < 0)
{
perror("Send failed");
return false;
}
cout<<"Data send\n";
return true;
close(sock);
}
bool client::rec_data() {
char buffer[20];
string reply;
//Receive a echo from the server
if (recv(sock, buffer, sizeof(buffer), 0) < 0) {
perror("receive failed");
return false;
}
reply = buffer;
return true;
close(sock);
}
client::client() {
// TODO Auto-generated constructor stub
sock=-1;
}
output is:
server:Already create socket!!!
Listening For Connection...
port number 51833
client: Socket created
Connected
Data send
receive failed: Connection reset by peer
or:
server:Already create socket!!!
Listening For Connection...
port number 51833
client:Socket created
Connected
Data send
srver:accepted
recv failed: Transport endpoint is not connected
In the server's recv and send calls, you need to pass the socket returned from accept.
So instead of
m = recv(conn_sock, packet, 10, 0);
do
m = recv(comm_sock, packet, 10, 0);
Same goes for the send call.
Also, don't call close on n and m, that is to say remove these two lines of code:
close(n);
close(m);
EDIT: Sorry, while I'm at it, this is probably not what you intended in the client's send_data and rec_data:
return true;
close(sock);
I am using winsock socket api to send a udp packet with a very high port number, which I expect to be unused at the destination. My intention is to receive back one icmp packet with destination unreachable/port unreachable message. I have created two sockets, one on which I am sending UDP packet and other socket on which I am expecting the icmp packet. The send is successful. The destination returns the ICMP reply as well, this I can verify on wireshark. Now when I do a recv, to receive data, the recv function hangs. My objective is to read the data from recv function, which does not happen.
Any help for understaning/fixing this behavior will be highly appreciated. Thanks in advance.
I am attaching the code snippets here...
void sendPacket(unsigned int socketFd, char *packet, char* remoteIP, char* pingType)
{
int nsent = -1;
int rc = -1;
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(struct addrinfo));
if (strcasecmp(pingType, "UDP")==0)
{
hints.ai_flags = AI_CANONNAME; /* always return canonical name */
hints.ai_family = AF_INET; /* 0, AF_INET, AF_INET6, etc. */
hints.ai_socktype = SOCK_DGRAM; /* 0, SOCK_STREAM, SOCK_DGRAM, etc. */
}
rc = getaddrinfo(remoteIP, NULL, &hints, &res);
if ( rc != 0)
{
printf("... Function: %s\tError setting remote address. Exiting. ... \n", __FUNCTION__);
exit(-1);
}
if (strcasecmp(pingType, "UDP")==0)
{
((struct sockaddr_in *)res->ai_addr)->sin_port = htons(34344);
strcpy(packet, "TIMESTAMP");
}
do
{
if (strcasecmp(pingType, "UDP")==0)
{
nsent=sendto(socketFd, packet, strlen(packet), 0, (struct sockaddr *)res->ai_addr, (socklen_t)res->ai_addrlen);
if (nsent < 0)
{
continue;
}
}
}while(nsent < 0);
return;
}
double receivePacket(int socketFd, struct timeval* tvSend, pingReply** lastReplyNode, char* pingType)
{
ssize_t nRecv = -1;
double rc = -1;
char recvbuf[1024];
do
{
nRecv = recv(socketFd, (char *)recvbuf, 1024, 0);
}
while(nRecv < 0);
if (nRecv < 0)
{
return -1;
}
rc = processPacket(recvbuf, nRecv, tvSend, lastReplyNode, pingType);
if (rc == -1)
{
printf("... Function: %s\tReceiving error in Data/Protocol ...\n", __FUNCTION__);
return -1;
}
return rc;
}
void createSocket(unsigned int *sendSocketFd, unsigned int *receiveSocketFd, char *pingType)
{
#ifdef _WIN32
WORD wVersionRequested;
WSADATA wsaData;
int err;
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
wVersionRequested = MAKEWORD(2, 2);
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0)
{
/* Tell the user that we could not find a usable */
/* Winsock DLL. */
printf("WSAStartup failed with error: %d\n", err);
exit(-1);
}
#endif
#ifdef _WIN32
if (strcasecmp(pingType, "UDP")==0)
{
int rc = -1;
struct sockaddr_in src_address;
unsigned long int length;
int optval = 1;
DWORD Length;
OSVERSIONINFO g_OSVersionInfo;
BOOLEAN g_IsWindowsLonghorn = TRUE;
BOOLEAN g_UseFtosToSetTos = TRUE;
int ret, iVal=0;
unsigned int sz = sizeof(iVal);
g_OSVersionInfo.dwOSVersionInfoSize = sizeof( OSVERSIONINFO);
GetVersionEx( &g_OSVersionInfo );
if( g_OSVersionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT )
{
if( g_OSVersionInfo.dwMajorVersion >= 6 )
{
g_IsWindowsLonghorn = TRUE;
g_UseFtosToSetTos = TRUE;
}
}
*receiveSocketFd = INVALID_SOCKET;
*receiveSocketFd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (*receiveSocketFd < 0)
{
printf("Function: %s\tReceiving Socket creation error.\tErrNo %d. ...\n", __FUNCTION__, WSAGetLastError());
exit(-1);
}
src_address.sin_family=AF_INET;
src_address.sin_addr.s_addr=inet_addr("x.x.x.x");
src_address.sin_port=htons(0);
rc = bind((SOCKET)*receiveSocketFd,(struct sockaddr *)&src_address,sizeof(src_address));
if (rc < 0)
{
printf("Function: %s\tReceiving Socket bind error.\tErrNo %d. ...\n", __FUNCTION__, WSAGetLastError());
exit(-1);
}
iVal = 30000; // in milliseconds
ret = setsockopt(*receiveSocketFd, SOL_SOCKET, SO_RCVTIMEO, (char *)&iVal, sz);
if (ret == SOCKET_ERROR)
{
printf("%d\n", WSAGetLastError());
return; // Temporary
}
rc = WSAIoctl((SOCKET)*receiveSocketFd, SIO_RCVALL, &optval, sizeof(optval), NULL, 0, &length, NULL, NULL);
if (rc == SOCKET_ERROR)
{
printf("Function: %s\tReceiving Socket ioctl error.\tErrNo %d. ...\n", __FUNCTION__, WSAGetLastError());
exit(-1);
}
printf("Function: %s\treceiveSocketFd %d ...\n", __FUNCTION__, *receiveSocketFd);
}
else
{
*receiveSocketFd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (*receiveSocketFd < 0)
{
printf("Function: %s\tReceiving Socket creation error.\tErrNo %d. ...\n", __FUNCTION__, WSAGetLastError());
exit(-1);
}
printf("Function: %s\treceiveSocketFd %d ...\n", __FUNCTION__, *receiveSocketFd);
}
#endif
#ifndef _WIN32
unsigned int size = 1024; /* OK if setsockopt fails */
setsockopt(*receiveSocketFd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
#else
char size[5] = "1024"; /* OK if setsockopt fails */
setsockopt(*receiveSocketFd, SOL_SOCKET, SO_RCVBUF, size, sizeof(size));
#endif
if (strcasecmp(pingType, "UDP")==0)
{
*sendSocketFd = socket(AF_INET, SOCK_DGRAM, 0);
if (*sendSocketFd < 0)
{
printf("Send Socket creation error.");
exit(-1);
}
printf("Function: %s\tsendSocketFd %d ...\n", __FUNCTION__, *sendSocketFd);
}
return;
}
You don't get 'destination unreachable' unless the socket is connected, in the UDP sense, and then you don't get it via recv(), you get it as an errno value from a probably subsequent send().
I've bumped into a problem with my broadcasting server. basically, I want it to send broadcasts continuously from the moment I launch it. for some reason it will not start until it receives a connection from somewhere. I must have messed up something but I can't realise what.
here is my code:
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET sock;
sock = socket(AF_INET, SOCK_DGRAM, 0);
char broadcast = 'a';
if(setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) < 0)
{
perror("broadcast options");
closesocket(sock);
return 1;
}
struct sockaddr_in Recv_addr;
struct sockaddr_in Sender_addr;
int len = sizeof(struct sockaddr_in);
char recvBuff[50];
int recvBuffLen = 50;
//char sendMsg[] = "broadcast message from salam rofl";
Recv_addr.sin_family = AF_INET;
Recv_addr.sin_port = htons(PORT);
Recv_addr.sin_addr.s_addr = INADDR_ANY;
if(bind(sock, (sockaddr*)&Recv_addr, sizeof(Recv_addr)) < 0)
{
perror("bind");
_getch;
closesocket(sock);
return 1;
}
//recvfrom(sock, recvBuff, recvBuffLen, 0, (sockaddr *)&Sender_addr, &len);
//cout << "\nreceived message: " << recvBuff;
while(1)
{
Sleep(3000);
//_getch();
getTime();
if(sendto(sock, currentTime, strlen(currentTime)+1, 0, (sockaddr *)&Sender_addr, sizeof(Sender_addr)) < 0)
{
perror("borhot send: ");
_getch();
closesocket(sock);
return 0;
}
else cout << "message sent successfully";
}
_getch;
closesocket(sock);
WSACleanup();
return 0;
basically if I remove recvfrom, it will give me a send error ("No error") which simply puzzles me. also, if I send it something with a client, it will start broadcasting, but if I connect with another client, only the first client is receiving the broadcast.
thank you in advance.
I doubt it matters but I'm trying to broadcast the current time.
You are not initializing Sender_Addr so you are not telling sendto() where to actually broadcast to.
Try this instead:
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
SOCKET sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == INVALID_SOCKET)
{
perror("socket creation");
_getch;
return 1;
}
BOOL enabled = TRUE;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char*)&enabled, sizeof(BOOL)) < 0)
{
perror("broadcast options");
_getch;
closesocket(sock);
return 1;
}
struct sockaddr_in Sender_addr;
Sender_addr.sin_family = AF_INET;
Sender_addr.sin_port = htons(BROADCAST_PORT);
Sender_addr.sin_addr.s_addr = inet_addr("Broadcast IP Here");
struct sockaddr_in Recv_addr;
Recv_addr.sin_family = AF_INET;
Recv_addr.sin_port = htons(PORT);
Recv_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sock, (sockaddr*)&Recv_addr, sizeof(Recv_addr)) < 0)
{
perror("bind");
_getch;
closesocket(sock);
return 1;
}
while(1)
{
Sleep(3000);
getTime();
if (sendto(sock, currentTime, strlen(currentTime)+1, 0, (sockaddr *)&Sender_addr, sizeof(Sender_addr)) < 0)
{
perror("borhot send: ");
_getch();
closesocket(sock);
return 0;
}
cout << "message sent successfully";
}
_getch;
closesocket(sock);
WSACleanup();
return 0;
Looks like your Sender_addr is never being initialized, thus when you remove the recvfrom you're getting an error, and when the recvfrom is in place it's getting populated with the address of the first client to connect (but never being updated).
If you don't know the addresses of the clients that you want to broadcast to, you'll need to setup some handshake where they send you a ping, you receive it with recvfrom, and you store their address in a list or something. Then, when you broadcast, you need to send your message to every client address in the list.