Wednesday, November 27, 2013

Unix Network Programming: Simple Client Server using Sockets

To keep this program simple
  1. The server accepts connection from a single client only
  2. The server doesn't fork new process to handle client requst
  3. Client sends only a single message to server and server replies back with another message

Algorithm

Server

  1. Create socket
  2. Bind
  3. Listen
  4. Accept connection
  5. Read data sent by client
  6. Send data back to client
  7. Close socket descriptors
Client

  1. Create socket
  2. Connect to server
  3. Send data to server
  4. Receive data from server
  5. Close socket descriptors

Program

server
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define SERVERPORT 3098
#define SERVERIP "192.168.1.4"
#define SIZE 10
#define DEFAULT_FLAG 0
int main()
{
    int sockfd, newsockfd, n;
    int addrlen = sizeof(struct sockaddr_in);
    struct sockaddr_in *serveraddr = (struct sockaddr_in *)malloc(addrlen);
    struct sockaddr_in *clientaddr = (struct sockaddr_in *)malloc(addrlen);
    char buff[10];

    /* Create socket */
    if((sockfd = socket(AF_INET, SOCK_STREAM, DEFAULT_FLAG))<0)
    {
        printf("Error: Socket creation failed... Exiting...\n");
        return 0;
    }
  
    /* Bind */
    bzero(serveraddr,addrlen);                // Initailize serveraddr as null. defined in <string.h>
    serveraddr->sin_family = AF_INET;        // Set address family as INET
    serveraddr->sin_port = SERVERPORT;
    serveraddr->sin_addr.s_addr = inet_addr(SERVERIP);    // Convert character string to dotted decimal format. Defined in <arpa/inet.h>
    if(bind(sockfd, (struct sockaddr*) serveraddr, addrlen)<0)
    {
        printf("Error: Bind failed... Exiting...\n");
        return 0;
    }
  
    /* listen */
    listen(sockfd,4);
    printf("Server waiting for connection request . . .\n");
  
    /* Accept connection */
    if((newsockfd = accept(sockfd, (struct sockaddr*)clientaddr, &addrlen))<0)
        printf("Error: Accepting connection request failed\n");
    else
        printf("Connection Accepted \n");

    /* Read data sent by client */
    n = recv(newsockfd, buff, SIZE, DEFAULT_FLAG);
    buff[n] = '\0';
    printf("Message Received: %s\n",buff);
  
    /* Send replt to client */
    strcpy(buff,"Thank You");
    send(newsockfd, buff, strlen(buff), DEFAULT_FLAG);

    /* Close both socket descriptors*/
    close(newsockfd);
    close(sockfd);
    return 0;
}


client

#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define SERVERPORT 3098
#define SERVERIP "192.168.1.4"
#define SIZE 10
#define DEFAULT_FLAG 0
int main()
{
    int sockfd,n;
    int addrlen = sizeof(struct sockaddr_in);
    struct sockaddr_in *serveraddr = (struct sockaddr_in *)malloc(addrlen);
    char buff[10];

    /* Create socket */
    if((sockfd = socket(AF_INET, SOCK_STREAM, DEFAULT_FLAG))<0)
    {
        printf("Error: Socket creation failed... Exiting...\n");
        return 0;
    }
  
    /* Connect to server */
    bzero(serveraddr, addrlen);                // Initailize serveraddr as null. defined in <string.h>
    serveraddr->sin_family = AF_INET;        // Set address family as INET
    serveraddr->sin_port = SERVERPORT;
    serveraddr->sin_addr.s_addr = inet_addr(SERVERIP);    // Convert character string to dotted decimal format defined in <arpa/inet.h>
    if(connect(sockfd, (struct sockaddr*)serveraddr, addrlen)<0)
    {
        printf("Error: Connection failed... Exiting...\n");
        return 0;
    }else
        printf("Connection to server established successfully . . . \n");
  
    /* Send data to Server */
    printf("Enter Message to be sent ");
    scanf("%s",buff);
    send(sockfd, buff, strlen(buff), DEFAULT_FLAG);
  
    /* Read reply from server */
    n = recv(sockfd, buff, SIZE, DEFAULT_FLAG);
    buff[n] = '\0';
    printf("Message from Server: %s\n",buff);

    /* Close socket descriptor */
    close(sockfd);
    return 0;
}


Output

server window
Server waiting for connection request . . .
Connection Accepted
Message Received: Welcome

client window
Connection to server established successfully . . .
Enter Message to be sent Welcome
Message from Server: Thank You


Friday, November 22, 2013

Unix Network Programming: FTP using message queue

Algorithm

Create a structute that defines a message item in the message queue, say messageBuf

Client Side
  1. Get a message queue
  2. Read a filename from the standard input
  3. Set the message type as 1 and send the message
  4. Read messages of type 2 from the mesaage queue
  5. display the contents read
Server Side
  1. Get a message queue
  2. Read the filename from the meassage queue with type as 1
  3. Open requested file
  4. Read contents from the file and write into the message queue with type as 2
  5. Close the file

Program

message.h
 
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>

#define PERMS 0666
#define KEY1 2345
#define TYPE1 100
#define TYPE2 111
#define SIZE 20

struct messageBuf{
    int type;
    char msg[SIZE];
}

server
#include<stdio.h>
#include<malloc.h>
#include<fcntl.h>
#include<string.h>
#include "message.h"
main()
{
    int mq1,fd,n;
    struct messageBuf *m1 = (struct messageBuf *)malloc(sizeof(struct messageBuf));
    if(mq1 = msgget(KEY1,PERMS|IPC_CREAT)<0)
        printf("Error: Message Q unavaillable\n");
    msgrcv(mq1,m1,sizeof(struct messageBuf),TYPE1,0);
    printf("File Requested:%s\n",m1->msg);
    fd = open(m1->msg, O_RDONLY, 0);
    if(fd<0)
        printf("Error: Unable to open requested file\n");
    do
    {
        n = read(fd,m1->msg,SIZE);
        m1->type = TYPE2;
        msgsnd(mq1, m1, n, 0);
    }while(n==SIZE);
    close(fd);
}

client
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include "message.h"

main()
{
    int mq1,n;
    struct messageBuf *m1 = (struct messageBuf *)malloc(sizeof(struct messageBuf));
    if(mq1 = msgget(KEY1,PERMS|IPC_CREAT)<0)
        printf("Error: Message Q unavaillable\n");
    printf("Enter Required file name: ");
    scanf("%s",m1->msg);
    m1->type = TYPE1;
    msgsnd(mq1, m1, strlen(m1->msg),0);
    do{
        n = msgrcv(mq1,m1,SIZE,TYPE2,0);
        printf("%s",m1->msg);
    }while(n==SIZE);
}

Run the server and client from two different windows

Output

Client window

Enter Required file name: message.h
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>

#define PERMS 0666
#define KEY1 2345
#define TYPE1 100
#define TYPE2 111
#define SIZE 20

struct messageBuf{
    int type;
    char msg[SIZE];
}

Server window
File Requested:message.h