I'm testing a code to work as server for FGFS.  When I run FGFS  with 
--native=socket,out,30,linux,ftp  without starting server I get the expected 
message of no possible conection but when I start server before FGFS it runs 
ok. Thus I wonder server is connecting fine but  I don't get anything shown 
on screen. Would someone help me on that?

Thanks.

Sergio Roth 
-- 
Linux user #226380
SuSE Linux 7.2 
/* Program: Comunication test using socket
 (Adapted from GNU C Library Reference manual)
*/

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>


#define PORT 5500
#define MAXMSG 5092


int make_socket(unsigned short port)
{
int sock;
struct sockaddr_in name;
/* Create a soquete */
sock=socket(PF_INET, SOCK_STREAM, 0);
if(sock < 0)
  {
  perror("socket");
  exit(EXIT_FAILURE);
  }
name.sin_family=AF_INET;
name.sin_port=htons(port);
name.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sock, (struct sockaddr *)&name, sizeof(name))<0)
  {
  perror("bind");
  exit(EXIT_FAILURE);
  }
return sock;
}

int read_from_client(int filedes)
{
char buffer[MAXMSG];
int nbytes;
nbytes = read(filedes, buffer, MAXMSG-1);
if(nbytes<0)
  {
  perror("read");
  exit(EXIT_FAILURE);
  }
if(nbytes==0) return -1;
printf("Bytes received:\n%s\n", buffer);
return nbytes;
}


int write_to_client(int filedes, char *msg)
{
int nbytes;
nbytes=write(filedes, msg, strlen(msg)+1);
if(nbytes<0)
  {
  perror("read");
  exit(EXIT_FAILURE);
  }
return nbytes;
}

int main()
{
int sock, newsock, i;

struct sockaddr_in clientname;
fd_set active_fd_set, read_fd_set;
size_t size;

sock=make_socket(PORT);
if(listen(sock,1) < 0)
  {
  perror("listen");
  exit(EXIT_FAILURE);
  }
FD_ZERO(&active_fd_set);
FD_SET(sock, &active_fd_set);

do{
  read_fd_set=active_fd_set;
  if(select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL)<0)
    {
    perror("select");
    exit(EXIT_FAILURE);
    }
  for(i=0; i<FD_SETSIZE; i++)
    {
    if(!FD_ISSET(i, &read_fd_set)) continue;
    if(i==sock)
      {
      size=sizeof(clientname);
      newsock=accept(sock, (struct sockaddr *)&clientname, &size);
      if(newsock < 0)
	{
	perror("select");
	exit(EXIT_FAILURE);
	}
      fprintf(stderr, "Server: connect from host \"%s\", port %d\n",
		inet_ntoa(clientname.sin_addr), ntohs(clientname.sin_port));
      FD_SET(newsock, &active_fd_set);
      continue;
      }
    }

  read_from_client(i);

  }while(1);

}

Reply via email to