Page 1 of 2

Better Client/Server Networking?

Posted: Tue Jan 13, 2009 3:30 pm
by Psych
Hi, and thanks for providing us with a wish section.

I've been recently looking up some questions I have regarding server/client functionality.

Firstly, is it right you can only have 1 server per application? I don't need a server at all for my application, however I did find this to be a little strange to limit the amount of listening ports (imagine trying to make a torrent client... nightmare).

Now what follows is only my understanding, having browsed through the foums and asked some questions.

Ok, this I find really annoying... You have a server listening on port 1000, a client connects on port 1000, the listening port is now basically a client sending information back and forth on port 1000. Nothing else can connect to the application because port 1000 is in use.

What needs to happen (IMHO) is the server needs to notify that there is a connection request on the active listening server. The server then needs to decide whether to accept the connection or refuse it (depending on IP, port of remote computer, this doesn't happen at present), and finally, if approved, the connection needs to be accepted on a new client (on a new port). The server keeps listening uninterupted.

I think too much emphasis is placed upon a connection being a 'Server' or a 'Client', as they are basically the same thing other than one listening, and the other connecting. You've made it really easy to accept connections, however, no need to bind or worry about any of the other stuff, so all credit to you there. I just feel alot is missing from these controls.

All clients should also have the funtionality to get their local IP and port and the remote IP and port at any time.

Re: Better Client/Server Networking?

Posted: Tue Jan 13, 2009 5:20 pm
by cas
Psych wrote: Firstly, is it right you can only have 1 server per application?
No. :P
Open as many servers as you want/need, each server on different port.
Psych wrote: Ok, this I find really annoying... You have a server listening on port 1000, a client connects on port 1000, the listening port is now basically a client sending information back and forth on port 1000. Nothing else can connect to the application because port 1000 is in use.
Yes it can. You can have many clients connected to one server on port 1000.
Psych wrote: What needs to happen (IMHO) is the server needs to notify that there is a connection request on the active listening server.
Server notifies you when client is connected. NetworkServerEvent() returns #PB_NetworkEvent_Connect. You could use CloseNetworkConnection(EventClient()) to close connection if you want.

Psych wrote: I just feel alot is missing from these controls.
You won't feel like this once you read purebasic manual. :lol:
Psych wrote: All clients should also have the funtionality to get their local IP and port and the remote IP and port at any time.
Easy to code by yourself once you understand how networking actually works.

Posted: Tue Jan 13, 2009 6:24 pm
by Psych
Well it would appear that I clearly know nothing about how sockets work, the bulk of what you just said is entirely different to what I've been reading in the forum over the past 2 days, and indeed what I have believed to be true about sockets my entire programming career.
Guess I should go back to school.

Posted: Tue Jan 13, 2009 7:26 pm
by Trond
There is a very big problem here: you keep saying sockets. PB doesn't use the idea of "sockets". PureBasic's networking commands aren't organized that way.

Sockets are nothing physical, they are just an abstraction of networking. PB uses a different abstraction. If you want the socket abstraction, use WinSock (accept_(), bind_(), close_(), etc...).

Posted: Tue Jan 13, 2009 7:39 pm
by Psych
Are they not sockets then? I mean what I call them doesn't really make any difference, does it?
I have found these forums really helpful, with alot of things, that said there seems to be alot of discussion in the topics regarding symantics.
As far as I am concerned they are a socket, I make no secret out of the fact that I am starting with this language, mainly because I like the speed aspect of having no runtimes or framework that I got using vb.
Making me look stupid or in the wrong forum by pointing out that I use terms everyone knows, but isn't in the reference doesn't help.
This is a Wish thread, right? Well what I wish for is a 'gadget' that does what I mentioned in the first post, and until such a gadget arrives, I'll muddle along with what I have, just like everyone else.

Posted: Tue Jan 13, 2009 8:23 pm
by gnasen
I know what you mean Psych, in VB is a control named "winsock" which is placed on the form and used like every common control.

However it is not a real control, it is just done that way because it fits to the rest of the visual designer of VB.

Purebasic is not OOP, you dont place a winsock "gadget" and use it like a control.

Posted: Tue Jan 13, 2009 8:54 pm
by Trond
Are they not sockets then?
I don't know much about sockets, but if i'm correctly informed then you can't connect three sockets together, only two and two. This is a way of abstracting the network, which in reality can send packets in all directions.
PB can have any number of clients connected to a single server, which means that this abstraction of the network is a client/server abstraction rather than a socket abstraction (ie. it's a higher level abstraction).
So no, they are not sockets (although they are probably implemented with sockets, but we don't get access to the implementation).
I mean what I call them doesn't really make any difference, does it?
You already assumed a lot of wrong things about the network library because you thought it worked like sockets. If you had instead not called it that, you wouldn't have assumed that it worked like sockets (which it doesn't do).
If I called you an idiot even though it wasn't true, would it matter to you? Probably, because it does matter what we call things. If it isn't a socket, then calling it a socket will cause all kinds of confusion.
You can use sockets in PB if you want to, but they are not documented in the manual, as they are provided by the OS, so they are documented by Microsoft.
As far as I am concerned they are a socket
And as far as you are concerned, you can't have multiple clients connected on the same port, which in reality you can, so obviously something is wrong with your approach there.
This is a Wish thread, right? Well what I wish for is a 'gadget' that does what I mentioned in the first post, and until such a gadget arrives, I'll muddle along with what I have, just like everyone else.
The problem is that all the functionality that you want is already available. If you have problems using it, post in the coding questions section, rather than this section. :)
You see, every time we have a new user, they start posting things in the features requests and whishlists sections, when these things are actually available already. They just want it to be "better", or in other words, like it was in VB. You're not the first person to make this mistake.

I will try to answer some of your questions in this topic, hopefully you will understand better how PB's network library works.
Firstly, is it right you can only have 1 server per application?
No, read the entire manual page called "CreateNetworkServer()". You can have any number of servers.
I don't need a server at all for my application
Remember that iIf you want to connect to your application from somewhere else, you need a server. You can't connect to a client (although each program can function as both client and server at the same time). Basically the server is the opened port that accepts connections.
Ok, this I find really annoying... You have a server listening on port 1000, a client connects on port 1000, the listening port is now basically a client sending information back and forth on port 1000. Nothing else can connect to the application because port 1000 is in use.
This shows a server that accepts strings from clients like the example client from the manual (you did look at it, right?). Or use the client I wrote for you, it sends a custom string. Start the client multiple time and see how the server handles it.

Code: Select all

; Server
; Run with the debugger
; Use debugger -> kill program to stop it
InitNetwork()

Port = 6832
*Buffer = AllocateMemory(1000)

If CreateNetworkServer(0, Port)
  Debug "Server: server created at (Port "+Str(Port)+")."
  Repeat
    SEvent = NetworkServerEvent()
    If SEvent
      ClientID = EventClient()
      Select SEvent
        Case #PB_NetworkEvent_Connect
          Debug "Server: A new client (" + Str(ClientID) + ") has connected"
        Case #PB_NetworkEvent_Data
          ReceiveNetworkData(ClientID, *Buffer, 1000)
          Debug "Server: Client " + Str(ClientID) + " send a string:"
          Debug PeekS(*Buffer)
        Case 4
          Debug "Server: Client "+Str(ClientID)+" has closed the connection."
      EndSelect
    Else
      Delay(1)
    EndIf
  ForEver
  CloseNetworkServer(0)
Else
  MessageRequester("Error", "Can't create the server (port in use ?)", 0)
EndIf

Code: Select all

; Client
; Start several instances to see how the server copes with multiple connections
InitNetwork()
Port = 6832
ConnectionID = OpenNetworkConnection("127.0.0.1", Port)
If ConnectionID
  OpenWindow(0, 0, 0, 512, 384, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
  StringGadget(0, 10, 10, 300, 22, "Hello from client")
  ButtonGadget(1, 10, 50, 150, 30, "Send string to server")
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            SendNetworkString(ConnectionID, GetGadgetText(0))
        EndSelect
      Case #PB_Event_CloseWindow
        Break
    EndSelect
  ForEver
  CloseNetworkConnection(ConnectionID)
Else
  MessageRequester("Client", "Can't find the server (Is it launched ?).", 0)
EndIf

Posted: Tue Jan 13, 2009 10:39 pm
by Thalius
Sockets:

Sockets are basically an API defined back in the 80s by Berkeley ( thoose used for internet communication ) - sockets in general handle I/O Operations over any kind of device and even can be used ( and has been on early unixes ) to access fileinformations from eg a harddrive.

Its more or less a path defined from a thread over an I/O Interface ( be it ethernet, usb or wahetever ). System sockets are soft limited - under unix you can set theese with a kernel parameter - not sure about the windows stack limit ( The Windows berkeley implementation is called WinSock and part of the WinAPI ).

Theoretically ( Practically, your Server has to be VERY fast to handle so manny connections under workload ) you can have over an eg PB Network connection a max of 65500 ( some are used by the system already ) connections to a single IP ( a single network card can also have multiple IPs ).

Purebasic networking utilizes winsock under windows in the background providing a very easy way to use the raw api speed transparently for people with just a basic understanding of networking.

You can implement your own Protocol ( theres some good examples of a client server model from mk-soft around here somewhere ) using the NetworkData Commands - which is a little more advanced but very flexible and fast compared to parsing strings all over ;)

Cheers,
Thalius

Posted: Tue Jan 13, 2009 11:09 pm
by Psych
Now that is more in line with my understanding, thankyou.
And the reason we can have those many connections per IP? It's the number of free ports, more or less, right? Thus meaning every connection needs it's own port.
I knew I wasn't going mad.
Does this mean then that the Network functions are only available to a windows platform project? Since they are derived from the winsock dll?
I'm probably going to use the dll, as I think that is the right way for me to go, I've played with it before when I wanted a web based ocx for a chat client many years ago.
I'll look up the mk-soft model though tomorrow when I have more time to post questions.
Thanks again.

Posted: Wed Jan 14, 2009 12:14 am
by Thalius
As a beginner use the PB network functions. They are avauilable across ALL platforms. Theese use the NATIVE networking subsystems of the running Operating System ( while winsocks is the implementation from microsoft under windows - and the berkeley implementations on linux and macOS ).
Purebasic s Network functions allow you this way with a single code to program for all supported os without knowing each of the implementations and almost nothing about networking. ;)

a socket connection on a local host has an outgoing port and an incoming port. The Outgoing port is basically the target port - so , your servers incoming port to where he can relate to whichs outgoing port the client belongs to. ( hope i didnt confuse anyone but fangles here completelly ! :lol: )

Above limits apply to general networking. While PBs function simplify things - they are powerful enough to do basically anything.

Posted: Wed Jan 14, 2009 11:36 am
by Trond
Thus meaning every connection needs it's own port.
No, you can have multiple clients connected to one server on the same port. I just showed you how to.

Posted: Wed Jan 14, 2009 2:36 pm
by Psych
Well I guess we could go on and on and on about ports per connection, but let's just agree to disagree.
This was originally just a desire for a few extra features in the methods and events you get back.
If that happens, fine.
If not, also fine.

Posted: Wed Jan 14, 2009 2:51 pm
by pdwyer
Psych,

(perhaps I'm misunderstanding what you are saying)
A server need only listen on one port, it doesn't need to open a new port for each client connecting to it. It does need to open a new port for each session that it initiates (ie is a client in) though.

If you do a netstat -a on a web server you will see lot's of sessions with 80 as the local server port and another port which is the port number on the remote client side. The server only has one port for this though. Each client has it's own dynamic 1023+ port assigned.

A server and client IP and port pair make up a session which is full duplex and uses only one port on each side (not a sending and a listening port on each side).

So, the PB server app which is say a web server, only listens on port 80 (or wherever) and needs no further ports assigned to do it's job dealing with lots of clients. It need not assign any ports for clients or anything like that

Perhaps this is what you meant though, if so, sorry for the unnecessary spiel

Posted: Wed Jan 14, 2009 4:23 pm
by Psych
Maybe it's the way I have always done it that is making me think in a way that is not correct.
My understanding was that a connection is much like a simple telephone, you dial a number, then wait for an answer, neither line can dial or ring while this is happening, when the person picks up the phone, the lines (ie. both numbers) are in use until either hangs up.
So how does a server work in my own little world? Let's use the phone example again, the phone rings, the guy waiting for a call says 'I have a call, I know, instead of tieing up this line, I'll take the call on this other line so other people wont get a busy tone'. The phone rings again because the line isn't busy, he takes that call on another line also, before you know it, the guy has lots of calls, even though all the people called the same number.
This may be complete and absolute rubbish, but it makes alot more sense to me than having one port handle lots of data from multiple clients all on the one port. That said, I am beginning to think I am in the minority in my belief, LOL!! Won't be the last time either I am sure.

Posted: Wed Jan 14, 2009 9:15 pm
by Thalius
hehehehe.

Its a bit confusing to understand at first basically thats where the Sockets com ein maybe you can google or wikipedia on it to get a bit more depth if you want :)

And trust me, you dont hit the soft limit even with a quad cpu that easy on a server that does a reasonable amount of workload over thousands of connections. For example mmo servers - use multiple machines ( typically one for each zone , or a limited amount of instances ) - linked over a server in the lan again which handles all the traffic between them ( often a database server / cluster ).

So, hang yourself in the networking commands - i think youll find help here if you run into a specific problem. Once you know whats there its still valid to ask for more - but this way you can provide examples where function xy would be of use and it can be weightened against other feature requests in complexity.


Cheers,
Thalius