t e m p o r a l 
 d o o r w a y 

Basic Socket Programming

 

Introduction

The following are two separate programs which use sockets to communicate. The hosts and socket port numbers are hard coded, but it would be simple to configure this from registry entries or a user interface.

The Server

The server program uses TServerSocket to recieve, display, and echo a text message from the client.

Here is the necessary .dfm file:

object RequestMemo: TMemo
   Left = 0
   Top = 0
   Width = 374
   Height = 199
   Align = alClient
   TabOrder = 0
end
object StatusBar: TStatusBar
   Left = 0
   Top = 199
   Width = 374
   Height = 19
   Panels = <>
   SimplePanel = True
end
object ServerSocket: TServerSocket
   Active = True
   Port = 80
   ServerType = stNonBlocking
   OnClientConnect = ServerSocketClientConnect
   OnClientRead = ServerSocketClientRead
   OnClientWrite = ServerSocketClientWrite
   Left = 168
   Top = 80
end

And the .cpp file event handlers...

void __fastcall TMainForm::ServerSocketClientRead(TObject *Sender, TCustomWinSocket *Socket)
{
    StatusBar->SimpleText = "Client Read";
    RequestMemo->Text = Socket->ReceiveText();
}

void __fastcall TMainForm::ServerSocketClientConnect(TObject *Sender, TCustomWinSocket *Socket)
{
    StatusBar->SimpleText = "Connect";
};

void __fastcall TMainForm::ServerSocketClientWrite(TObject *Sender, TCustomWinSocket *Socket)
{
    Socket->SendText(RequestMemo->Text);
    StatusBar->SimpleText = "Client Write";
}

Socket Client

The following is the .dfm

object TopPanel: TPanel
   Left = 0
   Top = 0
   Width = 601
   Height = 226
   Align = alClient
   TabOrder = 0
   object DisplayMemo: TMemo
      Left = 8
      Top = 0
      Width = 585
      Height = 185
      ScrollBars = ssVertical
      TabOrder = 0
   end
   object SendButton: TButton
      Left = 8
      Top = 191
      Width = 75
      Height = 25
      Caption = 'Send'
      TabOrder = 1
      OnClick = SendButtonClick
   end
   object SendEdit: TEdit
      Left = 88
      Top = 192
      Width = 505
      Height = 24
      TabOrder = 2
   end
end
object StatusBar: TStatusBar
   Left = 0
   Top = 226
   Width = 601
   Height = 19
   Panels = <>
   SimplePanel = True
end
object ClientSocket: TClientSocket
   Active = False
   ClientType = ctNonBlocking
   Host = 'localhost'
   Port = 80
   OnConnect = ClientSocketConnect
   OnRead = ClientSocketRead
   OnWrite = ClientSocketWrite
   Left = 344
   Top = 88
end

and the .cpp event handlers...

void __fastcall TMainForm::SendButtonClick(TObject *Sender)
{
    ClientSocket->Active:= true;
}

void __fastcall TMainForm::ClientSocketWrite(TObject *Sender, TCustomWinSocket *Socket)
{
    StatusBar->SimpleText:= "Write";
    Socket->SendText(SendEdit->Text)
}

void __fastcall TMainForm::ClientSocketRead(TObject *Sender, TCustomWinSocket *Socket)
{

    StatusBar->SimpleText:= "Read";
    DisplayMemo->Text:= Socket->ReceiveText;
    ClientSocket->Active:= false;
}

void __fastcall TMainForm::ClientSocketConnect(TObject *Sender,TCustomWinSocket *Socket)
{
    StatusBar->SimpleText:= "Connected";
}

Conclusion

The above program is extremely simple, but does the job - it sends text between a socket server and client and back again. Creating programs like these as testing baselines is essential to setting up and debugging networked software, since it can be used to verify that the network and socket layers are working correctly if your more complex application fails. Indeed, you can set the port numbers to correspond to that used by your more complex application to verify that the port itself is not in use or otherwise inaccessible.

Copyright © 2004 by Mark Cashman (unless otherwise indicated), All Rights Reserved