2015年2月11日 星期三

[Socket][C] Simple tutorial on using sockets for interprocess communication


Client :

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }

/* Input port number */
    portno = atoi(argv[2]);

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd < 0) 
        error("ERROR opening socket");

/* Input Server IP address */
    server = gethostbyname(argv[1]);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr));

    serv_addr.sin_family = AF_INET;

/* Copy a number of characters in one string to another */
    bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);

    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");

    printf("Please enter the message: ");
    bzero(buffer,256);
    fgets(buffer,255,stdin);

/* Return message to Server  */
    n = write(sockfd,buffer,strlen(buffer));

    if (n < 0) 
         error("ERROR writing to socket");

    bzero(buffer,256);

/* Read message from Server  */
    n = read(sockfd,buffer,255);
    if (n < 0) 
         error("ERROR reading from socket");
    printf("%s\n",buffer);
    close(sockfd);
    return 0;
}




Server :

/* A simple server in the internet domain using TCP
   The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

void error(const char *msg)
{
    perror(msg);
    exit(1);
}

int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }

/* socket - create an endpoint for communication */
     sockfd = socket(AF_INET, SOCK_STREAM, 0);

/* socket create fail return error message  */
     if (sockfd < 0) 
        error("ERROR opening socket");

     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);

/* Use the bind function to assign an address to a socket */
     if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
              error("ERROR on binding");

/* listen function to enable connection requests on the socket  */
     listen(sockfd,5);

     clilen = sizeof(cli_addr);

/* accept - accept a new connection on a socket */
     newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

/* socket create fail return error message  */
     if (newsockfd < 0) 
          error("ERROR on accept");

/* bzero - write zero-valued bytes  */
     bzero(buffer,256);

/* Read character from connection  */
     n = read(newsockfd,buffer,255);

/* If no read any thing return error  */
     if (n < 0) 
    error("ERROR reading from socket");

     printf("Here is the message: %s\n",buffer);

/* Return message to client  */
     n = write(newsockfd,"I got your message",18);

     if (n < 0) 
    error("ERROR writing to socket");

     close(newsockfd);
     close(sockfd);
     return 0; 
}


Reference:

1. Sockets Tutorial

0 意見:

張貼留言