winsock questions

This is a discussion on winsock questions within the Networking/Device Communication forums, part of the General Programming Boards category; 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 ...

  1. #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...

  2. #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.

  3. #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...

  4. #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.

  5. #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.

  6. #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.

  7. #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);

  8. #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.

  9. #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?

  10. #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.

  11. #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?

  12. #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.

  13. #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

  14. #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.

  15. #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 =)

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 07:32 AM
  2. 2 questions about winsock 2
    By inhahe in forum Windows Programming
    Replies: 2
    Last Post: 05-13-2008, 11:12 AM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  4. WinSock questions.
    By p_tyo in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2007, 11:20 PM
  5. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 11:11 AM

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