LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2006, 04:11 PM   #1
calorie712
LQ Newbie
 
Registered: Feb 2006
Posts: 26

Rep: Reputation: 15
server accept error


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);
   
}
 
Old 03-17-2006, 06:41 AM   #2
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
coding server to accept a client on any port calorie712 Programming 12 02-11-2006 06:50 PM
Accept error in kernel2.4.19 D. Marowa Linux - General 2 05-10-2004 12:42 AM
firewall error? couldn't load target "accept ...ipt_accept? error piratebiter Linux - Security 1 09-19-2003 05:37 PM
How to set SSH server to accept V2 connections only? chupacabra Linux - Security 18 01-24-2003 04:57 PM
my edonkey server can only accept 1015 connections Vaevictus Linux - Networking 1 11-16-2002 11:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration