+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    90

    WSACleanup question...

    I have a function that uses WinSock, and this is a private function for each thread. Almost all vars used in this function are local, only 2 booleans are private from thread. One to don't execute this function again if already executed once, and another to know if I got a valid result... In the end I call "WSACleanup;", but read in documentation this function(WSACleanup), terminates the sockets operations for all threads. The program looks like working nice, but I have this doubt... Is correct the way I'm doing this?

  2. #2
    Senior Member
    Join Date
    Nov 2011
    Posts
    156
    No, you must NOT use WSACleanup when ending thread functions, instead you must call it after you'll finish your job with sockets. You can use closesocket for closing sockets. It's like when you do:
    Code:
    LoadLibrary(kernel32.dll)
    do some kernel32 functions..
    FreeLibrary(kernel32.dll)
    do some kernel32 functions.. (but it will fail, cause kernel32.dll is released, but winsock api calls won't crash your app, it will just return WSANOTINITIALISED)
    or you can re-initialize winsock with WSAStartup after calling WSACleanup which are not recommended, just waste of speed.
    Last edited by Overflowz; 29-01-2012 at 14:53.

  3. #3
    Senior Member
    Join Date
    Jun 2011
    Location
    EAX
    Posts
    624
    Yeah its like locking an open door.

  4. #4
    Member
    Join Date
    Nov 2009
    Posts
    90
    Is this the right way??
    Code:
    var
      WSData: TWSAData;
      SockAddrIn: TSockAddrIn;
      HostEnt: PHostEnt;
      Sock: integer;
    begin
    ...
    Sock:= socket(AF_INET, SOCK_STREAM, 0);
    ...
    end;
    CloseSocket(Sock);

  5. #5
    Senior Member
    Join Date
    Nov 2011
    Posts
    156
    I don't know pascal well, but I think you must pass it in function. Look something like this:
    Code:
    int funca() {
    WSAStartup();
    ...
    socket();
    ...
    send something to server/client
    //done with sockets.
    closesocket();
    }

  6. #6
    Kool Kat envisiOn's Avatar
    Join Date
    Jul 2005
    Location
    www.seclude.us
    Posts
    119
    Loops work well. =]

    Code:
    procedure TServer.Connect;
    var
      Buffer: Array[0..8192] Of Char;
      iRecv: Integer;
    Begin
      if (WSAStartup($0202, wsaData) <> 0) then
      begin
        Exit;
      end;
      Close := False;
      repeat
        hSocket := Socket(AF_INET, SOCK_STREAM, 0);
        Addr.sin_family   := AF_INET;
        Addr.sin_port     := htons(hPort);
        Addr.sin_addr.S_addr := INET_ADDR(PChar(GetIPFromHost(hHost)));
    
        if (Winsock.Connect(hSocket, Addr, SizeOf(Addr)) = 0) then
        begin
          ZeroMemory(@Buffer, SizeOf(Buffer));
          iRecv := Recv(hSocket, Buffer, SizeOf(Buffer), 0);
    
          while ((iRecv > 0) and (iRecv <> INVALID_SOCKET)) do
            begin
              ReceiveData(hSocket, @Buffer);
              ZeroMemory(@Buffer, SizeOf(Buffer));
              iRecv := Recv(hSocket, Buffer, SizeOf(Buffer), 0);
            end;
          Windows.Sleep(200);
        end;
        CloseSocket(hSocket);
       until (Close);
    
       WSACleanup();
    end;
    and just call Server.Create to initialize the Connect (Mind you that you need Create, Connect, ReadData, SendData functions).
    I find that Krippler's socket source was one of the best ones who taught me how to do this.

    Located here: http://www.coderprofile.com/networks...insock-example

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. My question
    By MYSTiQUE in forum Delphi Help
    Replies: 9
    Last Post: 15-06-2009, 19:51
  2. On more question.....Please help me.....(spy-net rat 1.80
    By thanhduyle16 in forum Malware Discussion and General Help
    Replies: 2
    Last Post: 02-05-2009, 19:29
  3. <---- i'm the new guy. Obviously i have just ONE question!
    By duckyshucky in forum Malware Discussion and General Help
    Replies: 3
    Last Post: 12-12-2008, 19:57
  4. a question
    By acidrain in forum Delphi Help
    Replies: 10
    Last Post: 22-09-2008, 22:05
  5. C# Question Or C And C++
    By Akama in forum Malware Discussion and General Help
    Replies: 7
    Last Post: 16-09-2008, 20:28

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.