المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : UDP connection -> outgoing port trouble (still)



C++ Programming
08-03-2009, 12:03 PM
Good morning,

last week I already wrote about my little problem. Then I got sick and and couldn´t take part in the conversation. Even though I got an answer it didn´t help (or I misunderstood). Now I wanted to ask/clarify and would be glad for some help.

The code:


SOCKET m_oUDPSocket;
SOCKADDR_IN m_oUDPAddress;

.
.
.

m_oUDPSocket = socket(AF_INET,SOCK_DGRAM,0);
memset(&m_oUDPAddress,0,sizeof(SOCKADDR_IN));
m_oUDPAddress.sin_family=AF_INET;
m_oUDPAddress.sin_port=htons(m_iClientPort);
m_oUDPAddress.sin_addr.s_addr=inet_addr(m_sClientIP.data());

.
.
.

int rc=sendto(m_oUDPSocket,(char*)packet,size+12,0,(SOCKADDR*)&m_oUDPAddress,sizeof(SOCKADDR_IN));


The problem: I´d like to send the data via 'sendto' ALWAYS on the same outgoing port.

The question: Where and how do I define this outgoing port? Do I have to create another SOCKADDR_IN with the server port and address and bind it to m_oUDPSocket?

Like this?


SOCKADDR_IN addr;
memset(&addr,0,sizeof(SOCKADDR_IN));
addr.sin_family=AF_INET;
addr.sin_port=htons(desiredPort);
addr.sin_addr.s_addr=INADDR_ANY;
rc=bind(m_oUDPSocket,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN));


Or am I on the wrong path here? And how can I test if it indeed sends from the desired port?

Thanks for any help.

Souldrift