Click to See Complete Forum and Search --> : receive data from specified IP address and Port number
onlyphanim
January 29th, 2008, 05:49 AM
Hi
I need help to receive data from Broadcast server. I have IP and Port number.
my system was connected broadcast server with port number : 1111
and with IP address : "124.12.125.2"
My task is to receive data from port
i write code like
#include<stdio.h>
#include<winsock.h>
void main()
{
WSADATA wsaData;
SOCKET SendSocket;
char SendBuf[215] ;
sockaddr_in RecvAddr;
int len =sizeof(RecvAddr);
int Port = 1111;
char ip[] = "124.12.125.2";
WSAStartup(MAKEWORD(2,2), &wsaData);
SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
RecvAddr.sin_family = AF_INET;
RecvAddr.sin_port = htons(Port);
RecvAddr.sin_addr.s_addr = inet_addr(ip);
while(1){
memset(SendBuf,' ' ,215) ;
if (recvfrom(SendSocket,SendBuf,sizeof( SendBuf),0,(struct sockaddr*)&RecvAddr,&len) != SOCKET_ERROR){
printf("rece from server:%s\n",SendBuf);
}
else
{
fprintf(stderr,"Client: recv() failed: error %d.\n", WSAGetLastError());
closesocket(SendSocket);
WSACleanup();
exit(0) ;
}
}
WSACleanup();
}
I am getting output as
Client: recv() faild: error 10022
what is this error ?
How can i read data from broadcast ?
please help me
Richard.J
January 29th, 2008, 11:15 AM
10022 means WSAEINVAL, what you could have easily looked up in the winsock.h file.
It means that you are passing an invalid value to recvfrom. Carefully check the parameters you pass to that function. Is SendSocket valid? Did WSAStartup succeed?
upredsun
January 29th, 2008, 06:50 PM
10022 means WSAEINVAL, what you could have easily looked up in the winsock.h file.
It means that you are passing an invalid value to recvfrom. Carefully check the parameters you pass to that function. Is SendSocket valid? Did WSAStartup succeed?
exactly!!
onlyphanim
January 30th, 2008, 04:40 AM
Than you Richard.J
please check this code, in this code if i did't use bind() it is giving error 10022, otherwise my program getting struck at recvfrom() function.
Please tell me what might be the problem
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);
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
onlyphanim
January 30th, 2008, 04:46 AM
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
Richard.J
January 30th, 2008, 10:37 AM
recvfrom blocks until there really is something on the socket. Do you have your broadcast server up and running? Is it sending data to the network?
You might want to download wireshark to sniff what's on the network.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.