-
May 16th, 2007, 07:51 PM
#1
Problems with send/recv file
Hey guys
Below I have two functions, sendingThread() and listenThread()
These two functions are used in a program that allows a user to either receive files or send files. Two people can use the program to transfer files between machines.
I suspect there is a problem with sendingThread() and the gethostbyaddr() function. the void * passed to the sendingThread() function contains a char * that is the IP address of the machine to try connecting to.
Please let me know if you see any errors in the code.
Here is the code for the two functions.
Code:
DWORD CALLBACK sendingThread(void *v)
{
unsigned int tot;
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(m_socket == INVALID_SOCKET)
{
goto sendclean;
}
sockaddr_in clientService;
struct hostent *hnet;
unsigned int addr;
addr=inet_addr((char *)v);
hnet=gethostbyaddr((char*)&addr,sizeof(addr),AF_INET);
if(hnet == 0)
{
goto sendclean;
}
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr=*((unsigned long*)hnet->h_addr);
clientService.sin_port = htons(55089);
if(connect(m_socket, (SOCKADDR *) &clientService, sizeof(clientService)) != 0)
{
goto sendclean;
}
unsigned int i, j;
tot = 0;
char sockbuf[1024];
FillMemory(sockbuf, 1024, 0x0);
for(i = 0;;)
{
j = fread(sockbuf, 1, sizeof(sockbuf), fptr);
if(j <= 0)
{
break;
}
unsigned int iRet = 0, soFar = 0;
while(soFar < j)
{
iRet = send(m_socket, sockbuf + soFar, j - soFar, 0);
tot += iRet;
char buf[80];
wsprintf(buf, "%d", tot);
SetWindowText(hwndBytesSent, buf);
if(iRet <= 0)
{
break;
}
soFar += iRet;
}
}
sendclean:
if(m_socket!=INVALID_SOCKET)
{
closesocket(m_socket);
m_socket=INVALID_SOCKET;
}
if(fptr!=0)
{
fclose(fptr);
fptr=0;
}
sending = false;
EnableWindow(hwndSend,TRUE);
return 0;
}
DWORD CALLBACK listenThread(void *v)
{
oursock = socket(AF_INET, SOCK_STREAM, 0);
if(oursock == INVALID_SOCKET)
{
goto theclean;
}
int sz;
sockaddr_in local;
sockaddr_in from;
local.sin_family = AF_INET;
local.sin_addr.s_addr = INADDR_ANY;
local.sin_port = htons(55089);
if(bind(oursock, (sockaddr *) &local, sizeof(local)) != 0)
{
goto theclean;
}
if(listen(oursock, 1) != 0)
{
goto theclean;
}
listening = true;
sz = sizeof(struct sockaddr_in);
socknew = accept(oursock, (struct sockaddr *) &from, &sz);
char sockbuf[1024];
unsigned int i, j;
FillMemory(sockbuf, 1024, 0x0);
DWORD dw;
for(i = 0;;)
{
j = recv(socknew, sockbuf, sizeof(sockbuf), 0);
char buf[80];
wsprintf(buf, "%d", i);
SetWindowText(hwndBytesRecv, buf);
if(j <= 0)
{
break;
}
i += j;
if(WriteFile(h, sockbuf, j, &dw, 0) == 0)
{
break;
}
}
theclean:
if(oursock!=INVALID_SOCKET)
{
closesocket(oursock);
oursock=INVALID_SOCKET;
}
if(socknew!=INVALID_SOCKET)
{
closesocket(socknew);
socknew=INVALID_SOCKET;
}
if(h!=INVALID_HANDLE_VALUE)
{
CloseHandle(h);
h=INVALID_HANDLE_VALUE;
}
listening=false;
EnableWindow(hwndListen,TRUE);
return 0;
}
-
May 16th, 2007, 08:00 PM
#2
Re: Problems with send/recv file
You need to be more explicit about the behavior you are seeing vs the behavior you expect, and about your debugging efforts and results to date.
It is completely unhelpful to state that you "suspect there is a problem..." Is there a problem in actuality? Have you even tried the code? What did you see? What did you expect to see? What have you narrowed it down to by debugging? What don't you understand about your debug results?
Feed us information and we will try our best to help.
Mike
-
May 16th, 2007, 08:25 PM
#3
Re: Problems with send/recv file
sorry i wasn't more specific.
gethostbyaddr is returning zero, i tried adding a message box in there.
my sendingThread function is the IP address passed to it via CreateThread. I think the problem might be that I had a local char buffer variable in the function that calls the thread.
Here is what I had before
Code:
char ip[16]; //this is local to the function
if(GetWindowText(hwndIP, ip, 16) == 0)
{
break;
}
DWORD tid;
sendThread = CreateThread(0, 0, sendingThread, (void*)ip, 0, &tid);
if(sendThread == 0)
{
sending = false;
EnableWindow(hwndSend,TRUE);
break;
}
here is what I changed the code too now; I made ip declared as a static char global variable.
Code:
memset(ip,0x0,16);
if(GetWindowText(hwndIP, ip, 16) == 0)
{
break;
}
DWORD tid;
sendThread = CreateThread(0, 0, sendingThread, 0, 0, &tid);
if(sendThread == 0)
{
sending = false;
EnableWindow(hwndSend,TRUE);
break;
}
I haven't tested the new code yet but im suspecting the problem is that the ip variable was local
-
May 16th, 2007, 08:51 PM
#4
Re: Problems with send/recv file
Passing the address of a local variable to a thread function is a definite no-no.
I wouldn't use a global, however. There's only one global, and that would limit you to exactly one thread.
Instead, in the function that creates the thread, "new/malloc" the array from the heap. Then, the thread function can "delete/free" it. That way, you can start as many threads as you like (within reason).
Mike
-
May 16th, 2007, 09:05 PM
#5
Re: Problems with send/recv file
Thanks for your help, MikeAThon
So if only one thread is going to be accessing the variable for the IP address, it is safe to use the global variable?
-
May 16th, 2007, 09:53 PM
#6
Re: Problems with send/recv file
-
May 17th, 2007, 01:38 AM
#7
Re: Problems with send/recv file
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|