c++ udp server doesn't receive packets












1















I've written a UDP receiver in C++ with a Java client.



The client's receiver IP (C++ server) is set like 192.168.1.xxx as server and client are in a private network on different devices.



Everything works just fine.



On another hand. I have a 3ds Max plugin which forks a thread to do some async stuff. The problem is when I run the mentioned C++ server code in that thread, it receives nothing.



Does the thread run in a local environment that can't access 192.168.1.xxx?



maxsdk C++ part



void InitWinsock()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
}

void update_state(Interface* ip)
{
SOCKET socketS;

InitWinsock();
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen = sizeof(from);
local.sin_family = AF_INET;
local.sin_port = htons(12345);
local.sin_addr.s_addr = inet_addr("192.168.1.106");

socketS = socket(AF_INET, SOCK_DGRAM, 0);
bind(socketS, (sockaddr*)&local, sizeof(local));
char buffer[1024];
while (1)
{
ZeroMemory(buffer, sizeof(buffer));

if (recvfrom(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, &fromlen) != SOCKET_ERROR)
{
printf("Received message from %s: %sn", inet_ntoa(from.sin_addr), buffer);
ip->PushPrompt((wchar_t*)buffer);
//sendto(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, fromlen);
}

Sleep(50);
}
closesocket(socketS);

return;
}

void PCamMaxPlugin::BeginEditParams(Interface* ip,IUtil* iu)
{
this->iu = iu;
hPanel = ip->AddRollupPage(
hInstance,
MAKEINTRESOURCE(IDD_PANEL),
DlgProc,
GetString(IDS_PARAMS),
0);
thread (update_state, ip).detach();
ip->PushPrompt(_M("----"));
}


C++ udp server



#include <winsock.h>
#include <cstdio>

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

void InitWinsock()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
}

int main()
{
SOCKET socketS;

InitWinsock();
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen = sizeof(from);
local.sin_family = AF_INET;
local.sin_port = htons(12345);
local.sin_addr.s_addr = inet_addr("192.168.1.106");

socketS = socket(AF_INET, SOCK_DGRAM, 0);
bind(socketS, (sockaddr*)&local, sizeof(local));
char buffer[1024];
while (1)
{
ZeroMemory(buffer, sizeof(buffer));
if (recvfrom(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, &fromlen) != SOCKET_ERROR)
{
printf("Received message from %s: %sn", inet_ntoa(from.sin_addr), buffer);
//sendto(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, fromlen);
}
Sleep(50);
}
closesocket(socketS);

return 0;
}









share|improve this question

























  • You can use netstat to check whether the UDP port is still opened after the server code was moved.

    – LWimsey
    Nov 21 '18 at 21:16











  • @LWimsey I'll check it out. thanks

    – A_Kz
    Nov 21 '18 at 21:30











  • @user4581301 you're right. I edited the post to avoid more downvotes. Thank you

    – A_Kz
    Nov 21 '18 at 21:31






  • 2





    Note, after recvfrom() exits, your printf and PushPrompt calls assume the buffer is null-terminated, but that is not guaranteed. You need to take the return value of recvfrom() into account to know how much of the buffer has been filled. Also, your 3DS code should NOT be calling WSAStartup() on every new thread created. Ideally it should be called only once, such as at app startup, or at least when the plugin is loaded. But, if you do call it in every thread, then you need to call WSACleanup() before the thread exits.

    – Remy Lebeau
    Nov 21 '18 at 22:31













  • So why it works fine as a separate program? In 3Ds code recvfrom always return SOCKET_ERROR. And your mentioned issue will come up only if the plugin is going to get loaded twice or more since it sees the requested port is busy. But basically, it must work once.

    – A_Kz
    Nov 22 '18 at 8:27
















1















I've written a UDP receiver in C++ with a Java client.



The client's receiver IP (C++ server) is set like 192.168.1.xxx as server and client are in a private network on different devices.



Everything works just fine.



On another hand. I have a 3ds Max plugin which forks a thread to do some async stuff. The problem is when I run the mentioned C++ server code in that thread, it receives nothing.



Does the thread run in a local environment that can't access 192.168.1.xxx?



maxsdk C++ part



void InitWinsock()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
}

void update_state(Interface* ip)
{
SOCKET socketS;

InitWinsock();
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen = sizeof(from);
local.sin_family = AF_INET;
local.sin_port = htons(12345);
local.sin_addr.s_addr = inet_addr("192.168.1.106");

socketS = socket(AF_INET, SOCK_DGRAM, 0);
bind(socketS, (sockaddr*)&local, sizeof(local));
char buffer[1024];
while (1)
{
ZeroMemory(buffer, sizeof(buffer));

if (recvfrom(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, &fromlen) != SOCKET_ERROR)
{
printf("Received message from %s: %sn", inet_ntoa(from.sin_addr), buffer);
ip->PushPrompt((wchar_t*)buffer);
//sendto(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, fromlen);
}

Sleep(50);
}
closesocket(socketS);

return;
}

void PCamMaxPlugin::BeginEditParams(Interface* ip,IUtil* iu)
{
this->iu = iu;
hPanel = ip->AddRollupPage(
hInstance,
MAKEINTRESOURCE(IDD_PANEL),
DlgProc,
GetString(IDS_PARAMS),
0);
thread (update_state, ip).detach();
ip->PushPrompt(_M("----"));
}


C++ udp server



#include <winsock.h>
#include <cstdio>

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

void InitWinsock()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
}

int main()
{
SOCKET socketS;

InitWinsock();
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen = sizeof(from);
local.sin_family = AF_INET;
local.sin_port = htons(12345);
local.sin_addr.s_addr = inet_addr("192.168.1.106");

socketS = socket(AF_INET, SOCK_DGRAM, 0);
bind(socketS, (sockaddr*)&local, sizeof(local));
char buffer[1024];
while (1)
{
ZeroMemory(buffer, sizeof(buffer));
if (recvfrom(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, &fromlen) != SOCKET_ERROR)
{
printf("Received message from %s: %sn", inet_ntoa(from.sin_addr), buffer);
//sendto(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, fromlen);
}
Sleep(50);
}
closesocket(socketS);

return 0;
}









share|improve this question

























  • You can use netstat to check whether the UDP port is still opened after the server code was moved.

    – LWimsey
    Nov 21 '18 at 21:16











  • @LWimsey I'll check it out. thanks

    – A_Kz
    Nov 21 '18 at 21:30











  • @user4581301 you're right. I edited the post to avoid more downvotes. Thank you

    – A_Kz
    Nov 21 '18 at 21:31






  • 2





    Note, after recvfrom() exits, your printf and PushPrompt calls assume the buffer is null-terminated, but that is not guaranteed. You need to take the return value of recvfrom() into account to know how much of the buffer has been filled. Also, your 3DS code should NOT be calling WSAStartup() on every new thread created. Ideally it should be called only once, such as at app startup, or at least when the plugin is loaded. But, if you do call it in every thread, then you need to call WSACleanup() before the thread exits.

    – Remy Lebeau
    Nov 21 '18 at 22:31













  • So why it works fine as a separate program? In 3Ds code recvfrom always return SOCKET_ERROR. And your mentioned issue will come up only if the plugin is going to get loaded twice or more since it sees the requested port is busy. But basically, it must work once.

    – A_Kz
    Nov 22 '18 at 8:27














1












1








1


0






I've written a UDP receiver in C++ with a Java client.



The client's receiver IP (C++ server) is set like 192.168.1.xxx as server and client are in a private network on different devices.



Everything works just fine.



On another hand. I have a 3ds Max plugin which forks a thread to do some async stuff. The problem is when I run the mentioned C++ server code in that thread, it receives nothing.



Does the thread run in a local environment that can't access 192.168.1.xxx?



maxsdk C++ part



void InitWinsock()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
}

void update_state(Interface* ip)
{
SOCKET socketS;

InitWinsock();
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen = sizeof(from);
local.sin_family = AF_INET;
local.sin_port = htons(12345);
local.sin_addr.s_addr = inet_addr("192.168.1.106");

socketS = socket(AF_INET, SOCK_DGRAM, 0);
bind(socketS, (sockaddr*)&local, sizeof(local));
char buffer[1024];
while (1)
{
ZeroMemory(buffer, sizeof(buffer));

if (recvfrom(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, &fromlen) != SOCKET_ERROR)
{
printf("Received message from %s: %sn", inet_ntoa(from.sin_addr), buffer);
ip->PushPrompt((wchar_t*)buffer);
//sendto(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, fromlen);
}

Sleep(50);
}
closesocket(socketS);

return;
}

void PCamMaxPlugin::BeginEditParams(Interface* ip,IUtil* iu)
{
this->iu = iu;
hPanel = ip->AddRollupPage(
hInstance,
MAKEINTRESOURCE(IDD_PANEL),
DlgProc,
GetString(IDS_PARAMS),
0);
thread (update_state, ip).detach();
ip->PushPrompt(_M("----"));
}


C++ udp server



#include <winsock.h>
#include <cstdio>

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

void InitWinsock()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
}

int main()
{
SOCKET socketS;

InitWinsock();
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen = sizeof(from);
local.sin_family = AF_INET;
local.sin_port = htons(12345);
local.sin_addr.s_addr = inet_addr("192.168.1.106");

socketS = socket(AF_INET, SOCK_DGRAM, 0);
bind(socketS, (sockaddr*)&local, sizeof(local));
char buffer[1024];
while (1)
{
ZeroMemory(buffer, sizeof(buffer));
if (recvfrom(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, &fromlen) != SOCKET_ERROR)
{
printf("Received message from %s: %sn", inet_ntoa(from.sin_addr), buffer);
//sendto(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, fromlen);
}
Sleep(50);
}
closesocket(socketS);

return 0;
}









share|improve this question
















I've written a UDP receiver in C++ with a Java client.



The client's receiver IP (C++ server) is set like 192.168.1.xxx as server and client are in a private network on different devices.



Everything works just fine.



On another hand. I have a 3ds Max plugin which forks a thread to do some async stuff. The problem is when I run the mentioned C++ server code in that thread, it receives nothing.



Does the thread run in a local environment that can't access 192.168.1.xxx?



maxsdk C++ part



void InitWinsock()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
}

void update_state(Interface* ip)
{
SOCKET socketS;

InitWinsock();
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen = sizeof(from);
local.sin_family = AF_INET;
local.sin_port = htons(12345);
local.sin_addr.s_addr = inet_addr("192.168.1.106");

socketS = socket(AF_INET, SOCK_DGRAM, 0);
bind(socketS, (sockaddr*)&local, sizeof(local));
char buffer[1024];
while (1)
{
ZeroMemory(buffer, sizeof(buffer));

if (recvfrom(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, &fromlen) != SOCKET_ERROR)
{
printf("Received message from %s: %sn", inet_ntoa(from.sin_addr), buffer);
ip->PushPrompt((wchar_t*)buffer);
//sendto(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, fromlen);
}

Sleep(50);
}
closesocket(socketS);

return;
}

void PCamMaxPlugin::BeginEditParams(Interface* ip,IUtil* iu)
{
this->iu = iu;
hPanel = ip->AddRollupPage(
hInstance,
MAKEINTRESOURCE(IDD_PANEL),
DlgProc,
GetString(IDS_PARAMS),
0);
thread (update_state, ip).detach();
ip->PushPrompt(_M("----"));
}


C++ udp server



#include <winsock.h>
#include <cstdio>

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

void InitWinsock()
{
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
}

int main()
{
SOCKET socketS;

InitWinsock();
struct sockaddr_in local;
struct sockaddr_in from;
int fromlen = sizeof(from);
local.sin_family = AF_INET;
local.sin_port = htons(12345);
local.sin_addr.s_addr = inet_addr("192.168.1.106");

socketS = socket(AF_INET, SOCK_DGRAM, 0);
bind(socketS, (sockaddr*)&local, sizeof(local));
char buffer[1024];
while (1)
{
ZeroMemory(buffer, sizeof(buffer));
if (recvfrom(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, &fromlen) != SOCKET_ERROR)
{
printf("Received message from %s: %sn", inet_ntoa(from.sin_addr), buffer);
//sendto(socketS, buffer, sizeof(buffer), 0, (sockaddr*)&from, fromlen);
}
Sleep(50);
}
closesocket(socketS);

return 0;
}






c++ sockets udp 3dsmax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 22:25









Remy Lebeau

337k19260456




337k19260456










asked Nov 21 '18 at 20:58









A_KzA_Kz

4417




4417













  • You can use netstat to check whether the UDP port is still opened after the server code was moved.

    – LWimsey
    Nov 21 '18 at 21:16











  • @LWimsey I'll check it out. thanks

    – A_Kz
    Nov 21 '18 at 21:30











  • @user4581301 you're right. I edited the post to avoid more downvotes. Thank you

    – A_Kz
    Nov 21 '18 at 21:31






  • 2





    Note, after recvfrom() exits, your printf and PushPrompt calls assume the buffer is null-terminated, but that is not guaranteed. You need to take the return value of recvfrom() into account to know how much of the buffer has been filled. Also, your 3DS code should NOT be calling WSAStartup() on every new thread created. Ideally it should be called only once, such as at app startup, or at least when the plugin is loaded. But, if you do call it in every thread, then you need to call WSACleanup() before the thread exits.

    – Remy Lebeau
    Nov 21 '18 at 22:31













  • So why it works fine as a separate program? In 3Ds code recvfrom always return SOCKET_ERROR. And your mentioned issue will come up only if the plugin is going to get loaded twice or more since it sees the requested port is busy. But basically, it must work once.

    – A_Kz
    Nov 22 '18 at 8:27



















  • You can use netstat to check whether the UDP port is still opened after the server code was moved.

    – LWimsey
    Nov 21 '18 at 21:16











  • @LWimsey I'll check it out. thanks

    – A_Kz
    Nov 21 '18 at 21:30











  • @user4581301 you're right. I edited the post to avoid more downvotes. Thank you

    – A_Kz
    Nov 21 '18 at 21:31






  • 2





    Note, after recvfrom() exits, your printf and PushPrompt calls assume the buffer is null-terminated, but that is not guaranteed. You need to take the return value of recvfrom() into account to know how much of the buffer has been filled. Also, your 3DS code should NOT be calling WSAStartup() on every new thread created. Ideally it should be called only once, such as at app startup, or at least when the plugin is loaded. But, if you do call it in every thread, then you need to call WSACleanup() before the thread exits.

    – Remy Lebeau
    Nov 21 '18 at 22:31













  • So why it works fine as a separate program? In 3Ds code recvfrom always return SOCKET_ERROR. And your mentioned issue will come up only if the plugin is going to get loaded twice or more since it sees the requested port is busy. But basically, it must work once.

    – A_Kz
    Nov 22 '18 at 8:27

















You can use netstat to check whether the UDP port is still opened after the server code was moved.

– LWimsey
Nov 21 '18 at 21:16





You can use netstat to check whether the UDP port is still opened after the server code was moved.

– LWimsey
Nov 21 '18 at 21:16













@LWimsey I'll check it out. thanks

– A_Kz
Nov 21 '18 at 21:30





@LWimsey I'll check it out. thanks

– A_Kz
Nov 21 '18 at 21:30













@user4581301 you're right. I edited the post to avoid more downvotes. Thank you

– A_Kz
Nov 21 '18 at 21:31





@user4581301 you're right. I edited the post to avoid more downvotes. Thank you

– A_Kz
Nov 21 '18 at 21:31




2




2





Note, after recvfrom() exits, your printf and PushPrompt calls assume the buffer is null-terminated, but that is not guaranteed. You need to take the return value of recvfrom() into account to know how much of the buffer has been filled. Also, your 3DS code should NOT be calling WSAStartup() on every new thread created. Ideally it should be called only once, such as at app startup, or at least when the plugin is loaded. But, if you do call it in every thread, then you need to call WSACleanup() before the thread exits.

– Remy Lebeau
Nov 21 '18 at 22:31







Note, after recvfrom() exits, your printf and PushPrompt calls assume the buffer is null-terminated, but that is not guaranteed. You need to take the return value of recvfrom() into account to know how much of the buffer has been filled. Also, your 3DS code should NOT be calling WSAStartup() on every new thread created. Ideally it should be called only once, such as at app startup, or at least when the plugin is loaded. But, if you do call it in every thread, then you need to call WSACleanup() before the thread exits.

– Remy Lebeau
Nov 21 '18 at 22:31















So why it works fine as a separate program? In 3Ds code recvfrom always return SOCKET_ERROR. And your mentioned issue will come up only if the plugin is going to get loaded twice or more since it sees the requested port is busy. But basically, it must work once.

– A_Kz
Nov 22 '18 at 8:27





So why it works fine as a separate program? In 3Ds code recvfrom always return SOCKET_ERROR. And your mentioned issue will come up only if the plugin is going to get loaded twice or more since it sees the requested port is busy. But basically, it must work once.

– A_Kz
Nov 22 '18 at 8:27












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420388%2fc-udp-server-doesnt-receive-packets%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420388%2fc-udp-server-doesnt-receive-packets%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory