pethead
2nd November 2010, 03:49
I have this code in my C program.
Now I'm coding on Qt and want to remake this to QUdpSocket.
Is it really?
algorythm are here:
we have ip and port of udp server,
make data and send it to ip and port of server,
and waiting answer...
i can't use bind(ip,port), i don't know recv port, its generates the socket subsystem, and server get it from incoming datagramm.
i know Qudpsocket needs to be bind on fixed port.
Qudpsocket is unusable at my use?
unsigned char sendbuf[11] = "";
unsigned char recvbuf[DEFAULT_BUFLEN] = "";
SOCKET S;
sockaddr_in SA;
int SA_Size = sizeof(SA);
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
WSAData WSData;
WSAStartup(0x101,&WSData);
S = socket(AF_INET,SOCK_DGRAM,0);
SA.sin_family=AF_INET;
SA.sin_port=htons(2323);
SA.sin_addr.S_un.S_addr=inet_addr(_ip.c_str());
//filling sendbuf here..
//sending sendbuf
sendto(S, (char*)sendbuf,sizeof(sendbuf), 0,(struct sockaddr *)&SA, sizeof(SA))
//receiving answer
fd_set rfds;
struct timeval tv;
tv.tv_sec = _timeout;
tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_SET(S,&rfds);
if (select((int)S+1, &rfds, NULL, NULL, &tv) > 0)
{
//getting data from
iResult=recvfrom(S, (char*)recvbuf, recvbuflen, 0, (struct sockaddr *)&SA, &SA_Size);
closesocket(S);
WSACleanup();
}
...
Now I'm coding on Qt and want to remake this to QUdpSocket.
Is it really?
algorythm are here:
we have ip and port of udp server,
make data and send it to ip and port of server,
and waiting answer...
i can't use bind(ip,port), i don't know recv port, its generates the socket subsystem, and server get it from incoming datagramm.
i know Qudpsocket needs to be bind on fixed port.
Qudpsocket is unusable at my use?
unsigned char sendbuf[11] = "";
unsigned char recvbuf[DEFAULT_BUFLEN] = "";
SOCKET S;
sockaddr_in SA;
int SA_Size = sizeof(SA);
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
WSAData WSData;
WSAStartup(0x101,&WSData);
S = socket(AF_INET,SOCK_DGRAM,0);
SA.sin_family=AF_INET;
SA.sin_port=htons(2323);
SA.sin_addr.S_un.S_addr=inet_addr(_ip.c_str());
//filling sendbuf here..
//sending sendbuf
sendto(S, (char*)sendbuf,sizeof(sendbuf), 0,(struct sockaddr *)&SA, sizeof(SA))
//receiving answer
fd_set rfds;
struct timeval tv;
tv.tv_sec = _timeout;
tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_SET(S,&rfds);
if (select((int)S+1, &rfds, NULL, NULL, &tv) > 0)
{
//getting data from
iResult=recvfrom(S, (char*)recvbuf, recvbuflen, 0, (struct sockaddr *)&SA, &SA_Size);
closesocket(S);
WSACleanup();
}
...