Recent

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

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #45 on: October 20, 2017, 02:24:12 pm »
Quote
You commented out the SendStr in the server.
That's the line that sends Ok back to the client.
i commented out because it added the line every second, how can i make it added just once?

Quote
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.

yes i took your code for the client and the server was not responding. and sometimes the server is just not responding even before client program connects. so i guess there's something wrong with server program?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: can i make server?
« Reply #46 on: October 20, 2017, 02:28:29 pm »
Quote
You commented out the SendStr in the server.
That's the line that sends Ok back to the client.
i commented out because it added the line every second, how can i make it added just once?
Look again at this code:
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 = '';
It will put your text in buffer.
After that it TRIES to receive a string from the server.
If you uncommented that Ok line you'll receive Ok straight away.
Directly after that you AGAIN send the buffer to the server.
If you uncommented that Ok line you'll receive Ok straight away.
Directly after that you AGAIN send the buffer to the server.
If you uncommented that Ok line you'll receive Ok straight away.
Directly after that you AGAIN send the buffer to the server.
If you uncommented that Ok line you'll receive Ok straight away.
Directly after that you AGAIN send the buffer to the server.
If you uncommented that Ok line you'll receive Ok straight away.
Directly after that you AGAIN send the buffer to the server.
If you uncommented that Ok line you'll receive Ok straight away.
Directly after that you AGAIN send the buffer to the server.
If you uncommented that Ok line you'll receive Ok straight away.
Directly after that you AGAIN send the buffer to the server.
If you uncommented that Ok line you'll receive Ok straight away.
Directly after that you AGAIN send the buffer to the server.
If you uncommented that Ok line you'll receive Ok straight away.
Directly after that you AGAIN send the buffer to the server.

So why are you sending the same test (in buffer) over and over again?
No wonder you get Ok from the server each second because you send that buffer each second.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #47 on: October 20, 2017, 02:47:04 pm »
oh so 'ok' is not meant to be shown on the memo1?
what does this do then?
Code: Pascal  [Select][+][-]
  1. ASocket.SendString('Ok' + CRLF);
also what does this do?
Code: Pascal  [Select][+][-]
  1. until buffer = '';

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: can i make server?
« Reply #48 on: October 20, 2017, 02:56:47 pm »
oh so 'ok' is not meant to be shown on the memo1?
That's up to you.
Quote
what does this do then?
Code: Pascal  [Select][+][-]
  1. ASocket.SendString('Ok' + CRLF);
That sends a string Ok from the server to the client.
In the client you had a line to receive data from the server after you send a string to the server.
Not nice to not send a string then.

But... I also gave you code where the server does not need to respond at all. In that case you can leave the Ok line out.

Quote
also what does this do?
Code: Pascal  [Select][+][-]
  1. until buffer = '';
It ends the loop if buffer-string is empty. You should know this by now  >:D

But the whole idea of that loop that you have is flawed.
What does that loop do???
It just keeps sending the same buffer to the server.
Why !!

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #49 on: October 20, 2017, 03:41:56 pm »
Code: Pascal  [Select][+][-]
  1. But the whole idea of that loop that you have is flawed.
  2. What does that loop do???
  3. It just keeps sending the same buffer to the server.
  4. Why !!

Code: Pascal  [Select][+][-]
  1. until buffer = '';
does that mean edit3.text is ''?

also the server works really good if i open the project and compile it but if u just open the application it's laggy and not responding

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: can i make server?
« Reply #50 on: October 20, 2017, 03:46:37 pm »
Code: Pascal  [Select][+][-]
  1. until buffer = '';
does that mean edit3.text is ''?
No.

Can you check every line below and put a comment of what's happening for me?
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 = '';
Especially what happens to the variable buffer.

Quote
also the server works really good if i open the project and compile it but if u just open the application it's laggy and not responding
Did you try to change the client as I did/said?

I already said you need to throw that whole loop away and just use the TEdit to send the string.
(look at my code I posted previously)

(My intention was to teach you about threads after this, because you would need it, but I don't think you're ready for it)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #51 on: October 20, 2017, 04:11:24 pm »
Code: Pascal  [Select][+][-]
  1. buffer := edit3.text;//'hello, this is a client';
  2.   repeat
  3.     sock.SendString(buffer + CRLF); //it sends buffer to server
  4.     buffer := sock.RecvPacket(2000);//it waits until the server receive the buffer?
  5.     memo1.lines.add(buffer); //the buffer will be added to memo
  6.   until buffer = '';// ends the loop

i think that's very wrong i still don't get what crlf do, and i don't understand why buffer is equal to sock.RecvPacket(2000)

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: can i make server?
« Reply #52 on: October 20, 2017, 04:16:30 pm »
i think that's very wrong i still don't get what crlf do, and i don't understand why buffer is equal to sock.RecvPacket(2000)
We already told you what CRLF stands for. It's just two extra characters #13 and #10 to signal to the receiver the end of the string is reached.

And with  buffer := sock.RecvPacket(2000); you try to read a string from the connection (send by the other party).
I don't know how else to explain it.

It's just like you're asking what I := 1 does.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #53 on: October 20, 2017, 04:32:44 pm »
wait but that code is from server right?
why does it have to read a string the server?
Code: Pascal  [Select][+][-]
  1.  memo1.lines.add(buffer);

so the buffer in that code is the buffer from the server? not edit3.text?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: can i make server?
« Reply #54 on: October 20, 2017, 04:37:00 pm »
wait but that code is from server right?
No, in the server you used S as string variable to receive the string in.
In the client you used buffer.

Quote
why does it have to read a string the server?
It doesn't. If the server is not sending any string back, you also shouldn't try to read a string from the connection in the client.

Quote
Code: Pascal  [Select][+][-]
  1.  memo1.lines.add(buffer);

so the buffer in that code is the buffer from the server? not edit3.text?
Yes, potentially, if the server were to send a string (i.e. that Ok line). But if you commented that out, there is no string send from the server so buffer would be empty.

In that case the loop would end and the client does nothing anymore.
And the connection stays because the client doesn't end the connection, the program of the client just sits there doing nothing.

That's why I said the whole loop-thing is flawed and I showed you another way to do it.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #55 on: October 20, 2017, 04:47:42 pm »
Oh okay i get it now

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit3KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  2. begin
  3.   if Key = 13 { or VK_RETURN  and add lcltype in your uses }  then
  4.   begin
  5.     buffer := edit3.text;//'hello, this is a client';
  6.     sock.SendString(buffer + CRLF);
  7.     memo1.lines.add(buffer);
  8.   end;
  9. end;          
that is the code from you and if i want to get the message from the server i just need to put
Code: Pascal  [Select][+][-]
  1. buffer := sock.RecvPacket(2000);
after sock.sendstring?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: can i make server?
« Reply #56 on: October 20, 2017, 04:49:37 pm »
Yep.
sock.SendString(buffer + CRLF); and buffer := sock.RecvPacket(2000); are the two most important parts of sending and receiving the strings.

You also need to know that RecvPacket is blocking. It waits the timeout milliseconds before it determines there is no string.

If you don't know if the server has send a string you can use Sock.CanRead(1000). If will be true if there is data on the line and false if there is nothing. In that case you can build code that does not block if you want to see if there is a string or not.
« Last Edit: October 20, 2017, 04:51:52 pm by rvk »

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #57 on: October 20, 2017, 05:04:22 pm »
oh okay thank you btw my server is still not responding when i'm opening the application, but it works when i open the project and then compile. does yours work well when you open the application?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: can i make server?
« Reply #58 on: October 20, 2017, 05:16:30 pm »
oh okay thank you btw my server is still not responding when i'm opening the application, but it works when i open the project and then compile. does yours work well when you open the application?
You mean, it's not responding even before you execute Button1Click?

I don't understand what you mean by "opening the application".

O, maybe I understand you now...
You mean the program becomes sluggish AFTER pressing the button (If so YOU SHOULD HAVE TOLD SO, communication is everything).
Look at these lines:
Code: Pascal  [Select][+][-]
  1.   repeat
  2.     if ListenerSocket.canread(1000) then
  3.     begin
  4.       memo1.lines.add('Incoming connection');
  5.       ConnectionSocket := TTCPBlockSocket.Create; // multiple new ones
  6.       ConnectionSocket.Socket := ListenerSocket.accept;
  7.       //memo1.lines.add('Client connected on local port: ', ConnectionSocket.LocalSin.sin_port, ' ', ConnectionSocket.RemoteSin.sin_port);
  8.       HandleConnection(ConnectionSocket);
  9.       ConnectionSocket.CloseSocket;
  10.       ConnectionSocket.Free;
  11.       memo1.lines.add('Connection closed');
  12.       memo1.lines.add('Waiting for new connection');
  13.     end;
  14.   until false;
So when pressing the button this loop executes.
But it does nothing else and never finishes.
BUT, and this is a BIG BUT, you also don't allow the main thread to do anything.
This is a long-running loop and blocks your entire program.

To do a quick fix for that you can add Application.ProcessMessages; inside the loop.
Even then your program will be slugisch because the CanRead(1000) will pause the program for one second (blocking everything).

You can change it like this:
Code: Pascal  [Select][+][-]
  1.   repeat
  2.     Application.ProcessMessages;
  3.     if ListenerSocket.canread(100) then // <--- use 100 here instead of 1000
  4.     begin
  5.       memo1.lines.add('Incoming connection');
  6.       ConnectionSocket := TTCPBlockSocket.Create; // multiple new ones
  7.       ConnectionSocket.Socket := ListenerSocket.accept;
  8.       //memo1.lines.add('Client connected on local port: ', ConnectionSocket.LocalSin.sin_port, ' ', ConnectionSocket.RemoteSin.sin_port);
  9.       HandleConnection(ConnectionSocket);
  10.       ConnectionSocket.CloseSocket;
  11.       ConnectionSocket.Free;
  12.       memo1.lines.add('Connection closed');
  13.       memo1.lines.add('Waiting for new connection');
  14.     end;
  15.   until false;

Actually with good programming you should keep the runtime of a button-event low because you could click the button a second time and then you have duplicate code which executes at the same time. The quickest dirtiest fix for this is disabling the button at the beginning and enabling it at the end of the event-procedure.

Later on, you'll see that we need to hand off the connection to a thread and then the button-event procedure runs quickly.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #59 on: October 20, 2017, 05:30:05 pm »
oh actually i was talking about the application
server.exe
if i just open that application it gets laggy or not responding but if i open the project.ipr file and compile it, it works really well

 

TinyPortal © 2005-2018