cannot write file, cannot do variable length packet

M

Marcia Hon

Hi,

Here is my code. In clientFileList3.c, function create_List_of_Files, I
cannot create a file. It was working at one point but now it does not work.
Also, I would like to send variable length packets, but do not know how to
accomplish this.

clientFileList3.c
#include <stdio.h>

#include <stdlib.h>

#include <sys/socket.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <netdb.h>

#include <fcntl.h>

#include <unistd.h>

#include <sys/dir.h>

#include <sys/param.h>

#include <sys/stat.h>

#include <time.h>

#include <string.h>

#define BUFFER_MAX 512 //Max buffer size

#define MESSAGE_BODY 507 //Max Message body

#define ACK_ATTEMPTS 3 //Acknowledge attempts

#define ONE_SECOND 10000 //For loop for 1 second

// Packet Types

#define RQPG 10 //Request to join Peer Group

#define PGA 11 //Peer Group Acknowledge

#define SFL 12 //Shared File List

#define FLA 13 //File List Acknowledge

// Message Start Flags

#define MESST 60 //Start of Message

#define MESCON 61 //Continued from previous message

// Message End Flags

#define MEND 70 //End of Message

#define MCNTO 71 //Continue to next packet

#define bool int

#define false 0

#define true 1

char SharedDir[MAXPATHLEN];

typedef struct packet_header

{

unsigned size:16;

unsigned type:8;

unsigned message_start_flag:8;

unsigned message_end_flag:8;

} PACKET_HEADER;

int establish_connection(const char * server, unsigned short port);

int request_to_join_peer_group(const int ServerSocket, unsigned short
PeerPort);

int create_List_of_Files(FILE * List_of_Files, char * List, char * subdir,
FILE * Log, char * log_file);

int create_files(char * SharedDir);

int file_select();

int send_List_of_Files(FILE * List_of_Files, char * List, const int
ServerSocket);

unsigned long getFileSize(FILE * List_of_Files, char * List);

int main(int argc, char *argv[])

{

char * Server;

unsigned short ServerPort, PeerPort;

int ServerSocket;

FILE * List_of_Files;

int test;

char * List = "List_of_Files";

char * subdir;

char * log_file;

FILE * Log;


//Check if 3 argments passed

if (argc != 6)

{

fprintf(stderr, "Usage: %s <ser_ip> <ser_port> <cli_port> <subdir>
<log_file>\n", argv[0]);

exit(1);

}


//Assign arguments to variables

Server = argv[1];

ServerPort = atoi(argv[2]);

PeerPort = atoi(argv[3]);

subdir = argv[4];

log_file = argv[5];


if((ServerSocket = establish_connection(Server, ServerPort)) == -1)

{

fprintf(stderr, "Error establishing connection\n");

exit(1);

}

if(create_List_of_Files(List_of_Files, List, subdir, Log, log_file) == -1)

{

fprintf(stderr, "Error cannot create List_of_Files\n");

exit(1);

}

if(request_to_join_peer_group(ServerSocket, PeerPort) == -1)

{

fprintf(stderr, "Error cannot join Peer Group\n");

exit(1);

}

else

printf("Joined Peer Group\n");

if(send_List_of_Files(List_of_Files, List, ServerSocket) == -1)

{

fprintf(stderr, "Error cannot upload List_of_Files\n");

exit(1);

}

else

printf("List_of_Files uploaded. Ready for user.\n");

return 0;

}

int establish_connection (const char * server, unsigned short port)

{

struct sockaddr_in server_addr;

int ServerSocket;

struct hostent *h;


ServerSocket = socket(AF_INET, SOCK_STREAM, 0);


//Set up the structure information

server_addr.sin_family = AF_INET;

server_addr.sin_port = htons(port);

memset(&(server_addr.sin_zero), '\0', 8);


if((h = gethostbyname(server)) == NULL)

{

fprintf(stderr, "Erro getting host %s\n", server);

return -1;

}


server_addr.sin_addr = *((struct in_addr *)h->h_addr);


printf("IP Address: %s\n", inet_ntoa(*((struct in_addr *)h->h_addr)));

printf("IP Address: %s\n", inet_ntoa(server_addr.sin_addr));

printf("Port: %d\n", ntohs(server_addr.sin_port));


if((connect(ServerSocket, (struct sockaddr *) &server_addr,

sizeof(struct sockaddr))) == -1)

{

fprintf(stderr, "Error: Cannot connect\n");

return -1;

}


return ServerSocket;

}

int request_to_join_peer_group(const int ServerSocket, unsigned short
PeerPort)

{

PACKET_HEADER RequestPeerGroup;

int bytes_sent = 0;

unsigned * sbuffer;

int wait;

int packetsSent = 0;

int n;


PACKET_HEADER AckPeerGroup;

int bytes_received = 0;

unsigned * rbuffer;

unsigned * body;

unsigned block;

body = &block;


//Allocate memory for the sending buffer

if ((sbuffer = (unsigned *) malloc (BUFFER_MAX)) == NULL)

{

fprintf(stderr, "Not enough memory for a buffer size of %d \n", BUFFER_MAX);

return -1;

}


RequestPeerGroup.size = sizeof(PeerPort) + 5;

RequestPeerGroup.type = RQPG;

RequestPeerGroup.message_start_flag = MESST;

RequestPeerGroup.message_end_flag = MEND;


*sbuffer = RequestPeerGroup.size;

*(sbuffer + 2) = RequestPeerGroup.type;

*(sbuffer + 3) = RequestPeerGroup.message_start_flag;

*(sbuffer + 4) = PeerPort;

*(sbuffer + sizeof(*(sbuffer +4)) +4 - 1) =
RequestPeerGroup.message_end_flag;


//Allocate memory for the receiving buffer

if ((rbuffer = (unsigned *) malloc (BUFFER_MAX)) == NULL)

{

fprintf(stderr, "Not enough memory for a buffer size of %d \n", BUFFER_MAX);

return -1;

}


AckPeerGroup.type = 0;


printf("%x %x %x %x %x\n", *sbuffer, *(sbuffer +2), *(sbuffer +3),
*(sbuffer+4), *(sbuffer + sizeof(*(sbuffer+4)) + 4 - 1));


while((packetsSent < ACK_ATTEMPTS) && (AckPeerGroup.type != PGA))

{

bytes_sent += send(ServerSocket, sbuffer, *sbuffer, 0);

printf("Request to Join Peer Group: %d bytes sent\n", bytes_sent);


if((bytes_received += read(ServerSocket, rbuffer, 2)) > 0)

{

AckPeerGroup.size = *rbuffer;


if((n = read(ServerSocket, (rbuffer+2), (AckPeerGroup.size-2))) > 0)

{

bytes_received += n;

AckPeerGroup.type = *(rbuffer + 2);

AckPeerGroup.message_start_flag = *(rbuffer + 3);

*body = *(rbuffer + 4);

AckPeerGroup.message_end_flag = *(rbuffer + sizeof(*body) + 4 - 1);


printf("Peer Group Acknowledge: %d bytes received\n", bytes_received);

}

}


for(wait = 0; wait < ONE_SECOND; wait++);

packetsSent++;

printf("Packets Sent: %d\n", packetsSent);

}

printf("%x %x %x %x %x\n", *rbuffer, *(rbuffer +2), *(rbuffer +3),
*(rbuffer+4), *(rbuffer + sizeof(*(rbuffer+4)) + 4 - 1));


free(rbuffer);

free(sbuffer);


if(AckPeerGroup.type != PGA)

return -1;

else

return 0;

}

int create_List_of_Files(FILE * List_of_Files, char * List, char * subdir,
FILE * Log, char * log_file)

{

struct direct ** files;

int i;

int count;

char * commandformat = "rm -rf '%s'";

char * command = malloc(strlen(subdir) + strlen(commandformat)+1);

sprintf(command, commandformat, subdir);

time_t now;

time(&now);

if(system("cd ~/ECE361/project1/shared/") != 0)

{

if(system("cd; mkdir ECE361; mkdir ECE361/project1; mkdir
ECE361/project1/shared") != 0)

{

fprintf(stderr, "Error: Cannot make directory ~/ECE361/project1/shared\n",
subdir);

return -1;

}

else

printf("~/ECE361/project1/shared/%s created\n", subdir);

}

if(chdir((char *)getenv("HOME")) != 0)

{

fprintf(stderr, "Error: Cannot change to home directory\n");

return -1;

}

if(chdir("ECE361/project1/shared") != 0)

{

fprintf(stderr, "Error: Cannot change to ~/ECE361/project1/shared\n");

return -1;

}

else

{

if(system(command) != 0)

{

if(mkdir(subdir, S_IRWXU) != 0)

{

fprintf(stderr, "Error: make directory ~/ECE361/project1/shared/%s\n",
subdir);

return -1;

}

if(chdir(subdir) != 0)

{

fprintf(stderr, "Error: cannot change to ~/ECE361/project1/shared/%s\n",
subdir);

return -1;

}

}

else

{

if(mkdir(subdir, S_IRWXU) != 0)

{

fprintf(stderr, "Error: make directory ~/ECE361/project1/shared/%s\n",
subdir);

return -1;

}

if(chdir(subdir) != 0)

{

fprintf(stderr, "Error: cannot change to ~/ECE361/project1/shared/%s\n",
subdir);

return -1;

}

}

}

if(getwd(SharedDir) == NULL)

{

fprintf(stderr, "Error: Cannot get working directory\n");

return -1;

}

if((Log = fopen(log_file, "a")) == NULL)

{

fprintf(stderr, "Error: Cannot open file %s\n", log_file);

return -1;

}

else

{

fprintf(Log, "%.24s\n", ctime(&now));

}

if(create_files(SharedDir) == -1)

{

fprintf(stderr, "Error: Cannot create files\n");

return -1;

}

if((List_of_Files = fopen(List, "w")) == NULL)

{

fprintf(stderr, "Error: Cannot create and open %s\n", List);

return -1;

}

count = scandir(SharedDir, &files, file_select, alphasort);

for(i = 0; i < count; i++)

{

fprintf(List_of_Files, "%s\n", files->d_name);

}

fclose(List_of_Files);

return 0;

}

int file_select(struct direct *entry)

{

struct stat st;

if((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0))

return 0;

else

{

if( 0 > lstat(entry->d_name, &st))

{

return 0;

}

if(S_ISREG(st.st_mode))

return 1;

else

return 0;

}

}

int create_files(char * SharedDir)

{

FILE * passwd;

if(chdir("/etc") != 0)

{

fprintf(stderr, "Error: Cannot open /etc\n");

return -1;

}

if((passwd = fopen("passwd", "r")) == NULL)

{

fprintf(stderr, "Error: Cannot read file /etc/passwd\n");

Canntuc return -1;

}


return 0;

}

int send_List_of_Files(FILE * List_of_Files, char * List, const int
ServerSocket)

{

PACKET_HEADER SendFileList;

int bytes_sent = 0;

unsigned * sbuffer;

int bodysize = 0;

bool start;

bool end;

int i;

int j;

int wait;

int packetsSent = 0;

int n;

PACKET_HEADER AckFileList;

int bytes_received = 0;

unsigned * rbuffer;

unsigned * body;

unsigned block;

body = &block;


//Allocate memory for sending buffer

if((sbuffer = (unsigned *) malloc (BUFFER_MAX)) == NULL)

{

fprintf(stderr, "Not enough memory for a buffer size of %d \n", BUFFER_MAX);

return -1;

}

if((rbuffer = (unsigned *) malloc (BUFFER_MAX)) == NULL)

{

fprintf(stderr, "Not enough memory for a buffer size of %d \n", BUFFER_MAX);

return -1;

}

if((bodysize = getFileSize(List_of_Files, List)) == -1)

{

fprintf(stderr, "Error: Cannot get List_of_Files size\n");

return -1;

}

AckFileList.type = 0;

while((packetsSent < ACK_ATTEMPTS) && (AckFileList.type != FLA))

{

if((List_of_Files = fopen(List, "r")) == NULL)

{

fprintf(stderr, "Cannot open file.\n");

return -1;

}

start = true;

end = false;

SendFileList.type = SFL;

while(!feof(List_of_Files))

{

*(sbuffer + 2) = SendFileList.type;

if(start)

{

SendFileList.message_start_flag = MESST;

start = false;

}

else

SendFileList.message_start_flag = MESCON;

*(sbuffer + 3) = SendFileList.message_start_flag;

for(i = 0; i < MESSAGE_BODY; i++)

{

*(sbuffer + 4 + i) = fgetc(List_of_Files);

if(feof(List_of_Files))

{

end = true;

SendFileList.message_end_flag = MEND;

*(sbuffer + 4 + (i+1) + 4 -1) = SendFileList.message_end_flag;

break;

}

}

SendFileList.size = i + 5;

*sbuffer = SendFileList.size;

if(!end)

{

SendFileList.message_end_flag = MCNTO;

*(sbuffer + 4 + (i+1) + 4 - 1) = SendFileList.message_end_flag;

}

printf("%x %x %x\n", *sbuffer, *(sbuffer +2), *(sbuffer + 3));

for(j = 0; j < i; j++)

{

printf("%c", *(sbuffer + 4 + j));

}

printf("%x\n", *(sbuffer + 4 + j + 1 + 4 -1));

bytes_sent += send(ServerSocket, sbuffer, *sbuffer, 0);

}

printf("List_of_Files sent: %d bytes sent\n", bytes_sent);

if((bytes_received += read(ServerSocket, rbuffer, 2)) > 0)

{

AckFileList.size = *rbuffer;

if((n = read(ServerSocket, (rbuffer+2), (AckFileList.size-2))) > 0)

{

bytes_received += n;

AckFileList.type = *(rbuffer + 2);

AckFileList.message_start_flag = *(rbuffer + 3);

*body = *(rbuffer + 4);

AckFileList.message_end_flag = *(rbuffer + sizeof(body) + 4 - 1);


printf("List_of_Files Acknowledge: %d bytes received\n", bytes_received);

}

}


for(wait = 0; wait < ONE_SECOND; wait++);

packetsSent++;

printf("sent number %d\n", packetsSent);

}

}




unsigned long getFileSize(FILE * List_of_Files, char * List)

{

unsigned long fileSize;

if((List_of_Files = fopen(List, "r")) == NULL)

{

fprintf(stderr, "Cannot open file.\n");

return -1;

}

else

{

if(fseek(List_of_Files, 0, SEEK_END) != 0)

{

fprintf(stderr, "Cannot use file.\n");

return -1;

}

fileSize=ftell(List_of_Files);

}

if(fclose(List_of_Files)!=0)

{

fprintf(stderr, "Cannot close file.\n");

return -1;

}

return fileSize;

}

****************************************************************************
********************************************************************

serverFileList3.c:

#include <stdio.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#define BUFFER_MAX 512

#define PGA 11

#define FLA 13

#define MESST 60

#define MESCON 61

#define MEND 70

#define MCNTO 71

typedef struct packet_header

{

unsigned size:16;

unsigned type:8;

unsigned message_start_flag:8;

unsigned message_end_flag:8;

} PACKET_HEADER;

int main (int argc, char **argv)

{

int port;

FILE *fp;

int sd, new_sd, client_len;

struct sockaddr_in server, client;

unsigned * rbuffer, * sbuffer, * body, block;

int n;

PACKET_HEADER sendPacket;

PACKET_HEADER receivePacket;

int bytes_sent;

int bytes_received = 0;

body = &block;


if(argc == 3)

port = atoi(argv[1]);

else

{

fprintf(stderr, "Usage: %s <port> <file>", argv[0]);

exit(1);

}


if((fp = fopen(argv[2], "w")) == NULL)

{

fprintf(stderr, "Error: Could not open %s\n", argv[3]);

exit(1);

}


if((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)

{

fprintf(stderr, "Can't create a socket\n");

exit(1);

}


bzero((char *) &server, sizeof(struct sockaddr_in));

server.sin_family = AF_INET;

server.sin_port = htons(port);

server.sin_addr.s_addr = htonl(INADDR_ANY);

if(bind(sd, (struct sockaddr *)&server, sizeof(server)) == -1)

{

fprintf(stderr, "Can't bind name to socket\n");

exit(1);

}


listen(sd,1);


client_len = sizeof(client);

if((new_sd = accept(sd, (struct sockaddr *) &client, &client_len)) == -1)

{

fprintf(stderr, "Can't accept client\n");

close(fp);

close(sd);

exit(1);

}


printf("Connection with client established\n");


//Allocate memory for the receiving buffer

if ((rbuffer = (unsigned *) malloc (BUFFER_MAX)) == NULL)

{

fprintf(stderr, "Not enough memory for a buffer size of %d \n", BUFFER_MAX);

exit(1);

}


if((bytes_received += read(new_sd, rbuffer, 2)) > 0)

{

receivePacket.size = *rbuffer;

if((n = read(new_sd, (rbuffer+2), (*rbuffer-2))) > 0)

{

bytes_received += n;

receivePacket.type = *(rbuffer + 2);

receivePacket.message_start_flag = *(rbuffer + 3);

*body = *(rbuffer + 4);

receivePacket.message_end_flag = *(rbuffer + sizeof(body) + 4 - 1);


printf("Port received: %d \n", *body);

}

}


printf("%x %x %x %x %x\n", *rbuffer, *(rbuffer +2), *(rbuffer +3),
*(rbuffer+4), *(rbuffer + sizeof(*(rbuffer+4)) + 4 -1));


if((sbuffer = (unsigned *) malloc (BUFFER_MAX)) == NULL)

{

fprintf(stderr, "Not enough memory for a buffer size of %d \n", BUFFER_MAX);

exit(1);

}

sendPacket.size = 5;

sendPacket.type = PGA;

sendPacket.message_start_flag = MESST;

sendPacket.message_end_flag = MEND;

*sbuffer = sendPacket.size;

*(sbuffer + 2) = sendPacket.type;

*(sbuffer + 3) = sendPacket.message_start_flag;

*(sbuffer + 4) = 0;

*(sbuffer + sizeof(*(sbuffer +4)) +4 - 1) = sendPacket.message_end_flag;

bytes_sent += send(new_sd, sbuffer, *sbuffer, 0);

bytes_sent += send(new_sd, (sbuffer+2), (sendPacket.size-2),0);

printf("%x %x %x %x %x\n", *sbuffer, *(sbuffer +2), *(sbuffer +3),
*(sbuffer+4), *(sbuffer + sizeof(*(rbuffer+4)) + 4 -1));

sendPacket.type = FLA;

*(sbuffer + 2) = sendPacket.type;

n = read(new_sd, rbuffer, BUFFER_MAX);

bytes_sent = send(new_sd, sbuffer, BUFFER_MAX, 0);

close(new_sd);

fclose(fp);

close(sd);


return 0;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top