Code:
#include <sys/types.h> /* required include files */
#include <sys/socket.h>
#include <netinet/in.h> /* bind in internet domain */
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SOCKET_ERROR -1
#define STRGLEN 100 /* message string length */
const int APORT = 1234; /* chosen port number */
// FUNCTIONS
/* Function send_file
send the content of one file to the server */
void send_file(int s, char* namefile){
FILE* p;
int i;
char ch;
char length[100],text[1000];
p=fopen(namefile,"r");
i=0;
ch = getc(p); /* read 1st character in file */
while( ch != EOF){ /* keep going until end of file */
text[i]=ch;
ch = getc(p); /* read next character in in-file */
i++;
}
fclose(p);
text[i]='\0';
sprintf(length, "%d", i);
send(s, length, strlen(length), 0);
send(s, text, i, 0);
}
/* Function main */
main(int argc, char *argv[]){ /* argv[1] is server hostname argv[2] is port */
char string[STRGLEN]; /* message string */
register int s; /* created socket (index to unix descriptor table) */
struct hostent *hp; /* pointer to host structure */
struct sockaddr_in sin; /* socket structure */
float number;
int i; /* loop counter */
int selection; /* menu choice */
int num, server_port;
char c;
/* look up the network address of server hostname */
hp = gethostbyname(argv[1]);
if( hp==0 ){
fprintf(stderr, "unknown host %s\n", argv[1]);
exit(1);
}
/* create socket in the Internet domain(AF_INET) that will
be connection oriented SOCK_STREAM. (connectionless is
SOCK_DGRAM) 3rd argument indicated default TCP/IP protocol */
if( (s = socket(AF_INET, SOCK_STREAM, 0)) <0 ){
perror("client: socket");
exit(1);
}
/* Create the address we will be connecting to We use port
1234 but put it into network byte order.
Also we use bcopy to copy the network number */
sin.sin_family = AF_INET;
server_port=atoi(argv[2]);
sin.sin_port = htons(server_port);
bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
/* Try to connect to the address.
For this to succeed, the server must already have bound
this address, and must have issued a listen() request. */
if(connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0){
perror("client: connect");
exit(1);
}
//Save a file
printf("Enter file's name:\t");
scanf("%[^\n]", string);
printf("%s", string);
send(s, string, STRGLEN, 0);
//send_file(s, string);
/* close connection, since were finished on both sides */
close(s);
}
which works just fine
Code:
#include <sys/types.h> /* required include files */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#define SOCKET_ERROR -1
#define STRGLEN 100 /* message string length */
const int APORT = 1235; /* port number */
//DATA STRUCTURES
typedef struct{
char nameUser[100];
int numUser;
}user;
typedef struct userStruct{
user u;
struct userStruct* next;
}userknot;
typedef struct{
userknot* start;
userknot* end;
int totalUsers;
}userslist;
typedef struct{
char nameFile[100];
int numFile;
int numUser;
//time_t t;
}textFile;
typedef struct fileStruct{
textFile tf;
struct fileStruct* next;
}fileknot;
typedef struct{
fileknot* start;
fileknot* end;
int totalFiles;
}fileslist;
//FUNCTIONS
/* Function create_users_list
This function initializes the users list */
userslist* create_users_list(userslist* list){
if(list==NULL)return NULL;
list->start=list->end=NULL;
list->totalUsers=0;
return list;
}
/* Function create_files_list
This function initializes the files list */
fileslist* create_files_list(fileslist* list){
if(list==NULL)return NULL;
list->start=list->end=NULL;
list->totalFiles=0;
return list;
}
/* Function add_file
add a new file */
void add_file(char* name_file, int nFile, int nUser, fileslist* flist){
fileknot* fk;
fk=(fileknot*)malloc(sizeof(fileknot));
if(fk==NULL){
printf("Can't add a new file");
exit(-1);
}
else{
strcpy(fk->tf.nameFile, name_file);
fk->tf.numFile=nFile;
fk->tf.numUser=nUser;
if(flist->totalFiles==0){
flist->end=flist->start=fk;
}
else{
flist->end->next=fk;
flist->end=fk;
}
flist->end->next=NULL;
flist->totalFiles++;
}
}
/* Function get_savings
This function gets back the data that has been saved previously */
void get_savings(FILE* p, userslist* ul, fileslist* fl)
{
userknot* uk;
fileknot* fk;
int i, length;
char string[100];
fgets(string, STRGLEN, p);
sscanf(string, "%d", &(ul->totalUsers)); //read the total number of users
uk=(userknot*)malloc(sizeof(userknot));
if(uk==NULL){
printf("Couldn't allocate a userknot\n");
exit(-1);
}
if(ul->totalUsers!=0){
ul->start=uk;
fgets(string, STRGLEN, p); //read the name of the user
sscanf(string, "%[^\n]", uk->u.nameUser);
length=strlen(string);
uk->u.nameUser[length-1]='\0';
fgets(string, STRGLEN, p); //read the number of the user
sscanf(string, "%d", &(uk->u.numUser));
i=1;
while(i<ul->totalUsers){
uk->next=(userknot*)malloc(sizeof(userknot));
uk=uk->next;
fgets(string, STRGLEN, p); //read the name of the user
sscanf(string, "%[^\n]", uk->u.nameUser);
length=strlen(string);
uk->u.nameUser[length-1]='\0';
fgets(string, STRGLEN, p); //read the number of the user
sscanf(string, "%d", &(uk->u.numUser));
i++;
}
uk->next=NULL;
ul->end=uk;
}
fgets(string, STRGLEN, p);
sscanf(string, "%d", &(fl->totalFiles)); //read the total number of users
fk=(fileknot*)malloc(sizeof(fileknot));
if(fk==NULL){
printf("Couldn't allocate a userknot\n");
exit(-1);
}
if(fl->totalFiles!=0){
fl->start=fk;
fgets(string, STRGLEN, p); //read the name of the file
sscanf(string, "%[^\n]", fk->tf.nameFile);
length=strlen(string);
fk->tf.nameFile[length-1]='\0';
fgets(string, STRGLEN, p); //read the number of the file
sscanf(string, "%d", &(fk->tf.numFile));
fgets(string, STRGLEN, p); //read the number of the user
sscanf(string, "%d", &(fk->tf.numUser));
i=1;
while(i<fl->totalFiles){
fk->next=(fileknot*)malloc(sizeof(fileknot));
fk=fk->next;
fgets(string, STRGLEN, p); //read the name of the file
sscanf(string, "%[^\n]", fk->tf.nameFile);
length=strlen(string);
fk->tf.nameFile[length-1]='\0';
fgets(string, STRGLEN, p); //read the number of the file
sscanf(string, "%d", &(fk->tf.numFile));
fgets(string, STRGLEN, p); //read the number of the user
sscanf(string, "%d", &(fk->tf.numUser));
i++;
}
fk->next=NULL;
fl->end=fk;
}
}
/* Function print_all_files
this function prints the list of all the files of one user */
void print_all_files(int num_user, fileslist* fl){
fileknot* fk;
char string[STRGLEN];
int num, n=0, go_on, nSentFiles=0;
fk=(fileknot*)malloc(sizeof(fileknot));
num=fl->totalFiles;
sprintf(string, "%d", num);
printf("nb of files: %d\n", num); /* send the total number of files */
if(num!=0){
fk=fl->start;
while(n<num){
if(fk->tf.numUser==num_user){
strcpy(string, fk->tf.nameFile);
printf("%s\n", string);
nSentFiles++;
}
fk=fk->next;
n++;
}
printf("nSentfiles: %d\n", nSentFiles);
}
}
/* Function save_file
receive the content of one file from the client, encode it and save
it on the server */
void save_file(int s, char* namefile){
int max, total = 0, i;
const int bsize = 1024;
char c, length[bsize+1], buffer[bsize+1],text[bsize+1];
int read = bsize;
FILE* p;
length[0]=0; //initialize length
recv(s, length, bsize, 0);
sscanf(length, "%d", &max);
text[0] = 0; // initialize for strcat()
while(read == bsize){
read = recv(s, buffer, bsize, 0);
if(read < 0){
printf("Server: Error while receiving file\n"); // error, bail out
exit(-1);
}
total += read;
if(total > max){
printf("Error: overflow\n"); // overflowv
exit(-1);
}
buffer[read] = 0;
strcat(text, buffer); /* in case the string is too long and need more than one read */
}
total=strlen(text);
printf("debug: %s\n", text);
i=0;
p=fopen(namefile, "w");
while(i<total-1){
c=(char)(text[i]+5);
putc(c, p);
i++;
}
fclose(p);
}
/* Function main */
main(void){
char string[STRGLEN], name[STRGLEN], name_file[STRGLEN];
int fromlen;
char hostname[64]; /* local machine hostname */
struct hostent *hp; /* gethostbyname return ptr */
register int s; /* socket descriptor, or -1 if error */
register int ns; /* created new socket for accept */
struct sockaddr_in sin; /* socket structure */
struct sockaddr_in fsin; /* socket str. for accept*/
float number=0.0;
int i; /* loop counter */
int selection; /* menu choice */
int num_user, num_file;
userslist* ulist;
fileslist* flist;
FILE* p;
char* text;
text=(char*)malloc(1000*sizeof(char));
ulist=(userslist*)malloc(sizeof(userslist));
flist=(fileslist*)malloc(sizeof(fileslist));
/* first need to know our hostname. */
gethostname(hostname, sizeof(hostname));
/* Next look up the network address of our host.*/
if((hp = gethostbyname(hostname)) == NULL){
fprintf(stderr, "%s: unknown host\n", hostname);
exit(-1);
}
/* create socket in Internet domain(AF_INET), that is
connection oriented (SOCK_STREAM) rather than
connectionless (SOCK_DGRAM),
arg3=0 for default TCP/IP protocol*/
if(( s = socket(AF_INET, SOCK_STREAM, 0) ) < 0){
perror("server: socket");
exit(-1);
}
/* Create the address to connecting to. We use port
APORT but put it into network byte order.
Also we use bcopy to copy the network number. */
bzero(&sin, sizeof(sin));
sin.sin_family = AF_INET; /* IP protocol */
/* sin.sin_addr.s_addr = INADDR_ANY;*/
sin.sin_port = htons(APORT);
bcopy(hp->h_addr, &sin.sin_addr, hp->h_length);
/* Try to bind(address and port no.)to socket s */
if(bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0){
perror("server: bind");
exit(-1);
}
/* server ready, listen on socket s max 5 requests queued*/
if(listen(s, 5) < 0) {
perror("server: listen");
exit(-1);
}
/* Accept connections then fork ns (new socket) that
will be connected to the client. fsin will contain
the IP address of the client. */
bzero(&fsin, sizeof(fsin));
fromlen = sizeof(fsin);
if((ns = accept(s, (struct sockaddr *)&fsin, &fromlen))<0){
perror("server: accept");
exit(-1);
}
ulist=create_users_list(ulist); //Initialization of the users list
if(ulist==NULL){
printf("Impossible to allocate a users list");
exit(-1); //Exit the program
}
flist=create_files_list(flist); //Initialization of the files list
if(flist==NULL){
printf("Impossible to allocate a files list");
exit(-1); //Exit the program
}
/* get save file */
p=fopen("save.txt","r");
if(p!=NULL){
get_savings(p, ulist, flist);
fclose(p);
}
recv(ns, string, STRGLEN, 0); //read name of the file
strcpy(name_file, string);
num_file=flist->totalFiles+1;
num_user=1;
add_file(name_file, num_file, num_user, flist);
print_all_files(num_user, flist);
//save_file(ns, name_file);
/* We can simply use close() to terminate the
connection, since we're done with both sides */
close(s);
}
even if I put exit() instead of close, my program still freezes after the print_all_files. Well not freezes maybe, but I don't have the command prompter as I should.