Winsock problem

Posted on 2006-05-02
Last Modified: 2010-04-05
Hi everybody,

I want to know everything about Winsock, so I read al lot of  the
questions about WinSock in this forum and searched in google about Win-

I made this code because I want to connect to an existing AS400 server:

I know how to  declare and allocate a socket and make a connection
to a server, and Send a command, but when I send a command
I get nothing in return. Can somebody help me to get a reaction
from the server:

procedure TForm1.Button1Click(Sender: TObject);
  wVersionRequested: WORD;
  wsaData: TWSAData;
  wVersionRequested := MAKEWORD(1, 1);
  WSAStartup(wVersionRequested, wsaData);
  if WSAStartup($101, WSAData) <> 0 then begin
    Memo1.Lines.Add('WSAStartup error')
    end else
    Memo1.Lines.Add('WSAStartup OK');
procedure TForm1.Button2Click(Sender: TObject);
  fSocket := socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if fSocket = INVALID_SOCKET then Memo1.Lines.Add('Impossible to open a socket')
  else Memo1.Lines.Add('Socket opened');
function TForm1.INetAddr(Host: string): integer;
  pHost: PChar;
  HostEnt: PHostEnt;
  if Host = '' then begin
    Result := INADDR_NONE;
  end else begin
    pHost := PChar(Host);
    Result := inet_addr(pHost);
    if Result = INADDR_NONE then begin
      HostEnt := gethostbyname(pHost);
      if HostEnt <> nil then Result := integer(pointer(HostEnt^.h_addr^)^);
procedure TForm1.Button3Click(Sender: TObject);
  WSAASyncSelect(fSocket, Handle, WM_USER, FD_CONNECT or FD_READ or FD_WRITE or FD_CLOSE);
procedure TForm1.Button4Click(Sender: TObject);
  sa: TSockAddr;
  FillChar(sa, SizeOf(sa), 0);
  sa.sin_family := AF_INET;
  sa.sin_addr.S_addr := INetAddr('');
  sa.sin_port := htons(23);
  if connect(fSocket, sa, SizeOf(sa)) <> 0 then Memo1.Lines.add('Unable to connect to server')
  else Memo1.Lines.Add('Connection Established');
procedure TForm1.Button5Click(Sender: TObject);
var request: String;
    result: Integer;
        request:= 'Hello How Are You';
        result:= Send(fsocket, request[1], Length(request), 0);
        if result < 0
        then Memo1.Lines.Add('Message not Send')
        else Memo1.Lines.Add('Message Send');


Question by:peterkiers
    LVL 5

    Expert Comment

    Hi man i also wanted to communicate betweensome programs using sock connections and TheRealLoki gave me this example :

    Works great im sure you can mod it to your needs...

    Peace Scay7
    LVL 1

    Author Comment

    mmm, thanks for the respons but I'm not exacly sure
    what to do with it, and I only want to use the Winsock-unit
    I don't want no component, The purpose of my code is to
    get the text from the AS400-server.  

    LVL 5

    Accepted Solution

    Okay hmm here or some links that might help but im sure that is
    easier to use components that come standard with delphi...

    But ye what ever... :P here are the links that helped me out a few times... <-- this one looks promising...

    peace Scay7
    LVL 1

    Author Comment

    Pfff, I have been reading and trying for a week now, I know
    now how to send a string or a file to a server, but supose I
    don't want to send a file or string, but just getting the logon-
    text of the server in my memo!. Just a connection to an AS400
    -server and get the logon-text of the AS400 in my Memo.

    When I use a ICS-component, and make a connection the server
    I get this(an AS400-logon screen with language Dutch):

                                           Systeem . . . . . :   AS4SV007
                                            Subsysteem  . . . :   QINTER      
                                             Beeldstation  . . :   QPADEV0003

    Gebruiker . . . . . . . . . . . .  
    Wachtwoord  . . . . . . . . . . .
    Programma/procedure . . . . . . .  
    Menu  . . . . . . . . . . . . . .  
    Actuele bibliotheek . . . . . . .  

     (C) COPYRIGHT IBM CORP. 1980, 2003.    

    And I want this with Winsock (not with a component)!

    Greetings and thank you in advanced.

    Peter Kiers

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
    Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now