NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#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 portn", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such hostn");
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 num1 ;
int num2 ;
int ans;
int choice;



S:bzero(buffer,256);
n = read(sockfd,buffer,255); //Read Server String
if (n < 0)
error("ERROR reading from socket");
printf("Server - %sn",buffer);
scanf("%d" , &num1); //Enter No 1
write(sockfd, &num1, 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 - %sn",buffer);
scanf("%d" , &num2); //Enter No 2
write(sockfd, &num2, 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 , &ans , sizeof(int)); //Read Answer from Server
printf("Server - The answer is : %dn" , ans); //Get Answer from server

if(choice != 5)
goto S;

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




close(sockfd);
return 0;
}
/*
gcc Server.c -o Server
./Server 4562
gcc client.c -o client
./client 192.168.244.139 4562
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <ctype.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[1024];
struct sockaddr_in serv_addr, cli_addr;
int n;
if (argc < 2) {
fprintf(stderr,"ERROR, no port providedn");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
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);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");



int num1 , num2 , ans , choice;
S: n = write(newsockfd,"Enter Number 1 : ",strlen("Enter Number 1")); //Ask for Number 1
if (n < 0) error("ERROR writing to socket");
read(newsockfd, &num1, sizeof(int)); //Read No 1
printf("Client - Number 1 is : %dn" , num1);


n = write(newsockfd,"Enter Number 2 : ",strlen("Enter Number 2")); //Ask for Number 2
if (n < 0) error("ERROR writing to socket");
read(newsockfd, &num2, sizeof(int)); //Read Number 2
printf("Client - Number 2 is : %dn" , num2);


n = write(newsockfd,"Enter your choice : n1.Additionn2.Subtractionn3.Multiplicationn4.Divisionn5.Exitn",strlen("Enter your choice : n1.Additionn2.Subtractionn3.Multiplicationn4.Divisionn5.Exitn")); //Ask for choice
if (n < 0) error("ERROR writing to socket");
read(newsockfd, &choice, sizeof(int)); //Read choice
printf("Client - Choice is : %dn" , choice);


switch(choice)
{
case 1:
ans = num1 + num2;
break;
case 2:
ans = num1 -num2;
break;
case 3:
ans = num1*num2;
break;
case 4:
ans = num1/num2;
break;
case 5 :
goto Q;
break;
}

write(newsockfd , &ans , sizeof(int));
if(choice != 5)
goto S;

Q: close(newsockfd);
close(sockfd);
return 0;
}
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.