#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    3
    Rep Power
    0

    Delphi 2010 WINSOCK ACCEPT


    Hello! Help! I beg you.
    I do not know where to look and why do not work.
    I sketched a small server and client code.
    A socket is created, the server is listening on the port.
    Client also connect.
    This shows TCPView (client ESTABLISHED. Also tried through telnet.
    Connection occurs.
    But why is the server does not work function ACCEPT.
    This code program go to END. And it should stay on ACCEPT. Passes ACCEPT and outputs the result -1.
    Tried ACCEPT place in the cycle. constantly gives -1 and does not send the message HELLO. What's wrong? Everywhere such examples. Help.
    Code:
    procedure TForm1.Button2Click(Sender: TObject);
    var sock_client,sock_server:integer;
        wsa_Data:WSADATA;
        err:integer;
        addrClient,addrServ:sockaddr_in;
        buf:array[1..1024] of pansichar;
    begin
       //WSA
       err:=WSAStartup($101,wsa_Data);
       if err=SOCKET_ERROR then
       showmessage('WSA_START_UP '+IntToStr(GetlastError));
       //create socket
       sock_server:=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
       if sock_server<0 then
       begin
           showmessage('socket not create');
           exit;
       end;
       //set
       addrServ.sin_family:=AF_INET;
       addrServ.sin_addr.S_addr:=htonl(INADDR_ANY);
       addrServ.sin_port:=htons(5060);
       // link bind
       err:=bind(sock_server,&addrServ,sizeof(addrServ)) ;
       if err=SOCKET_ERROR then
          begin
           showmessage('bind error');
           exit;
       end;
       //listen socket
       listen(sock_server,5);
    
      buf[1]:='HELLO';
      sock_Client:=accept(sock_server,pointer(@addrClient),pointer(sizeof(addrClient)));
      send(sock_Client,buf,sizeOf(buf),0);
    procedure TForm1.Button2Click(Sender: TObject);
    var sock_client,sock_server:integer;
        wsa_Data:WSADATA;
        err:integer;
        addrClient,addrServ:sockaddr_in;
        buf:array[1..1024] of pansichar;
    begin
       //WSA
       err:=WSAStartup($101,wsa_Data);
       if err=SOCKET_ERROR then
       showmessage('WSA_START_UP '+IntToStr(GetlastError));
       //create socket
       sock_server:=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
       if sock_server<0 then
       begin
           showmessage('socket not create');
           exit;
       end;
       //set
       addrServ.sin_family:=AF_INET;
       addrServ.sin_addr.S_addr:=htonl(INADDR_ANY);
       addrServ.sin_port:=htons(5060);
       // link bind
       err:=bind(sock_server,&addrServ,sizeof(addrServ)) ;
       if err=SOCKET_ERROR then
          begin
           showmessage('bind error');
           exit;
       end;
       //listen socket
       listen(sock_server,5);
    
      buf[1]:='HELLO';
      sock_Client:=accept(sock_server,pointer(@addrClient),pointer(sizeof(addrClient)));
      send(sock_Client,buf,sizeOf(buf),0);
    end;
    and another question, how to use buf: array [1 .. 255] of char. lot function are essential to take or give result BUF. I only issued the first character.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    I have not done Winsock code for a while but I believe the buffers should be declared as
    ansichar NOT pansichar.

    Given the declaration as pansichar, I think that buf[1] contains 'HELLO' not 'H' as I suspect you imagine.

    In addition I do not see where you initialize the buffer, so all other elements will contain random data.

    Also, I recommend (although I do not think it affects whether the code works) that you declare any such buffers as zero based.

    Code:
    buf:array[0..1023] of ansichar;
    HTH - Clive
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    3
    Variable buf was never been initialized. And the declaration is wrong, for your purpose. You should declare buf either as:
    Code:
    var buf: PAnsiChar;
    or

    Code:
    var buf: array [1..1024] of AnsiChar;
    or
    Code:
    var buf: RawByteString;
    Code:
    var buf: AnsiString;


    Initialize the first approach by using GetMem(Buf, 1024), which later you must finalized with FreeMem.

    The second approach does not require initialization of finalization since Delphi will allocate and deallocate the required memory upon entering and leaving the routine.

    The third and fourth approach only need initialization, i.e. setting the length of the string. I.e.: SetLength(Buffer, 1024).

    Of course there will be small differences in using the buffer between the first approach and the rest.

IMN logo majestic logo threadwatch logo seochat tools logo