Part B-3

 

Program 3B:

 

CLIENT:

 

#include <stdio.h>

 

#include <sys/socket.h>

 

#include <netinet/in.h>

 

#include <string.h>

 

int main()

 

{

 

        int clientSocket, portNum, nBytes;

 

        char buffer[1024];

 

        struct sockaddr_in serverAddr;

 

        socklen_t addr_size;

 

        clientSocket = socket(PF_INET, SOCK_STREAM, 0);

 

        portNum = 7891;

 

        serverAddr.sin_family = AF_INET;

 

        serverAddr.sin_port = htons(portNum);

 

        serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

 

        memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

 

        addr_size = sizeof serverAddr;

 

        connect(clientSocket, (struct sockaddr *) &serverAddr,addr_size);

 

        while(1)

         {

 

                        printf("1. Sandwich\n");

 

                        printf("2. Ice Cream\n");

 

                        printf("3. Chocolate\n");

 

                        printf("4. Coffee\n");

                printf("Enter choice to send to server:\n");

 

                fgets(buffer,1024,stdin);

 

                printf("Choice typed is: %s",buffer);

 

                nBytes = strlen(buffer) + 1;

 

                send(clientSocket,buffer,nBytes,0);

 

                recv(clientSocket, buffer, 1024, 0);

 

                printf("Received price for item from server is: %s\n",buffer);

         }

 

        return 0;

 

}

 

SERVER:

 

#include <stdio.h>

 

#include <sys/socket.h>

 

#include <netinet/in.h>

 

#include <string.h>

 

#include <stdlib.h>

 

int main()

 

{

 

        int welcomeSocket, newSocket, portNum, clientLen, nBytes;

 

            char price[50];

 

            char buffer[1024];

 

        struct sockaddr_in serverAddr;

 

        struct sockaddr_storage serverStorage;

 

        socklen_t addr_size;

 

        int i;

 

        welcomeSocket = socket(PF_INET, SOCK_STREAM, 0);         portNum = 7891;

 

        serverAddr.sin_family = AF_INET;

 

        serverAddr.sin_port = htons(portNum);

 

        serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

 

        memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

 

        bind(welcomeSocket, (struct sockaddr *) &serverAddr,sizeof(serverAddr));

 

        if(listen(welcomeSocket,5)==0)

 

        printf("Server Listening\n");

 

        else

 

        printf("Error\n");

 

        addr_size = sizeof serverStorage;

 

        while(1)

         {

 

                newSocket = accept(welcomeSocket, (struct sockaddr  *)&serverStorage,&addr_size);

                 if(!fork())                  {

                         nBytes = 1;

 

                        while(nBytes!=0)

                         {

 

                                nBytes = recv(newSocket,buffer,1024,0);

 

                                                printf(" \nRecieved Choice is %s",buffer);

 

                                                if(buffer[0]=='1')

 

                                                {

 

                                                            strcpy(price,"100.00");

 

                                                }

 

                                                else if(buffer[0]=='2')

                                                {

 

                                                            strcpy(price,"80.00");

 

                                                }

 

                                                else if(buffer[0]=='3')

 

                                                {

 

                                                            strcpy(price,"36.00");

 

                                                }

 

                                                else if(buffer[0]=='4')

 

                                                {

 

                                                            strcpy(price,"12.00");

 

                                                }

 

                                                else

 

                                                {

 

                                                            strcpy(price,"Wrong Choice");

 

                                                }

 

                                     

 

                                                send(newSocket,price,50,0);

 

                                     

                         }

 

                        close(newSocket);

                         exit(0);                  }

 

 

 

            }

 

}

 

Comments