For the server you can just use 0.0.0.0.
The it will bind to every incoming IP-address possible.
But for the client you need to know the exact IP-address.
For your internal network you can see that if you type ipconfig in a cmd-console.
For the external internet you need to find out your external ip address (for example by going to myip.nl).
For just testing on your own computer (with client and server on the same computer) you can use 127.0.0.1 in the client.
The following code is by a long shot not a messaging system yet but it gives you an idea of a simple connection-example.
Server:
program server;
uses
blcksock;
procedure HandleConnection(ASocket: TTCPBlockSocket);
var
S: string;
begin
WriteLn('Received client text:');
repeat
S := ASocket.RecvString(120000);
WriteLn(S);
ASocket.SendString('Ok' + CRLF);
until ASocket.lasterror <> 0;
end;
var
ListenerSocket: TTCPBlockSocket;
ConnectionSocket: TTCPBlockSocket;
begin
ListenerSocket := TTCPBlockSocket.Create;
ConnectionSocket := TTCPBlockSocket.Create;
ListenerSocket.CreateSocket;
ListenerSocket.setLinger(true,10);
ListenerSocket.bind('0.0.0.0','1500');
ListenerSocket.listen;
writeln('waiting');
repeat
if ListenerSocket.canread(1000) then
begin
WriteLn('Incoming connection');
ConnectionSocket := TTCPBlockSocket.Create; // multiple new ones
ConnectionSocket.Socket := ListenerSocket.accept;
WriteLn('Client connected on local port: ', ConnectionSocket.LocalSin.sin_port, ' ', ConnectionSocket.RemoteSin.sin_port);
HandleConnection(ConnectionSocket);
ConnectionSocket.CloseSocket;
ConnectionSocket.Free;
WriteLn('Connection closed');
WriteLn('Waiting for new connection');
end;
until false;
ListenerSocket.Free;
writeln('done');
Readln;
end.
Client:
program client;
uses
blcksock;
var
sock: TTCPBlockSocket;
buffer: string = '';
begin
sock := TTCPBlockSocket.Create;
sock.Connect('127.0.0.1', '1500');
if sock.LastError <> 0 then
begin
writeLn('Could not connect to server.');
halt(1);
end;
buffer := 'hello, this is a client';
repeat
sock.SendString(buffer + CRLF);
buffer := sock.RecvPacket(2000);
Write(buffer);
Readln(buffer);
until buffer = '';
end.
The client makes the connection to the server (on local IP 127.0.0.1 at the moment) and sends a string "hello". The server accepts the connection and passes it to a ConnectionSocket. This is done because later on you would need to put the ConnectionSocket-code in a separate thread. The server prints out the received string and just sends back "Ok".
You could change the "Ok" to a string you read from the keyboard but at that point the connection is "blocked" so if the clients wants to send another string it can't because the connection isn't listening.
But play with this example first.