ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
When I run the below code, I can't figure out why I am getting hung when the server tries to accept the client. I am using Firefox as the client. I just want to read the Firefox header, and write back my HTML to the socket.
Code:
/* Library Section */
#include <stdio.h> /* Standard Input and Output */
#include <unistd.h> /* Unix Standard Functions */
#include <string.h> /* Used for bzero */
#include <math.h> /* Math Library used for log10 */
#include <sys/types.h> /* Used for System Data Type Defitions */
#include <sys/socket.h> /* Used for sockets */
#include <netinet/in.h> /* sockaddr_in definition */
#include <arpa/inet.h> /* Internet Sockets */
#include <stdlib.h>
/* Definitions */
#define SERV_TCP_PORT 10617 /* Port Number */
#define SERV_HOST_ADDR "198.102.147.137" /* IP address for Gimli */
#define BUFFSIZE 2048 /* Buffer Size */
int main() {
int sockfd; /* Initial socket descriptor */
int newsockfd; /* Client/Server socket descriptor */
socklen_t client_len; /* Client Socket File Length */
struct sockaddr_in serv_addr; /* Server Address Information */
struct sockaddr_in cli_addr; /* Client Address Information */
char fire_head[2048]; /* header from Firefox */
char g_file[2048]; /* HTML to send as response to client */
/* Open a TCP socket (an Internet stream socket). */
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)/* creating internet socket */
fprintf(stderr, "server: can't open stream socket\n");
/* Bind our local address so that the client can send to us. */
/* First, clear out the structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(SERV_TCP_PORT);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
fprintf(stderr, "server: can't bind local address\n");
/* Now, listen for incomming connections. */
listen(sockfd, 5);
printf("The server is now listening for connections\n");
bzero(fire_head, BUFFSIZE);
while (1) { /* loop forever */
client_len = sizeof(cli_addr);
/* Accept the client connection */
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &client_len);
if (newsockfd < 0)
fprintf(stderr, "server: accept error\n");
/* Read header from Firefox from the socket */
read(newsockfd, fire_head, BUFFSIZE);
printf("%s", fire_head);
/* Build g_file webpage */
strcat(g_file, "HTTP/1.1 200 OK\n");
strcat(g_file, "Content-Type: text/html\n");
strcat(g_file, "<HTML><HEAD><TITLE>PROGRAMMING EXERCISE #2</TITLE></HEAD><BODY>\n");
strcat(g_file, "<UL>\n");
strcat(g_file, "<LI>ITEM NUMBER ONE</LI>\n");
strcat(g_file, "<LI>TWO IS RIGHT HERE</LI>\n");
strcat(g_file, "<LI>THREE IS TAKING A BREAK, I AM FOUR</LI>\n");
strcat(g_file, "<LI>OKAY I AM THE LAST ONE, I AM NUMBER FIVE</LI>\n");
strcat(g_file, "</UL>\n");
strcat(g_file, "<A HREF = 'http://morningside.edu'>Morningside College Homepage</A>\n");
strcat(g_file, "</BODY></HTML>\n");
/* Write webpage socket */
write(newsockfd, g_file, strlen(g_file));
close(newsockfd); /* We are finished with the socket */
close(sockfd); /* We are finished with the socket */
bzero(fire_head, BUFFSIZE);
bzero(g_file, BUFFSIZE);
} /* END WHILE */
return (0);
}
Well you are closeing you listening socket (sockfd) after you write out the data, and then you are looping back around and trying to accept again on an invalid sock. So i would suggest either remove the while loop or move the close of the sockfd outside the while loop so when you loop back around to the accept again you are trying it on a valid socket.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.