Recent

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

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: can i make server?
« Reply #90 on: October 22, 2017, 06:46:05 am »
You keep on listening in the repeat-loop and when listenersocket.canread is true again there should be a new incoming connection to which you can connect again.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #91 on: October 22, 2017, 07:30:01 am »
Code: Pascal  [Select][+][-]
  1.  if ListenerSocket.canread(100) then // <--- use 100 here instead of 1000
  2.     begin
  3.       SetLength(Connections, Length(Connections) + 1);
  4.       Connections[Length(Connections)].Sock := TTCPBlockSocket.Create;
  5.       Connections[Length(Connections)].Sock.Socket := ListenerSocket.Accept;
  6.       Timer1.enabled:=true;      

so like that?

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: can i make server?
« Reply #92 on: October 22, 2017, 08:16:52 am »
Yes. But oops. Connections[Length(Connections)] should be Connections[Length(Connections) -1]
Or use
Connections[High(Connections)]
High(array) gives the upper index and is the same as Length (array)-1

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #93 on: October 22, 2017, 08:21:02 am »
Code: Pascal  [Select][+][-]
  1.  if ListenerSocket.canread(100)
wait but wouldnt it create more than i want if the listener can read only 1 client for multiple times?
how does the listener know if it's different client?

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: can i make server?
« Reply #94 on: October 22, 2017, 08:27:27 am »
Listener listens for incoming connections. When it catches one, it passes it to a new socket (which is on a different internal port). After that listener is free to listen to new incoming connections again. So when there a new listener.canread again, it is a new connection again.

So listener.canread and connection.canread are two different things. For the listener it means a new connection and for connection it means more data.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #95 on: October 22, 2017, 09:42:19 am »
button click
Code: Pascal  [Select][+][-]
  1. repeat
  2.     Application.ProcessMessages;
  3.  
  4.     if ListenerSocket.canread(100) then // <--- use 100 here instead of 1000
  5.     begin
  6.       edit3.enabled:=true;
  7.       button2.enabled:=true;
  8.  
  9.       SetLength(Connections, Length(Connections) + 1);
  10.       Connections[high(Connections)].Sock := TTCPBlockSocket.Create;
  11.       Connections[high(Connections)].Sock.Socket := ListenerSocket.Accept;
  12.       memo1.lines.add('');
  13.       memo1.lines.add('Incoming connection');
  14.       if Connections[high(connections)].sock.lasterror = 0 then Timer1.enabled:=true;  

timer
Code: Pascal  [Select][+][-]
  1. for i := 0 to high(connections) do
  2.   begin
  3.  
  4.       if connections[i].sock.lasterror <> 0 then
  5.         begin
  6.           if length(connections) = 1 then
  7.           begin
  8.           edit3.clear;
  9.           edit3.enabled:=false;
  10.           button2.enabled:=false;
  11.           timer1.enabled:=false;
  12.           ConnectionS[i].sock.CloseSocket;
  13.           ConnectionS[i].sock.Free;
  14.           memo1.lines.add('Clinet ' + inttostr(i+1) + 'disconnected');
  15.            memo1.lines.add( '------------------------------------------------------------------------');
  16.           memo1.lines.add('Connection closed');
  17.           memo1.lines.add( FormatDateTime('DDDD, dd MMMM YYYY HH:MM:SS', Now));
  18.           memo1.lines.add('Waiting for new connection');
  19.           exit; // <- we can exit because we closed the connection
  20.         end
  21.        else
  22.         begin
  23.           //edit3.clear;
  24.           //edit3.enabled:=false;
  25.           //button2.enabled:=false;
  26.           //timer1.enabled:=false;
  27.           ConnectionS[i].sock.CloseSocket;
  28.           ConnectionS[i].sock.Free;
  29.           memo1.lines.add('*Clinet ' + inttostr(i+1) + ' disconnected');
  30.           // memo1.lines.add( '------------------------------------------------------------------------');
  31.           //memo1.lines.add('Connection closed');
  32.           //memo1.lines.add( FormatDateTime('DDDD, dd MMMM YYYY HH:MM:SS', Now));
  33.           //memo1.lines.add('Waiting for new connection');
  34.           //exit; // <- we can exit because we closed the connection
  35.         end;
  36.     end;
  37.  
  38.     if ConnectionS[i].sock.canread(100) then
  39.       begin
  40.         S := ConnectionS[i].sock.RecvString(120000);
  41.         memo1.lines.add('client '+inttostr(i+1)+ ':' +S);
  42.       end;
  43.   end;                  

the server gets message from the clients but when i close one of the client program i get the sig error message.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #96 on: October 22, 2017, 09:53:01 am »
oh wait is it because i didn't do the setlength?

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: can i make server?
« Reply #97 on: October 22, 2017, 09:58:19 am »
That already look really good  8)

In the timer I would move the "if connections[i].sock.lasterror <> 0 then" block after the "if ConnectionS[i].sock.canread(100)" block so that first the if canread is done. And if that has an error the "sock.lasterror <> 0" will catch it and close the connection.

the server gets message from the clients but when i close one of the client program i get the sig error message.
Yes, that's because you correctly dispose of the socket with sock.Free; when the connection is lost. But now you still have a Connections[i].sock with a invalid socket. That's why you get the error. The second pass of the timer checks that invalid connection.

Remember how to do the removal of an item from the array? You don't need Connections[i] anymore so you have to remove it from the array and move all socket after it one up in the array and make the length of the array 1 less.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #98 on: October 22, 2017, 10:07:03 am »
so like this?
Code: Pascal  [Select][+][-]
  1. connections[i-1] = connections.sock[i]
  2. setlength(connections, length(connections)-1)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #99 on: October 22, 2017, 10:12:07 am »
Code: Pascal  [Select][+][-]
  1. ConnectionS[i].sock.CloseSocket;
  2.           ConnectionS[i].sock.Free;
  3.           for j:= i+1 to high(connections) do
  4.           begin
  5.               connections[j-1]:=connections(j)
  6.           end;
  7.           setlength(connections, length(connections) -1);

i think this is the correct one?

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: can i make server?
« Reply #100 on: October 22, 2017, 10:24:28 am »
i think this is the correct one?
Yep, that last one looks correct to me.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #101 on: October 22, 2017, 10:28:39 am »
Code: Pascal  [Select][+][-]
  1. ConnectionS[i].sock.CloseSocket;
  2.           ConnectionS[i].sock.Free;
  3.           if i <> high(connections) then
  4.           begin
  5.             for j:= i+1 to high(connections) do
  6.               begin
  7.                   connections[j-1]:=connections[j]
  8.               end;
  9.           end;
  10.           setlength(connections, length(connections) -1);    

it works if i don't close the high(connections) program. so lets say 3 client programs are open and if i close the 2nd client program, there's no error but if i close the 3rd one i get error

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: can i make server?
« Reply #102 on: October 22, 2017, 10:34:28 am »
Yes. In your loop you shouldn't go to high(connections) because high(connections) + 1 doesn't exist. So j+1 references a non existing array index. You need to go to high(connections) -1

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #103 on: October 22, 2017, 10:36:47 am »
Code: Pascal  [Select][+][-]
  1. connections[j-1]:=connections[j]
yes that's why i did j-1

rvk

  • Hero Member
  • *****
  • Posts: 6110
Re: can i make server?
« Reply #104 on: October 22, 2017, 10:39:30 am »
Did you move the check for lasterror to the end?

What's the complete timerprocedure?

 

TinyPortal © 2005-2018