Your IP : 216.73.216.40


Current Path : /var/www/html/vijayk/programs/
Upload File :
Current File : /var/www/html/vijayk/programs/confileserver.c

 
#include <stdio.h>
#include<strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#define SERVER_PORT  5432
#define MAX_PENDING  5
#define MAX_LINE     256


int main()
{
  
	struct sockaddr_in sin;
  char buf[MAX_LINE];
  int len;
  int s, new_s,child;
  int yes=1;
  FILE* fp;
  /* build address data structure */
  memset((void *)&sin,0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr= INADDR_ANY;
  sin.sin_port = htons(SERVER_PORT);
  /* setup passive open */
  if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    perror("simplex-talk: socket");
    return(1);
  }
  
  //if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes,sizeof(int)) == -1)
 // {
//    perror("setsockopt");
 //   return(1);
 // }

  
  if ((bind(s, (struct sockaddr *)&sin, sizeof(sin))) < 0) {
    perror("simplex-talk: bind");
    return(1);
  }

  listen(s, MAX_PENDING);
  /* wait for connection, then receive and print text */
 
  for(;;)
  {
    if ((new_s = accept(s, (struct sockaddr *)&sin, &len)) < 0)
    {
     perror("simplex-talk: accept");
     close(new_s);
     close(s);
     return(1);
    }
    
    if (fork()== 0)
    {
	close(s);
	printf("\n Client Connected\n");
	fp = fopen("server.txt","r");
	while(!feof(fp))
	{
		//bzero(buf,sizeof(buf));
		fread(buf,sizeof(char),MAX_LINE,fp);
		buf[strlen(buf)] = '\0';
		write(new_s,buf,MAX_LINE);
		strcpy(buf,"");
		buf[MAX_LINE-1] = '\0';
	}
	
	write(new_s,"bye",4);
	fclose(fp);
	close(new_s);
    }
    
	/*else if (child == -1)
    {
      perror("simplex-talk: no child");
      close(new_s);
      close(s);
     return(1);
    }	*/
    
	close(new_s);
 	 }
  
  close(s);
	
	return 1;
}