int main(int argc, char *argv[]) { int sockfd; // File descriptors FILE *get, *socks; int bytes, x, i, port; // Loops, etc int listing; // Processes char buffer[1024], fbuffer[1024], *tokens[1024], server[512], *flist[512]; struct sockaddr_in remote; struct hostent *addr; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } printf("Server address: "); scanf("%s", &server); printf("Server port: "); scanf("%i", &port); if ((addr=gethostbyname(server)) == NULL) { herror("gethostbyname"); exit(1); } remote.sin_family = PF_INET; remote.sin_port = htons(port); remote.sin_addr = *((struct in_addr*)addr->h_addr); memset(remote.sin_zero, '\0', sizeof(remote.sin_zero)); if ((connect(sockfd, (struct sockaddr *)&remote, sizeof(struct sockaddr))) == -1) { perror("connect"); exit(1); } else { printf("Connected to %s at port %i!.\n", server, port); Send(sockfd, "LIST\n"); socks = fdopen(sockfd, "r"); while (1) { if (fgets(fbuffer, 1024, socks) == NULL) { if (bytes != 0) perror("recv"); printf("Remote server closed connection... Exiting...\n"); close(sockfd); exit(1); } else { i = 0; bytes = strlen(fbuffer); for (x = 0; x < strlen(fbuffer); x++) { if (fbuffer[x] == '\0') break; else if (fbuffer[x] == '\n') break; else if (fbuffer[x] == '\r') break; else buffer[i++] = fbuffer[x]; } tokenize(tokens, buffer, ' '); if (strstr(buffer, "STARTLIST")) listing = 1; else if (strstr(buffer, "ENDLIST")) { listing = 0; printf("File list:\n"); for (x = 0; x < realSize(flist); x++) printf("File number %i: %s\n", x, flist[x]); } else if (strstr(tokens[0], "LIST") && listing == 1) { flist[atoi(tokens[1])] = strdup(tokens[2]); } printf("Incoming package (%i Bytes): %s\n", bytes, buffer); memset(buffer, '\0', sizeof(buffer)); } } } }