![]() |
| | #1 |
| Registered User Join Date: Apr 2005
Posts: 98
| winsock 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 | |
| | #2 | ||
| Registered User Join Date: Jan 2005
Posts: 847
| Quote:
Quote:
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 | |
| | #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 | |
| | #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 | |
| | #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);
}
and in the client: Code: char buf[10]; if(recv(sockfd, buf, sizeof(buf), 0) > 0) cout<<buf; |
| hiya is offline | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #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;
}
i heard that send() sometimes doesn't send all data, but TransmitFile has the same problem? |
| hiya is offline | |
| | #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 | |
| | #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);
Code: for(int i=0; i<(int)rn; i++)
{
send(newConn, buf[i], 1, 0);
}
|
| hiya is offline | |
| | #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);
}
Code: char buf[GetFileSize(hnd, NULL)+1]; 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 | |
| | #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 | |
| | #14 | |
| Registered User Join Date: Jan 2005
Posts: 847
| Quote:
Code: printf("%c", buff[i]);
Last edited by Quantum1024; 05-06-2005 at 04:11 PM. | |
| Quantum1024 is offline | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |