C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 05-05-2005, 09:34 AM   #1
Registered User
 
Join Date: Apr 2005
Posts: 98
winsock questions

i have 2 questions:

1) is it possible that i don't have ws2_32.lib? i'm using windows xp and i can't find it.

2) accept() stops the program until it finds someone? if it is, how to make it searching only once each call? coz i can't do anything until it finds someone...
hiya is offline   Reply With Quote
Old 05-05-2005, 09:50 AM   #2
Registered User
 
Join Date: Jan 2005
Posts: 847
Quote:
Originally Posted by hiya
i have 2 questions:

1) is it possible that i don't have ws2_32.lib? i'm using windows xp and i can't find it.
it's always possible for a file to be deleted somehow, check your compilers's lib directory. If you using devc++ I think libraries are named *.a instead of *.lib

Quote:
Originally Posted by hiya
2) accept() stops the program until it finds someone? if it is, how to make it searching only once each call? coz i can't do anything until it finds someone...
If your socket is in blocking mode then accept will wait untill there is a connection available or an error before returning. If your socket is set to none-blocking mode then accept will return staight away. If there isn't any connection waiting to be accepted and there was no error a call to WSAGetLastError would give you the message WSAWOULDBLOCK.
If you want to change your socket to none-blocking mode look up the function ioctlsocket

Last edited by Quantum1024; 05-05-2005 at 09:55 AM.
Quantum1024 is offline   Reply With Quote
Old 05-05-2005, 10:09 AM   #3
Registered User
 
Join Date: Apr 2005
Posts: 98
thanks, you helped alot!

one more thing:
i'm trying to send a file with TransmitFile() and i don't get any errors for that, but when i try to receive the file in the client with recv(), all i get is one weird character...
hiya is offline   Reply With Quote
Old 05-05-2005, 10:27 AM   #4
Registered User
 
Join Date: Jan 2005
Posts: 847
Without seeing your code that calls recv it is difficult to say what the problem is.
Quantum1024 is offline   Reply With Quote
Old 05-05-2005, 10:58 AM   #5
Registered User
 
Join Date: Apr 2005
Posts: 98
the file sending function is:
Code:
bool SendFile()
{
     WIN32_FIND_DATA fData;
     HANDLE hnd = FindFirstFile("D:\aaa.txt", &fData);
     TransmitFile(newConn, hnd, 4, 3, NULL, NULL, 0)
     FindClose(hnd);
}
newConn = the socket.

and in the client:
Code:
char buf[10];
if(recv(sockfd, buf, sizeof(buf), 0) > 0)
cout<<buf;
sockfd = the socket.
hiya is offline   Reply With Quote
Old 05-05-2005, 11:30 AM   #6
Registered User
 
Join Date: Jan 2005
Posts: 847
You have 2 problems.
FindFirstFile returns a Find handle not a file handle you need to use CreateFile to open the file.
recv dosen't allways receive the entire amount at once because of the way networks work, you need to call recv in a loop untill you've received all bytes.
Quantum1024 is offline   Reply With Quote
Old 05-05-2005, 01:01 PM   #7
Registered User
 
Join Date: Apr 2005
Posts: 98
why the following CreateFile isn't working? (the return value is 123, don't know what that means)
Code:
HANDLE hnd = CreateFile("D:\aaa.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
hiya is offline   Reply With Quote
Old 05-05-2005, 02:10 PM   #8
Registered User
 
Join Date: Jan 2005
Posts: 847
Your CreateFile call looks fine, it would return INVALID_HANDLE_VALUE (0xffffffff) on error.
Another thing I've just noticed is your passing 4 as the file size to TransmitFile. If your file is 4 bytes thats fine otherwise use GetFileSize.
Quantum1024 is offline   Reply With Quote
Old 05-05-2005, 02:27 PM   #9
Registered User
 
Join Date: Apr 2005
Posts: 98
i found out what the problem was: i had to use double slashes ( \\ ).

here's the SendFile and recv code:
Code:
bool SendFile()
{
     HANDLE hnd = CreateFile("D:\\aaa.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
     if(hnd == INVALID_HANDLE_VALUE)
     cout<<"error => "<<GetLastError()<<endl;
     
     bool ret = TransmitFile(newConn, hnd, GetFileSize(hnd, NULL), 1, NULL, NULL, 0);
     CloseHandle(hnd);
     return ret;
}
Code:
char buf[2];
ZeroMemory(buf, 2);

while(true)
{
     if(recv(sockfd, buf, 1, 0) > 0)
          cout<<buf;
}
the problem is that everytime i get only part of the text in the file (each time different size of text).
i heard that send() sometimes doesn't send all data, but TransmitFile has the same problem?
hiya is offline   Reply With Quote
Old 05-05-2005, 03:50 PM   #10
Registered User
 
Join Date: Jan 2005
Posts: 847
Send can fail to send all of the data if you close the socket while data is still pending. You can get around this problem either by place a call to sleep before your call to closesocket or using WSAAsyncSelect to notify your application's window of when it is safe to close the socket.

Try a receive loop like this:
Code:
char buf[3]; //extra space for NULL
int nRemaining=2; //the amount you want to receive
int nRet;
char *Ptr=buf;
ZeroMemory(buf, 3);

while (nRemaining>0)
{
   nRet=recv(sockfd, buf, 2, 0)
   if (nRet==0 || nRet==SOCKET_ERROR
      return FALSE;
   nRemaining-=nRet;
   ptr+=nRet;
}
cout<<buf;

Last edited by Quantum1024; 05-05-2005 at 03:54 PM.
Quantum1024 is offline   Reply With Quote
Old 05-06-2005, 05:55 AM   #11
Registered User
 
Join Date: Apr 2005
Posts: 98
before i can check your code i have another problem:

after i read the file into a buffer
Code:
HANDLE hnd = CreateFile("D:\\aaa.txt", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hnd == INVALID_HANDLE_VALUE)
     cout<<"error => "<<GetLastError()<<endl;

char buf[GetFileSize(hnd, NULL)+1];
DWORD rn;
ReadFile(hnd, buf, GetFileSize(hnd, NULL), &rn, NULL);
i want to create a loop for sending one character at a time, but i can't send one element of buf in send, because it takes char* not int
Code:
for(int i=0; i<(int)rn; i++)
{
     send(newConn, buf[i], 1, 0);
}
so what to do?
hiya is offline   Reply With Quote
Old 05-06-2005, 06:40 AM   #12
Registered User
 
Join Date: Jan 2005
Posts: 847
There isn't anything wrong with sending the whole buffer in one call to send if you wanted to send it char at a time then you would need to use the address of the element not the element itself:
Code:
for(int i=0; i<(int)rn; i++)
{
     send(newConn, &buf[i], 1, 0);
}
and
Code:
char buf[GetFileSize(hnd, NULL)+1];
I'm not sure if you can allocate a buffer that way, I think you would either have to do:
Code:
//c++
char *buf=new char [GetFileSize(hnd, NULL)+1];
//c
char *buf=(char *)malloc(GetFileSize(hnd, NULL)+1);

Last edited by Quantum1024; 05-06-2005 at 06:49 AM.
Quantum1024 is offline   Reply With Quote
Old 05-06-2005, 09:46 AM   #13
Registered User
 
Join Date: Apr 2005
Posts: 98
when I printing &buf[i] it prints the whole file text and each time one letter from the beginning removed, for example, if the file contains "abcd" it will print:

abcd
bcd
cd
d
hiya is offline   Reply With Quote
Old 05-06-2005, 04:05 PM   #14
Registered User
 
Join Date: Jan 2005
Posts: 847
Quote:
Originally Posted by hiya
when I printing &buf[i] it prints the whole file text and each time one letter from the beginning removed, for example, if the file contains "abcd" it will print:

abcd
bcd
cd
d
Yes, because &buff[0] is the address of the fist charactor in the buffer and &buffer[1] is the address of the second charactor in the buffer and so on. So if you call printf with "%s" with the address of the first charactor it will start printing chars untill it reaches a zero at the end so you get the whole string and if you start printing from the second charactor address you get the buffer starting at the second charactor untill it reaches a zero. If you just want to print a singal char then use
Code:
printf("%c", buff[i]);
When you use send with a length paramater of 1 it only sends one char from the address you provide.

Last edited by Quantum1024; 05-06-2005 at 04:11 PM.
Quantum1024 is offline   Reply With Quote
Old 05-07-2005, 12:18 AM   #15
Registered User
 
Join Date: Apr 2005
Posts: 98
yea i got it working.
I'll see if i'm having another problem, but until now you helped alot, thanks =)
hiya is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Winsock issues tjpanda Windows Programming 3 12-04-2008 08:32 AM
2 questions about winsock 2 inhahe Windows Programming 2 05-13-2008 11:12 AM
A very long list of questions... maybe to long... Ravens'sWrath C Programming 16 05-16-2007 05:36 AM
WinSock questions. p_tyo C++ Programming 2 01-15-2007 12:20 AM
Where do I initialize Winsock and catch messages for it? Lithorien Windows Programming 10 12-30-2004 12:11 PM


All times are GMT -6. The time now is 02:11 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22