Saturday, July 30, 2011

IPC Socket - Simple Client Exaple

/**************************************************************
 * File   :  simple-client.cpp                                                
 * Desc   :
 * simple client socket reader.  It opens a socket, connects
 * to a server, reads the message, and closes.
 ***************************************************************/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT_TIME       13  // "time" (not available on RedHat)
#define PORT_FTP        21  // FTP connection port
#define SERVER_ADDR     "127.0.0.1"     // localhost
#define MAXBUF          1024

using namespace std;

int main()
{   int sockfd;
    struct sockaddr_in dest;
    char buffer[MAXBUF];

   /*
    * Open socket for streaming
    */
    if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
    {
        perror("Socket");
        exit(errno);
    }

   /*
    *Initialize server address/port struct
    */
    bzero(&dest, sizeof(dest));
    dest.sin_family = AF_INET;
    dest.sin_port = htons(PORT_FTP);
    if ( inet_aton(SERVER_ADDR, &dest.sin_addr) == 0 )
    {
        perror(SERVER_ADDR);
        exit(errno);
    }

   /*
    *Connect to server
    */
    if ( connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 )
    {
        perror("Connect ");
        exit(errno);
    }

   /*
    * Get "message from socket"
    */
    bzero(buffer, MAXBUF);
    recv(sockfd, buffer, sizeof(buffer), 0);
    printf("%s", buffer);

   /*
    *Clean up
    */
    close(sockfd);
    return 0;
}


//
//Output
//
/**************************************************************

[sgupta@rhel55x86 chap1]$ ./simple-client
220 (vsFTPd 2.0.5)
[sgupta@rhel55x86 chap1]$
***************************************************************/

No comments:

Post a Comment