Search code examples
formsdelphiconsole-applicationdelphi-2010

How to port Windows form to console application in Delphi


I've made a chat server using ScktComp and Winsock in a Windows form. How can I make the same thing in an console application?

I found this as a starting point but, the links inside (which the thread's author replied "almost perfect answer") are expired.

What's the equivalent of ServerSocketClientConnect / ClientSocketConnect and the other methods inside in a console app?


Solution

  • Delphi uses the same classes and non-visual components in console applications. You don't need to change what classes you use, but you do need to learn how to add things to units by typing them out manually instead of relying on the Delphi IDE to generate the code for you.

    You could create them yourself by using the steps below, or you could use a data-module already constructed while using a VCL Win32 GUI application containing non-visual components, within a console application.

    Steps:

    1. Add unit name that contains the class or component you want to use to the Uses clause.

    2. Construct the component as if it were a class:

        var
          aSomething:TSomething;
        begin
          aSomething := TSomething.Create(Parameter1,Parameter2);
        end;
      
    3. Remember to free it at the right place.

           aSomething.Free;
      

    However I have a hard time understanding why you would want to turn a chat server into a console application, other than as a toy, or an experiment. In real world use, you probably want a Win32 GUI, or else you want a completely non-visual service (an NT-style Service), which you can create, without changing the APPTYPE to console, but which could run without any GUI, nevertheless.

    The primary practical reason I can see for writing console applications is to write things which are useful from the command-line, such as build helper utilities, etc. Making a console application is in and of itself extremely easy. Start by creating a new empty console application, add a new empty pascal unit to it, make a main method, then add the unit names you want to the uses clause of the main-unit you are working on. That's all there is to it.

    You could keep using the server socket and client sockets you're currently using, although I would like to suggest you forget about the console app, unless you really need it, as all you're doing is making development and operation of your program more difficult for no real purpose. You don't have to change components or classes to use them in a console application, however, I should say that GUI or console alike, that the TTcpClient and TTcpServer components on the "Internet" tab in Delphi are not suitable for real use in any real world application. They are there for backwards compatibility and should be considered "out of date" and "no longer to be used in any serious way".

    Look at a real TCP/IP library like Indy, ICS, Synapse, or anything, anything at all, other than those components from the "Internet" page of the component palette, which are not suitable for real world use in any scale other than "toys".