Recent

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

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #105 on: October 22, 2017, 10:44:06 am »
here
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2. var
  3.   s:string;
  4.   i, j: Integer;
  5. begin
  6.  
  7.   // and this part is again the same as in the client
  8.   for i := 0 to high(connections) do
  9.   begin
  10.     Application.ProcessMessages;
  11.  
  12.      if ConnectionS[i].sock.canread(100) then
  13.       begin
  14.         S := ConnectionS[i].sock.RecvString(120000);
  15.         memo1.lines.add('client '+inttostr(i+1)+ ' : ' +S);
  16.       end;
  17.  
  18.       if connections[i].sock.lasterror <> 0 then
  19.         begin
  20.           if length(connections) <= 1 then
  21.           begin
  22.           edit3.clear;
  23.           edit3.enabled:=false;
  24.           button2.enabled:=false;
  25.           timer1.enabled:=false;
  26.  
  27.           ConnectionS[i].sock.CloseSocket;
  28.           ConnectionS[i].sock.Free;
  29.  
  30.           setlength(connections, 0);
  31.  
  32.           memo1.lines.add('Clinet ' + inttostr(i+1) + 'disconnected');
  33.            memo1.lines.add( '------------------------------------------------------------------------');
  34.           memo1.lines.add('Connection closed');
  35.           memo1.lines.add( FormatDateTime('DDDD, dd MMMM YYYY HH:MM:SS', Now));
  36.           memo1.lines.add('Waiting for new connection');
  37.           exit; // <- we can exit because we closed the connection
  38.         end
  39.        else
  40.         begin
  41.           //edit3.clear;
  42.           //edit3.enabled:=false;
  43.           //button2.enabled:=false;
  44.           //timer1.enabled:=false;
  45.           ConnectionS[i].sock.CloseSocket;
  46.           ConnectionS[i].sock.Free;
  47.           if i <> high(connections) then
  48.           begin
  49.             for j:= i+1 to high(connections) do
  50.               begin
  51.                   connections[j-1]:=connections[j]
  52.               end;
  53.           end;
  54.           setlength(connections, length(connections) -1);
  55.  
  56.           memo1.lines.add('*Clinet ' + inttostr(i+1) + ' disconnected');
  57.           // memo1.lines.add( '------------------------------------------------------------------------');
  58.           //memo1.lines.add('Connection closed');
  59.           //memo1.lines.add( FormatDateTime('DDDD, dd MMMM YYYY HH:MM:SS', Now));
  60.           //memo1.lines.add('Waiting for new connection');
  61.           //exit; // <- we can exit because we closed the connection
  62.         end;
  63.     end;
  64.   end;
  65. end;    

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: can i make server?
« Reply #106 on: October 22, 2017, 10:51:21 am »
You are still in the outer loop of high (connections) for I. I'm not sure if that is remembered.

You can wrap everything inside in an if I  <= high (connections)

Otherwise im not sure what's wrong and have to test it but i haven't much time today.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #107 on: October 22, 2017, 11:12:30 am »
Code: Pascal  [Select][+][-]
  1. else
  2.         begin
  3.           //edit3.clear;
  4.           //edit3.enabled:=false;
  5.           //button2.enabled:=false;
  6.           //timer1.enabled:=false;
  7.  
  8.           if i <= high(connections) then
  9.           begin
  10.           ConnectionS[i].sock.CloseSocket;
  11.           ConnectionS[i].sock.Free;
  12.             for j:= i+1 to high(connections) do
  13.               begin
  14.                   connections[j-1]:=connections[j]
  15.               end;
  16.            setlength(connections, length(connections) -1);
  17.           end;
  18.                                                                  
like this?

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #108 on: October 22, 2017, 11:17:23 am »
it's not working but i will try to work it on it later

i have a question

so now if i open 1 server and 2 clients, the client 1 can get message from server but not from client 2 and client 2 can not get message from 1.

to recieve the message from client, should i make array of tcp in client program as well?

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: can i make server?
« Reply #109 on: October 22, 2017, 10:00:01 pm »
like this?
Yes, you could do it like this.
But I have been thinking and want you to do it another way.

When you manipulate the length of an array within a loop that checks that array you can better use a while loop. Something like this:
Code: Pascal  [Select][+][-]
  1. var
  2.   I: Integer;
  3. begin
  4.   I := 0;
  5.   while I <= High(Array) do
  6.   begin
  7.     // check messages for I
  8.     // do that here
  9.  
  10.     // if Error remove item from array
  11.     if LastError <> 0 then
  12.     begin
  13.       Continue; // we don't need to increment so directory continue the loop
  14.     end;
  15.     Inc(I);
  16.   end;
This is because when you remove I from the array, and you move everything up one, you still need to check I again because you just moved everything up. You can do this in a while loop by using continue. It will go back in the loop without incrementing I.

so now if i open 1 server and 2 clients, the client 1 can get message from server but not from client 2 and client 2 can not get message from 1.

to recieve the message from client, should i make array of tcp in client program as well?
At the moment you can only communicate from server to client and back. You can't communicate between clients (I asked you and you said only communication from client to server was needed).

If you want to communicate between clients you could do that two ways.
One: You could establish a direct connection between clients. But you probably don't know the IPs of the client. Also, if you want to do that you need to open up a port on the router if the clients are on the world wide internet. I don't think you wanted to do that for the clients, do you?

Two: You already should open up the port on your router to your server if you want a client on the internet to connect to your server. But after that the server could relay messages from client to client. For example if the client sends a message "for client2: Hello client 2", you could check the beginning of a string on the server and if it says "for client2:" you send that message to client 2 on behalf of client1.

That way you are the only one who needs to change something in your router (port forwarding) and all the clients don't have to do anything.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #110 on: October 22, 2017, 11:19:34 pm »
oh okay thank you  :)
Quote
Also, if you want to do that you need to open up a port on the router if the clients are on the world wide internet.
wait i can actually do that? is that possible?


rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: can i make server?
« Reply #111 on: October 22, 2017, 11:39:24 pm »
Quote
Also, if you want to do that you need to open up a port on the router if the clients are on the world wide internet.
wait i can actually do that? is that possible?
Well, if you can't, you can't reach the server with the client from the internet.

You said the client was on the internet and not on the local network. If that's true you NEED to open up a port (for example 1500) and forward that port to your local IP (for example 192.168.1.44). The client needs to connect to your external internet IP otherwise the whole thing won't work on the internet.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #112 on: October 22, 2017, 11:42:47 pm »
so i the client using other internet can connect to my server?

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #113 on: October 22, 2017, 11:43:15 pm »
if so do i have to write the code all over again?

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: can i make server?
« Reply #114 on: October 22, 2017, 11:45:40 pm »
so i the client using other internet can connect to my server?
You said the client was on the internet (on another network than your local network).
If this is nog the case then you don't have to do anything and don't have to forward ports.

if so do i have to write the code all over again?
No, if you want to connect a client from the internet you just need to enter your external IP of the server, and your server will just respond (IF you have forwarded the needed port from the router to your local IP).

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #115 on: October 22, 2017, 11:49:39 pm »
so if the client on another network, they can just put my ip address and connect my server?

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: can i make server?
« Reply #116 on: October 23, 2017, 12:01:33 am »
so if the client on another network, they can just put my ip address and connect my server?
If they put your EXTERNAL IP in, AND you forward the used port on the router to your INTERNAL IP, yes, then they can connect to your server.


shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #117 on: October 23, 2017, 12:11:06 am »
what do you mean by external ip? is it different to my normal ip?

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: can i make server?
« Reply #118 on: October 23, 2017, 12:19:25 am »
what do you mean by external ip? is it different to my normal ip?
Yes, you have an internal IP. Normally this is in the range of 192.168.1.xxx or 10.0.0.xxx on your local network. Users from outside your network can't connect to your internal IP. For users from outside your network to reach your internal ip address you need to forward the ports you want to your internal ip.

Your external ip address is the ip address your router gets from your internet-provider. It can be anything.

If you go to http://www.myip.nl (or some other site which gives you your external info) you can view your external ip.

If your external ip changes a lot (for example if you have a dial in via phone-line (very rare these days) you can map your ip to a hostname with services like no-ip of duckdns.

In that case the client doesn't need to enter 83.234.12.234 or some weird number (which is hard to remember), but they can enter shs.duckdns.org (or other chosen subdomain) and the computer translates this automatically to your 83.234.12.234 number.

Apparently you don't do a lot of gaming where port forwarding is done on a regular basis.

For more information how to forward a port on your router (which is different for every router) you can go to https://portforward.com/ (which also shows your external IP in the upper-right corner).


shs

  • Sr. Member
  • ****
  • Posts: 310
Re: can i make server?
« Reply #119 on: October 23, 2017, 02:25:40 am »
Code: Pascal  [Select][+][-]
  1. begin
  2.   I := 0;
  3.   while I <= High(connections) do
  4.   begin
  5.   Application.ProcessMessages;
  6.  
  7.      if ConnectionS[i].sock.canread(100) then
  8.       begin
  9.         S := ConnectionS[i].sock.RecvString(120000);
  10.         memo1.lines.add('client '+inttostr(i+1)+ ' : ' +S);
  11.         for j:= 0 to high(connections) do
  12.         begin
  13.           if j <> i then  connections[j].sock.SendString('client '+inttostr(i+1)+ ' : ' +s + crlf);
  14.         end;
  15.     // check messages for I
  16.     // do that here
  17.  
  18.     // if Error remove item from array
  19.      if connections[i].sock.lasterror <> 0 then
  20.         begin
  21.           if length(connections) = 1 then
  22.           begin
  23.           edit3.clear;
  24.           edit3.enabled:=false;
  25.           button2.enabled:=false;
  26.           timer1.enabled:=false;
  27.  
  28.           ConnectionS[i].sock.CloseSocket;
  29.           ConnectionS[i].sock.Free;
  30.  
  31.           setlength(connections, 0);
  32.  
  33.           memo1.lines.add('Clinet ' + inttostr(i+1) + 'disconnected');
  34.            memo1.lines.add( '------------------------------------------------------------------------');
  35.           memo1.lines.add('Connection closed');
  36.           memo1.lines.add( FormatDateTime('DDDD, dd MMMM YYYY HH:MM:SS', Now));
  37.           memo1.lines.add('Waiting for new connection');
  38.           exit; // <- we can exit because we closed the connection
  39.         end
  40.         else
  41.         begin
  42.  
  43.  
  44.           if i < high(connections) then
  45.           begin
  46.           ConnectionS[i].sock.CloseSocket;
  47.           ConnectionS[i].sock.Free;
  48.             for j:= i+1 to high(connections) do
  49.               begin
  50.                   connections[j-1]:=connections[j]
  51.               end;
  52.             memo1.lines.add('*Clinet ' + inttostr(i+1) + ' disconnected');
  53.              setlength(connections, length(connections) -1);
  54.  
  55.           end;
  56.  
  57.           if i = high(connections) then
  58.             begin
  59.           setlength(connections, length(connections) -1);
  60.             ConnectionS[i].sock.CloseSocket;
  61.             ConnectionS[i].sock.Free;
  62.              setlength(connections, length(connections) -1);
  63.              memo1.lines.add('*Clinet ' + inttostr(i+1) + ' disconnected');
  64.  
  65.             end;
  66.  
  67.  
  68.  
  69.  
  70.           // memo1.lines.add( '------------------------------------------------------------------------');
  71.           //memo1.lines.add('Connection closed');
  72.           //memo1.lines.add( FormatDateTime('DDDD, dd MMMM YYYY HH:MM:SS', Now));
  73.           //memo1.lines.add('Waiting for new connection');
  74.           //exit; // <- we can exit because we closed the connection
  75.         end;
  76.     end;
  77.   end;
  78.  
  79.     Inc(I);
  80.   end;
  81.                

is this right? i still get error

 

TinyPortal © 2005-2018