|
-
January 30th, 2008, 05:46 AM
#4
Re: receive data from specified IP address and Port number
Thank you upredsun for u r reply
If i use bind() code is getting struck at recvfrom() function, if i didnot use bind() it is giving error number 10022.
Please tell me what might be the mistake and how do i rectify it ?
code ....
/*********************************/
#include <stdio.h>
#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
void main() {
WSADATA wsaData;
SOCKET RecvSocket;
sockaddr_in RecvAddr;
int Port = 6791;
char RecvBuf[1024];
int BufLen = 1024;
sockaddr_in SenderAddr;
int SenderAddrSize = sizeof(SenderAddr);
// Initialize Winsock
WSAStartup(MAKEWORD(2,2), &wsaData);
// Create a receiver socket to receive datagrams
RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
// Bind the socket to any address and the specified port.
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY); // (or)some IP address(112.12.13.14)
bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));
// Call the recvfrom function to receive datagrams
// on the bound socket.
printf("Receiving datagrams...\n");
if(recvfrom(RecvSocket,RecvBuf,BufLen,0,(SOCKADDR *)&SenderAddr,&SenderAddrSize)!= SOCKET_ERROR) {
printf("RecvData ==>%s",RecvBuf) ;
}
else{
fprintf(stderr,"Client: recvfrom() failed: error %d.\n", WSAGetLastError());
closesocket(RecvSocket);
WSACleanup();
exit(1) ;
}
// Close the socket when finished receiving datagrams
printf("Finished receiving. Closing socket.\n");
closesocket(RecvSocket);
// Clean up and exit.
printf("Exiting.\n");
WSACleanup();
return;
}
please help me to solve this problem
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|