If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hybrid View

UDP Socket between C++ and Python

Hi there,

Appologise if i have posted in the wrong forum but since it uses both C++ and Python i thought that i would post here.

Basically i have a program which i want to send a string of data from C++ to Python in real time i.e. when the C++ program is being executed. My program compiles with visual studio (i am on a Windows 7 64bit system) and the program runs but the socket between C++ and python does not seem to work, the Python file just crashes as though it is listening but not picking anything up.

I use the Microsoft loopback adapter and i transmit the packet between both on the ip address which i have been given for the loopback adapter through windows:

Can somebody tell me if the packet in the C++ code below should be sent ok to Python via a socket? I'm thinking that the Python code on the other end is fine and should pick up the packet but as i say, for some reason the Python just hangs as though it is waiting for a packet to come. I am using winsock2 library to perform this. The two programs are to be ran on the same machine.

packet, address = mySocket.recvfrom(1024) #the code seems to be constantly stuck in a loop once this is added

As you can see, both use the same IP address and port numbers and so i am quite stumped. I am quite new to socket programming and so if there is any way i can see if it has been sent then that would be great. If you think i could write my code in a different way then that would be good if i could see the right way to write it in C++

Many thanks and if somebody could help me notice why my socket is now working and why it doesn't seem to connect to it in Python then that would be great as it just seems to hang in Python at the moment.

Re: UDP Socket between C++ and Python

Originally Posted by VictorN

1. Please, use Code tags when you post code snippets.

2. What does this line mean? What is 1024?

Hi there and appologies for not using the snippets. The 1024 is the size of the message in bytes which it can handle. Since i am only sending one string of data it should be fine. Does my C++ look ok or is there a way i can test if the actual message has been sent over to Python? As i say, the C++ program doesn't spit out any errors in the console or anything. Any help with it would be great for me to get it working.

Re: UDP Socket between C++ and Python

As i say, the C++ program doesn't spit out any errors in the console or anything.

That's not how c++ programs work unless you have coded the program to do this. Any call to a function that returns data should have the return value checked to make sure that the function has worked properly or not.

I suggest you amend your c++ code to check function return codes for errors and then see what happens. If the program still does not work then you will know which c++ function is failling and the error code.

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
C, C++ Compiler: Microsoft VS2015

Re: UDP Socket between C++ and Python

Originally Posted by 2kaud

That's not how c++ programs work unless you have coded the program to do this. Any call to a function that returns data should have the return value checked to make sure that the function has worked properly or not.

I suggest you amend your c++ code to check function return codes for errors and then see what happens. If the program still does not work then you will know which c++ function is failling and the error code.

Ok sorry to be a right pain but yeah, i last programmed something in c++ 4 years ago and have not really touched the language since, in fact the last thing i did on sockets and pipes was in standard C and over the years i have forgotten it and so excuse my "dumbness" if you like.

The reason i am doing this is because an application which has been pre-made was made in C++ to get data from an external device. I just need to get a string of data from it to Python so that i can code the rest in python once i have got this data.

Basically i decided to run a console application and put all of the code into it (to make it easier for both me and yourselves); for some reason the socket is not even starting and i have the error code below:

Socket failed with error: 0

the error is coming from this part of the code after i put the break points on and check:

Anybody got any ideas? I am basically just writing the string "hello" now through the socket and it is not working. I have tried launching the python program first and then the c++ one but the Python program still hangs as though it is waiting for something.

Re: UDP Socket between C++ and Python

Dudeand the rest you are a legend, i know that you only led me to some Microsoft reference library websites but my socket is now working. All i did was move the WSAStatup above the socket initialisation in my previous code and therefore it worked :-).

I have also added in all of the error checks too; in python i would normally use try and catch blocks but it's nice to know how to achieve a similar effect in C++. The python now picks up the string and i can use this data from within Python now.

Many thanks for all of your help, going through the basic and the modifying it for my need seemed to really help my cause.