hi friends, i want to move folder contents from one m/c(client) to another m/c(server). for this i have written two programs client.c and server.c. for this there can be two approches :- (a) client sends file wait for ack from server and on recieving ack send next file. (b) client just pushes the all the files and the server reads the contents. no ack in this approach. i m trying 2nd approach but it doesnt work. here i have decided for a delimeter which is '\012'(LF). the cliebt send file name ending with LF, hen send file perms ending with LF then sends file size ending with LF and last the file contents ending with LF. please go through the code and reply. mehul. /************************ client.c *******************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define PORT 8000 #define BACKLOG 10 #define ERR_REPORT "client" #define DOUGNET_VERSION "1.0" #define CVS_OK_SEND_FILES "100 Ok; send files.\n" /* * Sends a string 'buf' on the descriptor sd. Returns the number of * bytes written, or -1 if an error occured. */ int sendstring(int sd, char *buf) { return write(sd, buf, strlen(buf)); }/* sendstring() */ /* * This will close the socket 'sd'. Returns -1 on error, 0 on success. */ int closesock(int sd) { return close(sd); }/* closesock() */ /* * This will return a new socket connected to the host specified in * 'toconn' (can be an IP or a DNS name) on the port 'port'. Returns * NULL if unsuccessful. */ int openconnection(char *toconn, int port) { struct hostent *he; struct sockaddr_in their_addr; /* connector's address information */ int sd, res; he = gethostbyname(toconn); /* get the host info */ if(NULL == he) { #ifdef ERR_REPORT fprintf(stderr,"%s: Unable to lookup host!\n", ERR_REPORT); #endif return (int) NULL; }/* if(NULL == he) */ sd = socket(AF_INET, SOCK_STREAM, 0); if(sd == -1) { #ifdef ERR_REPORT fprintf(stderr,"%s: Unable to create connection socket!\n", ERR_REPORT); #endif return (int) NULL; }/* if(sd == -1) */ their_addr.sin_family = AF_INET; /* host byte order */ their_addr.sin_port = htons(port); /* short, network byte order */ their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero), 8); /* zero the rest of the struct */ res = connect(sd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)); if(res == -1) { #ifdef ERR_REPORT fprintf(stderr, "%s: Unable to create connection to %s!\n", ERR_REPORT, toconn); #endif return (int) NULL; }/* if(res == -1) */ return sd; }/* openconnection() */ int GetFileSize(const char *file) { int fileSize = 0; struct stat st; int retVal; retVal = lstat(file,&st); if(retVal < 0) { return -1; }/* if(retVal < 0) */ fileSize = st.st_size; return fileSize; }/* GetFileSize() */ /* * checks whether a folder exists. * return 1 if folder exists else 0. */ int isDir(char *dirName) { DIR *dt = NULL; dt = opendir(dirName); if(NULL == dt) { return 0; }/* if(NULL == dt) */ closedir(dt); return 1; }/* isDir() */ /* * sends folder contents. * if successful returns 1 else 0. */ int sendDirContents(int s, char *dirName) { DIR *dp; char fileName[256 + 1]; struct dirent *d; dp = opendir(dirName); if(NULL == dp) { printf("ERROR unable to open %s directory, at line %d of file %s %s.\n", dirName,__LINE__, __FILE__, strerror(errno)); return 0; }/* if(NULL == dp) */ while((d = readdir(dp)) != NULL) { int fileSize, retStatus; char str[10]; char cmd[1025]; FILE *fp = NULL; if( !( strcmp(d->d_name,".") ) || !( strcmp(d->d_name,"..") ) ) { continue; }/* if( !( strcmp(d->d_name,".") ) || !( strcmp(d->d_name,"..") ) ) */ sprintf(fileName,"%s/%s",dirName,d->d_name); retStatus = isDir(fileName); if(retStatus) { /* folder */ int res = sendDirContents(s, fileName); if(!res) { return 0; }/* if(!res) */ }/* if(retStatus) */ sendstring(s, fileName); sendstring(s,"\012"); sendstring(s, "u=rw,g=r,o=r"); sendstring(s,"\012"); fileSize = GetFileSize(fileName); memset(str, 0, sizeof(str)); sprintf(str, "%d", fileSize); sendstring(s, str); sendstring(s, "\012"); printf("fileName = %s str = %s\n", fileName, str); fp = fopen(fileName,"rb"); if(NULL == fp) { return; }/* if(NULL == fp) */ while(!feof(fp)) { int bytesRead; memset(cmd, 0, 1024); bytesRead = fread(cmd, 1, 1024, fp); cmd[bytesRead] = 0; sendstring(s, cmd); printf("cmd = %s strlen(cmd) = %d bytesRead = %d\n", cmd, strlen(cmd), bytesRead); }/* while(!feof(fp)) */ fclose(fp); }/* while((d = readdir(dp)) != NULL) */ closedir(dp); return 1; }/* sendDirContents() */ int main (int argc, char *argv[]) { int s, res; char tpbuf[100]; char dirName[50]; char *str = NULL; if(argc != 3) { fprintf(stderr, "Usage: client \n"); exit(0); }/* if(argc != 3) */ s = openconnection(argv[1], PORT); if(s == (int) NULL) { fprintf(stderr, "client: Unable to connect to %s on port %d!\n", argv[1], PORT); exit(0); }/* if(s == (int) NULL) */ bzero(dirName, sizeof(dirName)); str = (char *)strrchr(argv[2], '/'); if(NULL == str) { strncpy(dirName, argv[2], strlen(argv[2])); }/* if(NULL == str) */ else { strncpy(dirName, (str + 1), strlen((str + 1))); *str = '\0'; if(chdir(argv[2]) != 0) { printf("Error : cannot chdir to %s (error %s)!\n", argv[2], strerror(errno)); }/* if(chdir(argv[2]) != 0) */ }/* else for if(NULL == str) */ printf("dirName = %s\n", dirName); sendstring(s, dirName); sendstring(s, "\012"); res = sendDirContents(s, dirName); if(!res) { printf("unable to send contents of dir %s\n", dirName); }/* if(!res) */ sendstring(s, "Finished sending."); sendstring(s, "\012"); closesock(s); return 0; }/* main() */ /************************ client.c *******************************/ /************************ server.c *******************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #define PORT 8000 #define BACKLOG 10 #define ERR_REPORT "server" #define DOUGNET_VERSION "1.0" /* Permission Modes required when creating dir */ #define DIR_PERM (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH) #define ownerRead 0400 #define ownerWrite 0200 #define ownerExecute 0100 #define groupRead 040 #define groupWrite 020 #define groupExecute 010 #define othersRead 04 #define othersWrite 02 #define othersExecute 01 #define CVS_BASE_PATH "/usr/kalinga/tmp" #define CVS_FINISHED_SENDING "Finished sending." /* * Will immediatley return a positive value when sd gets data on it. * It will wait up to ustimeout microseconds, and return 0 if no * data is waiting on the socket. */ int pollsocket(int sd, long ustimeout) { fd_set rfds; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = ustimeout; FD_ZERO(&rfds); FD_SET(sd, &rfds); select(FD_SETSIZE, &rfds, NULL, NULL, &tv); return FD_ISSET(sd, &rfds); }/* pollsocket() */ /* * Will accept a waiting connection on 'listenfd', and return this new * socket. It will return NULL if unsuccessful. * If 'theirip' isn't NULL, it will copy the dotted quad notation IP * address (eg 192.168.23.4) into 'theirip'. Make sure 'theirip' can * contain at least 16 characters. */ int acceptfromlistener(int listenfd, char *theirip) { struct sockaddr_in their_addr; int tp = sizeof(struct sockaddr_in); int connfd; if(!pollsocket(listenfd, 0)) { return -1; }/* if(!pollsocket(listenfd, 0)) */ connfd = accept(listenfd, (struct sockaddr *)&their_addr, &tp); if(connfd == -1) { #ifdef ERR_REPORT fprintf(stderr, "%s: Unable to accept connection!\n", ERR_REPORT); #endif return (int) NULL; }/* if(connfd == -1) */ if(theirip != NULL) { snprintf(theirip, 16, "%s", inet_ntoa(their_addr.sin_addr)); }/* if(theirip != NULL) */ return connfd; }/* acceptfromlistener() */ /* This will close the socket 'sd'. Returns -1 on error, 0 on success. */ int closesock(int sd) { return close(sd); }/* closesock() */ /* This will start listening on port 'port', and return this new socket. * Returns NULL if unsuccessful. */ int openlistener(int port) { int tp, sd; struct sockaddr_in my_addr; int res; sd = socket(AF_INET, SOCK_STREAM, 0); if(sd == -1) { #ifdef ERR_REPORT fprintf(stderr,"%s: Unable to create server socket!\n", ERR_REPORT); #endif return (int) NULL; }/* if(sd == -1) */ /* * This excellent bugfix added by Fabrice Marie to Anti-Web. */ res = setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &tp, sizeof(tp)); if(res == -1) { #ifdef ERR_REPORT fprintf(stderr, "%s: Unable to setsockopt()s. Non-fatal.\n", ERR_REPORT); #endif }/* if(res == -1) */ my_addr.sin_family = AF_INET; /* host byte order */ my_addr.sin_port = htons(port); /* short, network byte order */ my_addr.sin_addr.s_addr = INADDR_ANY;/* automatically fill with my IP */ memset(&(my_addr.sin_zero), 0, 8); /* zero the rest of the struct */ res = bind(sd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)); if(res == -1) { #ifdef ERR_REPORT fprintf(stderr,"%s: Unable to bind!\n", ERR_REPORT); #endif return (int) NULL; }/* if(res == -1) */ if(listen(sd, BACKLOG) == -1) { #ifdef ERR_REPORT fprintf(stderr,"%s: Unable to listen!\n", ERR_REPORT); #endif return (int) NULL; }/* if(listen(sd, BACKLOG) == -1) */ return sd; }/* openlistener() */ /* * Read a line from socket SOCK. Result does not include the * terminating linefeed. * Returns number of bytes read. */ static int recv_line (int sock, char **resultp) { char *result; size_t input_index = 0; size_t result_size = 80; result = (char *)malloc(result_size); if(NULL == result) { return 0; }/* if(NULL == result) */ bzero(result, result_size); while(1) { char ch; int n; n = recv (sock, &ch, 1, 0); if(n <= 0) { printf("recv() from server\n"); }/* if(n <= 0) */ if(ch == '\012') { break; }/* if(ch == '\012') */ result[input_index++] = ch; while(input_index + 1 >= result_size) { result_size *= 2; result = (char *) realloc(result, result_size); if(NULL == result) { return 0; }/* if(NULL == result) */ bzero(&result[input_index], (result_size - input_index)); }/* while(input_index + 1 >= result_size) */ }/* while(1) */ /* * Terminate it just for kicks, but we *can* deal with embedded NULs. */ result[input_index] = '\0'; if(resultp) { *resultp = result; }/* if(resultp) */ return input_index; }/* recv_line () */ /* * checks whether a folder exists. * return 1 if folder exists else 0. */ int isDir(char *dirName) { DIR *dt = NULL; dt = opendir(dirName); if(NULL == dt) { return 0; }/* if(NULL == dt) */ closedir(dt); return 1; }/* isDir() */ /* * creates folder. * returns 1 if successful else 0. */ int createDir(char *dirName) { int retStatus; char *str = NULL, *str1 = NULL; char *name = NULL, *name1 = NULL; int bytes = 0; if(!dirName) { return 0; }/* if(!dirName) */ str = str1 = name1 = dirName; printf("str = %s strlen(str) = %d\n", str, strlen(str)); while(name1 != NULL) { name1 = (char *)strchr(str,'/'); if(NULL == name1) { bytes = strlen(str1); }/* if(NULL == name1) */ else { bytes = strlen(str1) - strlen(name1); printf("name1 = %s\n", name1); }/* else for if(NULL == name1) */ printf("bytes = %d\n", bytes); name = (char *)malloc((bytes + 1) * sizeof(char)); if(NULL == name) { return 0; }/* if(NULL == name) */ strncpy(name, str1, bytes); name[bytes] = '\0'; printf("name = %s\n", name); if(!isDir(name)) { retStatus = mkdir(name, DIR_PERM); if(retStatus != 0) { printf("%s\n", strerror(errno)); return 0; }/* if(retStatus != 0) */ }/* if(!isDir(name)) */ if(name1) { if(strlen(name1) == 1) { name1 = NULL; }/* if(strlen(name1) == 1) */ else { str = name1 + 1; }/* else for if(strlen(name1) == 1) */ free(name); name = NULL; }/* if(name1) */ }/* while(str != NULL) */ return 1; }/* createDir() */ /* * change mode of the file. * if successful returns 1 else 0 value. */ int changeMod(char *fName, char *modeStr) { mode_t mode = 0; FILE *fp = NULL; char *ptr = NULL; int ownerFlag = 0, groupFlag = 0, othersFlag = 0; int retStatus = 0; fp = fopen(fName,"wb"); if(NULL == fp) { return 0; }/* if(NULL == fp) */ fclose(fp); ptr = modeStr; for(;*ptr;ptr++) { if((*ptr == '=') || (*ptr == ',')) { continue; }/* if((*ptr == '=') || (*ptr == ',')) */ if(*ptr == 'u') { ownerFlag = 1; groupFlag = 0; othersFlag = 0; }/* if(*ptr == 'u') */ if(*ptr == 'g') { ownerFlag = 0; groupFlag = 1; othersFlag = 0; }/* if(*ptr == 'g') */ if(*ptr == 'o') { ownerFlag = 0; groupFlag = 0; othersFlag = 1; }/* if(*ptr == 'o') */ if(*ptr == 'r') { if(ownerFlag) { mode = mode + ownerRead; }/* if(ownerFlag) */ else if(groupFlag) { mode = mode + groupRead; }/* else if(groupFlag) */ else if(othersFlag) { mode = mode + othersRead; }/* else if(othersFlag) */ }/* if(*ptr == 'r') */ if(*ptr == 'w') { if(ownerFlag) { mode = mode + ownerWrite; }/* if(ownerFlag) */ else if(groupFlag) { mode = mode + groupWrite; }/* else if(groupFlag) */ else if(othersFlag) { mode = mode + othersWrite; }/* else if(othersFlag) */ }/* if(*ptr == 'r') */ if(*ptr == 'x') { if(ownerFlag) { mode = mode + ownerExecute; }/* if(ownerFlag) */ else if(groupFlag) { mode = mode + groupExecute; }/* else if(groupFlag) */ else if(othersFlag) { mode = mode + othersExecute; }/* else if(othersFlag) */ }/* if(*ptr == 'r') */ }/* for(;ptr;) */ retStatus = chmod(fName, mode); if(retStatus != 0) { return 0; }/* if(retStatus != 0) */ return 1; }/* changeMod() */ /* * gets file contents from the client. * if successful returns 1 else 0 value. */ int getFileContents(int sock, char *fName, int bytes) { FILE *fp = NULL; int cnt = 0, len, bytesWritten; char *cmd = NULL; int a; char ch; int n; fp = fopen(fName,"wb"); if(NULL == fName) { return 0; }/* if(NULL == fName) */ while(cnt < bytes) { n = recv (sock, &ch, 1, 0); if(n <= 0) { printf("recv() from server\n"); }/* if(n <= 0) */ bytesWritten = fwrite(&ch, 1, n, fp); if(bytesWritten != n) { if(fp) { fclose(fp); }/* if(fp) */ if(cmd) { free(cmd); cmd = NULL; }/* if(cmd) */ return 0; }/* if(bytesWritten != n) */ printf("cnt = %d\n", cnt); cnt = cnt + n; printf("cnt = %d\n", cnt); }/* while(cnt < bytes) */ printf("file name = %s\n", fName); printf("bytes = %d\n", bytes); /* scanf("%d", &a); */ if(fp) { fclose(fp); }/* if(fp) */ return 1; }/* getFileContents() */ /* * gets contents of a folder. * including contents of subfolders. * returns 1 if successful else 0. */ int getDirContents(int sock, char *basePath, char *dirName) { char fullPath[256]; int res; int exitFlag = 0; int a; bzero(fullPath, sizeof(fullPath)); sprintf(fullPath, "%s/%s", basePath, dirName); printf("fullPath = %s\n", fullPath); if(chdir(basePath) != 0) { printf("Error : cannot chdir to %s (error %s)!\n", basePath, strerror(errno)); }/* if(chdir(basePath) != 0) */ res = createDir(dirName); if(!res) { printf("unable to create folder %s\n", dirName); return 0; }/* if(!res) */ while(1) { char *fileName, *filePerms, *fileSize; char *str = NULL; int fSize; if(exitFlag) { break; }/* if(exitFlag) */ recv_line(sock, &fileName); /* strstr(fileName, "pfds[") && (strstr(fileName, "].events") || strstr(fileName, "].fd")) */ res = strcasecmp(fileName, CVS_FINISHED_SENDING); if(!res) { exitFlag = 1; if(fileName) { free(fileName); fileName = NULL; }/* if(fileName) */ if(filePerms) { free(filePerms); filePerms = NULL; }/* if(filePerms) */ if(fileSize) { free(fileSize); fileSize = NULL; }/* if(fileSize) */ continue; }/* if(!res) */ printf("fileName = %s\n", fileName); str = (char *)strrchr(fileName, '/'); if(NULL != str) { char *path = (char *)malloc(sizeof(char) * strlen(fileName) - strlen(str) + 1); if(NULL == path) { exitFlag = 1; if(fileName) { free(fileName); fileName = NULL; }/* if(fileName) */ if(filePerms) { free(filePerms); filePerms = NULL; }/* if(filePerms) */ if(fileSize) { free(fileSize); fileSize = NULL; }/* if(fileSize) */ continue; }/* if(NULL == path) */ memset(path, 0, strlen(fileName) - strlen(str) + 1); strncpy(path, fileName, strlen(fileName) - strlen(str) + 1); res = createDir(path); if(!res) { printf("unable to create folder %s\n", path); exitFlag = 1; if(fileName) { free(fileName); fileName = NULL; }/* if(fileName) */ if(filePerms) { free(filePerms); filePerms = NULL; }/* if(filePerms) */ if(fileSize) { free(fileSize); fileSize = NULL; }/* if(fileSize) */ continue; }/* if(!res) */ if(path) { free(path); path = NULL; }/* if(path) */ }/* if(NULL != str) */ recv_line(sock, &filePerms); res = changeMod(fileName, filePerms); printf("filePerms = %s\n", filePerms); if(!res) { printf("error while changing mode of file %s\n", fileName); exitFlag = 1; if(fileName) { free(fileName); fileName = NULL; }/* if(fileName) */ if(filePerms) { free(filePerms); filePerms = NULL; }/* if(filePerms) */ if(fileSize) { free(fileSize); fileSize = NULL; }/* if(fileSize) */ continue; }/* if(res != 0) */ recv_line(sock, &fileSize); printf("fileSize = %s\n", fileSize); fSize = atoi(fileSize); printf("fSize = %d\n", fSize); res = getFileContents(sock, fileName, fSize); if(!res) { printf("error while recieving contents of file %s\n", fileName); exitFlag = 1; if(fileName) { free(fileName); fileName = NULL; }/* if(fileName) */ if(filePerms) { free(filePerms); filePerms = NULL; }/* if(filePerms) */ if(fileSize) { free(fileSize); fileSize = NULL; }/* if(fileSize) */ continue; }/* if(res != 0) */ if(fileName) { free(fileName); fileName = NULL; }/* if(fileName) */ if(filePerms) { free(filePerms); filePerms = NULL; }/* if(filePerms) */ if(fileSize) { free(fileSize); fileSize = NULL; }/* if(fileSize) */ }/* while(1) */ if(exitFlag) { return 0; }/* if(exitFlag) */ return 1; }/* getDirContents() */ int main(int argc, char *argv[]) { int s,t; char tpbuf[16]; char *dirName; int res; if((s = openlistener(PORT)) == (int) NULL) { exit(0); }/* if((s = openlistener(PORT)) == (int) NULL) */ while(1) { if(pollsocket(s, 10000)) { t = acceptfromlistener(s, tpbuf); printf("Connection from %s\n", tpbuf); recv_line(t, &dirName); printf("dirName = %s\n", dirName); res = getDirContents(t, CVS_BASE_PATH, dirName); if(!res) { printf("unable to get contents of dir %s\n", dirName); }/* if(!res) */ if(dirName) { free(dirName); dirName = NULL; }/* if(dirName) */ closesock(t); }/* if(pollsocket(s, 10000)) */ }/* while(1) */ }/* main() */ /************************ server.c *******************************/