CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2008
    Posts
    4

    receive data from specified IP address and Port number

    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

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: receive data from specified IP address and Port number

    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?

  3. #3
    Join Date
    Dec 2007
    Posts
    30

    Re: receive data from specified IP address and Port number

    Quote Originally Posted by Richard.J
    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!!
    Best,
    Kevin Jo
    http://www.upredsun.com
    **Easily and automatically build tcp-based or udp-based network protocol source code**

  4. #4
    Join Date
    Jan 2008
    Posts
    4

    Re: receive data from specified IP address and Port number

    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

  5. #5
    Join Date
    Jan 2008
    Posts
    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

  6. #6
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: receive data from specified IP address and Port number

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured