-
Hanging code:
this code hangs at line:
Code:
if (connect(serverSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
complete code:
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
char *ip;
char filename[32];
char buffer[256];
unsigned short port;
int serverSocket;
unsigned long filesize;
FILE *readFile, *writeFile;
struct sockaddr_in serverAddr;
char done;
int main (int argc, const char * argv[]) {
printf("rev: 2\n");
if (argc > 1)
{
ip = malloc(15*sizeof(char));
strncpy(ip, argv[1], sizeof(ip));
if (argc > 2)
port = atoi(argv[2]);
else
{
printf("Usage: fEcho [ip] [port]\n--using: %s:65530\n", ip);
port = 65530;
}
}
else
{
ip = malloc(15*sizeof(char));
ip = "127.0.0.1";
port = 65530;
printf("Usage: fEcho [ip] [port]\n--using: %s:%d\n", ip, port);
}
printf("Filename: ");
scanf("%s", filename);
fflush(stdouot);
readFile = fopen(filename, "r");
strcat(filename, ".copy");
writeFile = fopen(filename, "w");
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
serverAddr.sin_addr.s_addr = htonl(inet_addr(ip));
if (ip != NULL)
{
// free(ip);
ip = NULL;
}
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket < 0)
{
printf("Error creating socket\n");
exit(1);
}
if (connect(serverSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0)
{
printf("Error: connect()\n");
exit(1);
}
for(;;)
{
printf("Tranfering file:", stdout);
while(fgets(buffer, sizeof(buffer), readFile) != NULL)
{
send(serverSocket, &buffer, sizeof(buffer), 0);
//printf(".");
memset(&buffer, 0, sizeof(buffer));
listen(serverSocket, 1);
recv(serverSocket, &buffer, sizeof(buffer), 0);
//printf(".");
fprintf(writeFile, "%s", buffer);
}
fclose(writeFile);
fclose(readFile);
printf("Next file['q' to quit]:");
scanf("%s", filename);
if (strcmp(filename, "q") == 0)
{
close(serverSocket);
return(0);
}
readFile = fopen(filename, "r");
strcat(filename, ".copy");
writeFile = fopen(filename, "w");
}
return(0);
}
I'm just absolutely baffled...
TIA
-
Do you know that it's really hanging there? I can see a number of things could take a bit of tweaking:
Code:
for(;;) you are in an infinite loop...
{
printf("Tranfering file:", stdout); you don't flush your output stream
while(fgets(buffer, sizeof(buffer), readFile) != NULL) assume it reads...
{
send(serverSocket, &buffer, sizeof(buffer), 0); you don't check any of these return values
//printf(".");
memset(&buffer, 0, sizeof(buffer));
listen(serverSocket, 1);
recv(serverSocket, &buffer, sizeof(buffer), 0);
//printf(".");
fprintf(writeFile, "%s", buffer);
}
fclose(writeFile);
fclose(readFile);
printf("Next file['q' to quit]:"); you still haven't flushed your output..
scanf("%s", filename); you might just be sitting here...
if (strcmp(filename, "q") == 0)
{
close(serverSocket);
return(0);
}
readFile = fopen(filename, "r");
strcat(filename, ".copy");
writeFile = fopen(filename, "w");
}
I would advise checking the return values of all of your functions, and flush the output of all of your attempts. You may just be sitting there waiting for you to type something. Add a \n on the end of all of your prints, or fflush( stream ). That way you'll know for sure. If it is really hanging at connect, then you'll probably need to see that what you're really trying to connect to can actually be connected to (though again, I doubt it's actually connect that is hanging).
Quzah.
-
The exact problem is in this line ,
Code:
serverAddr.sin_addr.s_addr = htonl(inet_addr(ip));
It should be,
Code:
serverAddr.sin_addr.s_addr = inet_addr(ip);
Because inet_addr is used to convert the host byte order ip to network byte order ip . But htonl is used to convert the hostlong integer to network long integer .So no need to use this for the ip.inet_addr is enough .