Anyway. I have made a very simple ftp program... excedingly simple in
fact...
Here's the server code:
/* server.c */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/signal.h>
#include <errno.h>
void main(argc, argv)
int argc;
char *argv[];
{
int sock,msgsock,i;
struct sockaddr_in server;
char *buffer;
char filename[100];
FILE *fin;
struct stat stat;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("opening stream socket");
exit(1);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = atoi(argv[1]);
if (bind(sock, (struct sockaddr *) &server, sizeof(server))) {
perror("binding stream socket");
exit(1);
}
while (1) {
listen(sock,1);
msgsock = accept(sock,0,0);
if (msgsock == -1)
if (errno != EINTR) perror("accept");
read(msgsock,filename,sizeof(filename));
printf("Client requested '%s'\n",filename);
fin=fopen(filename,"rt");
if (fin==NULL)
{
i=0;
printf("Error opening file\n");
write(msgsock,&i,sizeof(off_t));
}
else
{
// fstat(fin->_file,&stat); // sunos
fstat(fin->_fileno,&stat); // linux
printf("File is %d bytes.. \n",stat.st_size);
write(msgsock,&(stat.st_size),sizeof(off_t));
printf("Reading file....\n");
buffer=(void *)malloc(stat.st_size);
fread(buffer,stat.st_size,1,fin);
fclose(fin);
printf("Sending File...\n");
write(msgsock,buffer,stat.st_size);
sleep(1);
printf("Done. Click.\n");
close(msgsock);
free(buffer);
}
}
}
and heres my client:
/* client .c */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
main(argc, argv)
int argc;
char *argv[];
{
int sock,flen;
struct sockaddr_in client;
struct hostent *hp, *gethostbyname();
char *buffer;
char filename[100];
FILE *fout;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0){
perror("opening stream socket\n");
exit(1);
}
client.sin_family = AF_INET;
hp = gethostbyname(argv[1]);
if (hp == 0){
fprintf(stderr,"%s: unknown host",argv[1]);
exit(2);
}
bcopy(hp->h_addr, &client.sin_addr, hp->h_length);
client.sin_port = atoi(argv[2]);
if (connect(sock, (struct sockaddr *) &client, sizeof(client)) < 0){
perror("connecting stream socket");
exit(1);
}
printf("What file should I get from the server?\n");
gets(filename);
write(sock,filename,sizeof(filename));
read(sock,&flen,sizeof(int));
if(flen==0)
printf("Error on server side... File not found or 0 byte file?\n");
else
{
printf("File is %d bytes\n",flen);
buffer=malloc(flen);
printf("Reveiving file\n");
read(sock,buffer,flen);
printf("What file should I save the data in?\n");
gets(filename);
fout = fopen(filename,"wt");
printf("Saving file\n");
fwrite(buffer,flen,1,fout);
free(buffer);
fclose(fout);
close(sock);
}
}
The problem is that this works on sunos just fine... but when I port it to
linux, only the first part comes therough. Aproximately a pagefull... the
rest is lost somwhere....
Why isnt the data coming over my socket? is the linux socket limited as to
how much you can write at a time or somthingt?
-Chuck
s253343@gettysburg.edu
(717)-337-8212
"God is real, unless declared integer."