Question
hello my major is computer science. and i really need your help. i am network beginner. i had homework..but i don't know about sock programming.
hello my major is computer science. and i really need your help.
i am network beginner. i had homework..but i don't know about sock programming.
professor give me code that can using in linux server.c and client.c. home work is ressponse in http.
i have VM ubuntu linux system and in linux, browser is fire fox.
if i send request to server by browser head. i get file from server.
/* A simple server in the internet domain using TCP Usage:./server port (E.g. ./server 10000 ) */ #include #include // definitions of a number of data types used in socket.h and netinet/in.h #include // definitions of structures needed for sockets, e.g. sockaddr #include // constants and structures needed for internet domain addresses, e.g. sockaddr_in #include #include
void error(char *msg) { perror(msg); exit(1); }
int main(int argc, char *argv[]) { int sockfd, newsockfd; //descriptors rturn from socket and accept system calls int portno; // port number socklen_t clilen;
char buffer[256];
/*sockaddr_in: Structure Containing an Internet Address*/ struct sockaddr_in serv_addr, cli_addr;
int n; if (argc < 2) { fprintf(stderr,"ERROR, no port provided "); exit(1); }
/*Create a new socket AF_INET: Address Domain is Internet SOCK_STREAM: Socket Type is STREAM Socket */ 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]); //atoi converts from String to Integer serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; //for the server the IP address is always the address that the server is running on serv_addr.sin_port = htons(portno); //convert from host to network byte order
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) //Bind the socket to the server address error("ERROR on binding");
listen(sockfd,5); // Listen for socket connections. Backlog queue (connections to wait) is 5
clilen = sizeof(cli_addr); /*accept function: 1) Block until a new connection is established 2) the new socket descriptor will be used for subsequent communication with the newly connected client. */ newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); if (newsockfd < 0) error("ERROR on accept");
bzero(buffer,256); n = read(newsockfd,buffer,255); //Read is a block function. It will read at most 255 bytes if (n < 0) error("ERROR reading from socket"); printf("Here is the message: %s ",buffer);
n = write(newsockfd,"I got your message",18); //NOTE: write function returns the number of bytes actually sent out ?> // this might be less than the number you told it to send if (n < 0) error("ERROR writing to socket");
close(sockfd); close(newsockfd);
return 0; }
that is server.c
/* A simple client in the internet domain using TCP Usage: ./client server_hostname server_port (./client 192.168.0.151 10000) */ #include #include #include #include #include // define structures like hostent #include #include
void error(char *msg) { perror(msg); exit(0); }
int main(int argc, char *argv[]) { int sockfd; //Socket descriptor int portno, n; struct sockaddr_in serv_addr; struct hostent *server; //contains tons of information, including the server's IP address
char buffer[256]; if (argc < 3) { fprintf(stderr,"usage %s hostname port ", argv[0]); exit(0); } portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); //create a new socket if (sockfd < 0) error("ERROR opening socket"); server = gethostbyname(argv[1]); //takes a string like "www.yahoo.com", and returns a struct hostent which contains information, as IP address, address type, the length of the addresses... if (server == NULL) { fprintf(stderr,"ERROR, no such host "); exit(0); } bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; //initialize server's address 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) //establish a connection to the server error("ERROR connecting"); printf("Please enter the message: "); bzero(buffer,256); fgets(buffer,255,stdin); n = write(sockfd,buffer,strlen(buffer)); //write to the socket if (n < 0) error("ERROR writing to socket"); bzero(buffer,256); n = read(sockfd,buffer,255); //read from the socket if (n < 0) error("ERROR reading from socket"); printf("%s ",buffer); close(sockfd); //close socket return 0; }
that is client.c
i don't know about http request , response in client..
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started