KEMBAR78
Cne Lab 4 | PDF | Software Engineering | C++
0% found this document useful (0 votes)
68 views5 pages

Cne Lab 4

This C code is a client program that connects to a server, sends it two numbers and an operation choice, receives the result from the server, and repeats or exits. It includes header files for socket programming, initializes a socket, connects to the server, reads/writes data, and closes the socket before ending. The client gets input from the user, sends it to the server, receives the result back, and prints it, looping until the user chooses to exit.

Uploaded by

Roshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views5 pages

Cne Lab 4

This C code is a client program that connects to a server, sends it two numbers and an operation choice, receives the result from the server, and repeats or exits. It includes header files for socket programming, initializes a socket, connects to the server, reads/writes data, and closes the socket before ending. The client gets input from the user, sends it to the server, receives the result back, and prints it, looping until the user chooses to exit.

Uploaded by

Roshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

19MIA1001 CNE LAB 4(2)

ROSHAN SRINIVAAS.S

12/4/21
#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>

#include<ctype.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[1024];

if (argc < 3)

fprintf(stderr,"usage %s hostname port\n", argv[0]);

exit(0);

portno = atoi(argv[2]);

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0)

error("ERROR");

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;

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");
int number1 ;

int number2 ;

int answer;

int choice;

S:bzero(buffer,256);

n = read(sockfd,buffer,255); //Read Server String

if (n < 0)

error("ERROR reading from socket");

printf("Server - %s\n",buffer);

scanf("%d" , &number1); //Enter No 1

write(sockfd, &number1, sizeof(int)); //Send No 1 to Server

bzero(buffer,256);

n = read(sockfd,buffer,255); //Read Server String

if (n < 0)

error("ERROR reading from socket");

printf("Server - %s\n",buffer);

scanf("%d" , &number2); //Enter No 2

write(sockfd, &number2, sizeof(int)); //Send No 2 to Server


bzero(buffer,256);

n = read(sockfd,buffer,255); //Read Server String

if (n < 0)

error("ERROR reading from socket");

printf("Server - %s",buffer);

scanf("%d" , &choice); //Enter choice

write(sockfd, &choice, sizeof(int)); //Send choice to Server

if (choice == 5)

goto Q;

read(sockfd , &answer , sizeof(int)); //Read Answer from Server

printf("Server - The answer is : %d\n" , answer); //Get Answer from server

if(choice != 5)

goto S;

Q: printf("You chose to exit, Thank You so much.");

close(sockfd);

return 0;
}

You might also like