Recent

Author Topic: can i make server?  (Read 68468 times)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #30 on: October 19, 2017, 02:44:52 pm »
oh okay i will start coding but i need to use TCP right?
so i just have to swap TUDPBlockSocket with TTCPBlockSocket?

also what do i have to for this?
 
Quote
sock.bind('127.0.0.1','12345');
(this is from the example you gave me)

the second one is port? and the first one is IP is it?
where do i find the ip address?

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: can i make server?
« Reply #31 on: October 19, 2017, 02:57:09 pm »
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:
Code: Pascal  [Select][+][-]
  1. program server;
  2.  
  3. uses
  4.   blcksock;
  5.  
  6. procedure HandleConnection(ASocket: TTCPBlockSocket);
  7. var
  8.   S: string;
  9. begin
  10.   WriteLn('Received client text:');
  11.   repeat
  12.     S := ASocket.RecvString(120000);
  13.     WriteLn(S);
  14.     ASocket.SendString('Ok' + CRLF);
  15.   until ASocket.lasterror <> 0;
  16. end;
  17.  
  18. var
  19.   ListenerSocket: TTCPBlockSocket;
  20.   ConnectionSocket: TTCPBlockSocket;
  21. begin
  22.   ListenerSocket := TTCPBlockSocket.Create;
  23.   ConnectionSocket := TTCPBlockSocket.Create;
  24.   ListenerSocket.CreateSocket;
  25.   ListenerSocket.setLinger(true,10);
  26.   ListenerSocket.bind('0.0.0.0','1500');
  27.   ListenerSocket.listen;
  28.   writeln('waiting');
  29.   repeat
  30.     if ListenerSocket.canread(1000) then
  31.     begin
  32.       WriteLn('Incoming connection');
  33.       ConnectionSocket := TTCPBlockSocket.Create; // multiple new ones
  34.       ConnectionSocket.Socket := ListenerSocket.accept;
  35.       WriteLn('Client connected on local port: ', ConnectionSocket.LocalSin.sin_port, ' ', ConnectionSocket.RemoteSin.sin_port);
  36.       HandleConnection(ConnectionSocket);
  37.       ConnectionSocket.CloseSocket;
  38.       ConnectionSocket.Free;
  39.       WriteLn('Connection closed');
  40.       WriteLn('Waiting for new connection');
  41.     end;
  42.   until false;
  43.  
  44.   ListenerSocket.Free;
  45.   writeln('done');
  46.   Readln;
  47. end.

Client:
Code: Pascal  [Select][+][-]
  1. program client;
  2.  
  3. uses
  4.   blcksock;
  5.  
  6. var
  7.   sock: TTCPBlockSocket;
  8.   buffer: string = '';
  9.  
  10. begin
  11.   sock := TTCPBlockSocket.Create;
  12.   sock.Connect('127.0.0.1', '1500');
  13.  
  14.   if sock.LastError <> 0 then
  15.   begin
  16.     writeLn('Could not connect to server.');
  17.     halt(1);
  18.   end;
  19.  
  20.   buffer := 'hello, this is a client';
  21.   repeat
  22.     sock.SendString(buffer + CRLF);
  23.     buffer := sock.RecvPacket(2000);
  24.     Write(buffer);
  25.     Readln(buffer);
  26.   until buffer = '';
  27.  
  28. 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.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #32 on: October 19, 2017, 02:58:54 pm »
Code: Pascal  [Select][+][-]
  1. sock.CreateSocket;
  2. sock.bind
  3.  sock.SendString
  4. sock.LastError

hi also i cannot find those methods in this website
http://synapse.ararat.cz/doc/help/blcksock.TTCPBlockSocket.html

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #33 on: October 19, 2017, 03:00:09 pm »
okay thank you :)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #34 on: October 19, 2017, 03:09:32 pm »
Code: Pascal  [Select][+][-]
  1. sock.SendString(buffer + CRLF);  

what is crlf?
Code: Pascal  [Select][+][-]
  1. buffer := sock.RecvPacket(2000);
and i don't understand this code

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: can i make server?
« Reply #35 on: October 19, 2017, 03:11:12 pm »
Code: Pascal  [Select][+][-]
  1. sock.SendString(buffer + CRLF);  

what is crlf?
Set you cursor on the CRLF and press Alt + Arrow up.
You'll jump to the definition of CRLF and as you can see it should be Carriage return (CR) and LineFeed (LF). Those are the characters for a new line in a console. RecvString also uses these characters to detect that it only needs to read one string (terminated by CR + LF).

Code: Pascal  [Select][+][-]
  1.      Method waits until data string is received. This string is terminated by
  2.      CR-LF characters. The resulting string is returned without this termination
  3.      (CR-LF)! If @link(ConvertLineEnd) is used, then CR-LF sequence may not be
  4.      exactly CR-LF. See @link(ConvertLineEnd) description. If no data is
  5.      received within TIMEOUT (in milliseconds) period, @link(LastError) is set
  6.      to WSAETIMEDOUT. You may also specify maximum length of reading data by
  7.      @link(MaxLineLength) property.}
  8.     function RecvString(Timeout: Integer): AnsiString; virtual;
  9.  

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #36 on: October 19, 2017, 03:12:44 pm »
Code: Pascal  [Select][+][-]
  1. sock.SendString(buffer + CRLF);  

what is crlf?
Code: Pascal  [Select][+][-]
  1. buffer := sock.RecvPacket(2000);
and i don't understand this code

Code: Pascal  [Select][+][-]
  1.  repeat
  2.     S := ASocket.RecvString(120000);
  3.     WriteLn(S);
  4.     ASocket.SendString('Ok' + CRLF);
  5.   until ASocket.lasterror <> 0;

does that mean repeat that until client connects the server?

Code: Pascal  [Select][+][-]
  1. ASocket.lasterror <> 0
what does that mean?


Thaddy

  • Hero Member
  • *****
  • Posts: 14364
  • Sensorship about opinions does not belong here.
Re: can i make server?
« Reply #37 on: October 19, 2017, 03:31:06 pm »
Code: Pascal  [Select][+][-]
  1. sock.SendString(buffer + CRLF);  

what is crlf?
Carriage return+Linefeed. Can be substituted x-platform by LineEnding;
Quote
Code: Pascal  [Select][+][-]
  1. buffer := sock.RecvPacket(2000);
and i don't understand this code

Code: Pascal  [Select][+][-]
  1.  repeat
  2.     S := ASocket.RecvString(120000);
  3.     WriteLn(S);
  4.     ASocket.SendString('Ok' + CRLF);
  5.   until ASocket.lasterror <> 0;

does that mean repeat that until client connects the server?
Yes. But it looks silly code to me  ;D.
Quote
Code: Pascal  [Select][+][-]
  1. ASocket.lasterror <> 0
what does that mean?
0 means no error.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: can i make server?
« Reply #38 on: October 19, 2017, 03:40:34 pm »
Code: Pascal  [Select][+][-]
  1. sock.SendString(buffer + CRLF);  
what is crlf?
Carriage return+Linefeed. Can be substituted x-platform by LineEnding;
No, it can't. Because Synapse actually expects CRLF when using RecvString() !!!
Code: Pascal  [Select][+][-]
  1.   s := RecvTerminated(Timeout, CRLF);

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: can i make server?
« Reply #39 on: October 19, 2017, 03:43:37 pm »
Code: Pascal  [Select][+][-]
  1.  repeat
  2.     S := ASocket.RecvString(120000);
  3.     WriteLn(S);
  4.     ASocket.SendString('Ok' + CRLF);
  5.   until ASocket.lasterror <> 0;

does that mean repeat that until client connects the server?
The connection is already established before calling HandleConnection (and this loop).
So RecvString(120000) waits (with a timeout) until a complete string (terminated by CRLF) is received.
This string is printed to the console and Ok+CRLF is send back.
This step is repeated until the client is disconnected.
In that case ASocket.lastError will be <> 0 (the lasterror will be the number for "connection lost" or something) and the loop ends.


shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #40 on: October 19, 2017, 11:25:04 pm »
Code: Pascal  [Select][+][-]
  1.  S := ASocket.RecvString(120000);
so that number is like timer interval?

also
Code: Pascal  [Select][+][-]
  1. var
  2.   ListenerSocket: TTCPBlockSocket;
  3.   ConnectionSocket: TTCPBlockSocket;  
why did you create two tcp?
Code: Pascal  [Select][+][-]
  1. ListenerSocket.setLinger(true,10);
  2.   ListenerSocket.bind('0.0.0.0','1500');
  3.   ListenerSocket.listen;    
  4. repeat
  5.     if ListenerSocket.canread(1000) then
  6.     begin    
  7.     ...          
and what does that linger do? also what is canread(1000) do why is ConnectionSocket created if canread(1000)?

the methods i'm asking are not showing on here so i'm asking you
http://synapse.ararat.cz/doc/help/blcksock.TTCPBlockSocket.html#Create

and how do you if client disconnected(or closed the program)?

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: can i make server?
« Reply #41 on: October 20, 2017, 01:39:02 pm »
Code: Pascal  [Select][+][-]
  1.  S := ASocket.RecvString(120000);
so that number is like timer interval?
No, an interval is something else. It's a timeout. It waits until it receives a complete string or 120000 milliseconds have past.

Quote
and what does that linger do?
Search Google for synapse setlinger.
Quote
Sets linger. Enabled linger means that the system waits another LINGER (in milliseconds) time for delivery of sent data. This function is only for stream type of socket! (TCP)

Quote
also what is canread(1000) do why is ConnectionSocket created if canread(1000)?
If canread(1000) is true then there is something received.
With ListenerSocket.accept there is a NEW socket created and the connection is passed to that socket.
That means that the ListenerSocket can keep on listening on the same port for multiple incoming connections (although because we haven't used threads yet, the main program is "blocking" that).
But that's the reason we have a ListenerSocket and a ConnectionSocket.

Quote
and how do you if client disconnected(or closed the program)?
In HandleConnection there is a loop. That loop ends when ASocket.lasterror <> 0. Because when the client disconnects or ends the program the connection is severed. And you get a LastError <> 0 on the server.

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: can i make server?
« Reply #42 on: October 20, 2017, 02:01:38 pm »
In your client you have this:
Code: Pascal  [Select][+][-]
  1.   buffer := edit3.text;//'hello, this is a client';
  2.   repeat
  3.     sock.SendString(buffer + CRLF);
  4.     buffer := sock.RecvPacket(2000);
  5.     memo1.lines.add(buffer);
  6.   until buffer = '';
So you are sending the buffer and waiting for a string back.

But in the server you are not sending anything back.

If the purpose is that you enter something in edit3 and want to send it you can do this in the client:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. begin
  3.   sock := TTCPBlockSocket.Create;
  4.   sock.Connect(edit1.text, edit2.text);
  5.  
  6.   if sock.LastError <> 0 then
  7.   begin
  8.     memo1.lines.add('Could not connect to server.');
  9.     //halt(1);
  10.   end;
  11.  
  12.    if sock.LastError = 0 then
  13.   begin
  14.     memo1.lines.add('Connected');
  15.     //halt(1);
  16.   end;
  17.  
  18. end;
  19.  
  20. procedure TForm1.Edit3KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  21. begin
  22.   if Key = 13 { or VK_RETURN  and add lcltype in your uses }  then
  23.   begin
  24.     buffer := edit3.text;//'hello, this is a client';
  25.     sock.SendString(buffer + CRLF);
  26.     memo1.lines.add(buffer);
  27.   end;
  28. end;


shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #43 on: October 20, 2017, 02:11:12 pm »
Code: Pascal  [Select][+][-]
  1. But in the server you are not sending anything back.
how can i send anything back in the server?

also it works good when i first send the message but after that the program is not responding, how can i fix this problem?

rvk

  • Hero Member
  • *****
  • Posts: 6162
Re: can i make server?
« Reply #44 on: October 20, 2017, 02:16:00 pm »
how can i send anything back in the server?
You commented out the SendStr in the server.
That's the line that sends Ok back to the client.

Quote
also it works good when i first send the message but after that the program is not responding, how can i fix this problem?
The client-program is not responding anymore because it keeps trying to read from the connection.

The server-program is not responding because the client isn't sending anything anymore. It also doesn't disconnect but waits for a server-response. So you have a dead-lock. Both are waiting on each other. That's why the server needs to respond.

If you take my code for the client, then the server does not need to respond. The client can just keep sending strings to the server.


 

TinyPortal © 2005-2018