Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.development.system > #591

how to check data is received on the socket ??

Newsgroups comp.os.linux.development.system
Date 2014-01-24 09:24 -0800
Message-ID <2670d8af-e019-46b9-b0e0-b349aaaada6b@googlegroups.com> (permalink)
Subject how to check data is received on the socket ??
From Hemanth Venkatappa <hemanthvenkatappa@gmail.com>

Show all headers | View raw


The below is a server side program and it is accepting a connection from the master. I want to call XcpIp_RxCallback (chunkLen, *pChunkData, port); the API, whenever I receive a data on the socket fd. Could anyone tell me how to do that ?? 

SOCKET sock;
SOCKET fd;
uint8* pChunkData;
uint16 chunkLen;
uint16 port = 52428;


void CreateSocket() {
    struct sockaddr_in server, client;  // creating a socket address structure: structure contains ip address and port number
    WORD wVersionRequested;
    WSADATA wsaData;
    int len;
    int iResult;

    u_long iMode = 0;
    int sockoptval = 1;

    printf("Initializing Winsock\n");

    wVersionRequested = MAKEWORD (1, 1);
    iResult =  WSAStartup (wVersionRequested, &wsaData);      
    if (iResult != NO_ERROR)
    printf("Error at WSAStartup()\n"); 

    // create socket
    sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock < 0) {
        printf("Could not Create Socket\n");
        //return 0;
    }

    printf("Socket Created\n");

    setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&sockoptval, 1);

    iResult = ioctlsocket(sock, FIONBIO, &iMode);
    if (iResult != NO_ERROR)
    printf("ioctlsocket failed with error: %ld\n", iResult);

    // create socket address of the server
    memset( &server, 0, sizeof(server));

    // IPv4 - connection
    server.sin_family = AF_INET;
    // accept connections from any ip adress
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    // set port
    server.sin_port = htons(52428);

    //Binding between the socket and ip address
    if(bind (sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
        printf("Bind failed with error code: %d", WSAGetLastError());
    }


    //Listen to incoming connections
    if(listen(sock, 3) == -1) {
        printf("Listen failed with error code: %d", WSAGetLastError());
    }

    printf("Server has been successfully set up - Waiting for incoming connections");

    for(;;) {
        len = sizeof(client);
        fd = accept(sock, (struct sockaddr*) &client, &len);

        if (fd < 0){
        printf("Accept failed");
        //closesocket(sock);
        }
        //echo(fd);

        printf("\n Process incoming connection from (%s , %d)", inet_ntoa(client.sin_addr),ntohs(client.sin_port));
    }
}

void main() {
    HANDLE h1,h2,h3;
    int bytes_recieved;

    double Task2ms_Raster, Task10ms_Raster, Task100ms_Raster ;

    pChunkData = &recv_data;
    chunkLen = sizeof(pChunkData);

    CreateSocket();

    while(1) {
        bytes_recieved = recv(fd, recv_data, 100 , 0 );
        recv_data[bytes_recieved] = '\0';
    }                         
    XcpIp_RxCallback (chunkLen, *pChunkData, port);
}

Back to comp.os.linux.development.system | Previous | NextNext in thread | Find similar


Thread

how to check data is received on the socket ?? Hemanth Venkatappa <hemanthvenkatappa@gmail.com> - 2014-01-24 09:24 -0800
  Re: how to check data is received on the socket ?? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2014-01-24 22:30 +0000

csiph-web