[Implemented] Extra parameters for OpenNetworkConnection

Got an idea for enhancing PureBasic? New command(s) you'd like to see?

OpenNetworkConnection(ServerName$, Port,[LocalIp],[LocalPort],[Timeout])

Poll ended at Thu Mar 16, 2006 3:08 pm

Useful
8
73%
Not useful
1
9%
Dunno'
2
18%
 
Total votes: 11

HarryE
New User
New User
Posts: 9
Joined: Thu Aug 11, 2005 6:51 am
Location: Bucharest,Romania

[Implemented] Extra parameters for OpenNetworkConnection

Post by HarryE »

Dear PB Team,
In order to create decent "professional" apps in PB, I/we need an OpenNetworkConnection(ServerName$, Port,[LocalIp],[LocalPort],[Timeout]) having these three optional parameters.
And it could be renamed OpenTCPConnection.

Should you need details/help in implementing the required function(s), I would be more than happy to participate.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

What's the point of LocalIP and LocalPort? What should they do?
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

A timeout would be nice.
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

Syntax

Connection = OpenNetworkConnection(ServerName$, Port [, Mode])
Description

Try to open a connection on the specified server. 'ServerName$' can be an IP address or a full name (ie: "127.0.0.1" or "ftp.home.net").

If the connection has be granted by the server, the 'Connection' is not NULL, otherwise the connection has failed. 'Connection' is needed by all receive and send commands.

The optional 'Mode' parameter can be one of the following value:
#PB_Network_TCP: The connection will use the TCP network protocol (default)
#PB_Network_UDP: The connection will use the UDP network protocol. The connection will not be
explicitely created, as UDP is a connection less protocol, but it will
add an entry in the PureBasic management stack and allow to send
data via UDP using the regular SendNetworkData() and such commands.
Why renaming it to OpenTCPConnection as we can specify TCP or UDP with the 'mode' argument' ?

And in ServerName$ you can specify a local IP, so i don't understand your request... sorry...

But the timeout might be very useful, yes.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

ServerName$ is only available to client side not server side. This have been wished over and over again since 10/2003, but Fred expressed no interest in completing the network library in this fashion.

Btw: the correct server syntax is:

Code: Select all

Result = CreateNetworkServer(Port)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Obviously there is no use for a ServerName$ on the server side as a program cannot point a domain name to itself.

For LocalIp we have the same, a computer has an IP and that IP is not changed by a network command. You cannot listen on a different IP than the computer you are on.

LocalPort is totally nonsense as both the client and the server will have to use the same port, so it will always be the same as the port.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Lets think about this for a moment. Having server side computer experience is a must .... .... ....

The majority of servers have multiple IP addresses pointed to it. Multiple domains can point to one IP address or can be spread over several on the same computer.

Any professional server side software will allow you to choose to either listen on one IP address, all IP addresses, or certain IP addresses. With your choice of ports on the IP(s) chosen. This is a rule of thumb for any server side software. If it doesnt have this feature, it is not professional software.

This feature have been requested repeatively for years. I mean, it makes no sense to use PB for network development if it doesnt have this basic feature. Alot of people turn to third party network libraries that are complete (server side). This is one of the areas where PB is severly lacking.

So, think of it from the view of someone who develops, and use professional server side software. So for server side, it makes sense (ask anybody in the ISP business, or who develop real server applications). But for client side ....

wth? I have to agree, local port and port is the same thing, no? servername$ and localip is the same (for client side).

The timeout was requested, fred responded by making the timeout reduce from 30 seconds to less. Sometimes adding/fixing the problem rather then covering with a bandaid is more helpful :)
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

The problem is Fred can only implement features that are possible trough the operating system APIs so for example he could easily implement ipv6 if needed, but what would you do to make a server only accept cetrain ip addresses?
The problem is you can't really do it, the only thing you can do is to test the socket and it's address structure returned from the listen command, implementing this in a way easy to use in Pb style isn't easy and if you really need it why not use the WinSock API directly, it's not that difficult and you can do it in your way.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Currently you can bind an ip address with winsock using winsock's Bind function. It sounds like your thinking for something different. I am talking about multiple ip addresses that is assigned to one box. In a real world, a server would have the option of which ip address it wants to listen on, (use), to accept connectsion targetting that address.

Some references here:

PowerBasic/UDP: http://www.powerbasic.com/support/forum ... 00073.html

VB: http://www.vbip.com/winsock-api/bind-so ... ket-01.asp

MSDN: http://msdn.microsoft.com/library/defau ... bind_2.asp
http://msdn.microsoft.com/library/defau ... dtopic.asp
http://msdn.microsoft.com/library/defau ... socket.asp


So, it is part of winsock, the OS API, Fred just need to change the internal Connect call with the Bind call and add in that parameter(s).

Though you did give a great reason for people to create their own server network library or use an existing one :) But that is what people are doing now :) I figured it would be nice for the pb network library make use of the OS's API for server features. It's already there on the OS level, why not expose it in PB?
HarryE
New User
New User
Posts: 9
Joined: Thu Aug 11, 2005 6:51 am
Location: Bucharest,Romania

Need for Localport, LocalIP

Post by HarryE »

Trond, it is not a nonsense.
The client connection needs LocalIp and localPort for the following reason:
Good news! I have a computer with multiple network interfaces(192.168.0.15, 10.0.0.8 for example) and NO DEFAULT GATEWAY defined.
The current OpenNetworkConnection is almost useless in this case.
I want to connect to a different machine listening on port 7777 for example.
I want to be able to initiate a connection to that machine using a specific local port, eg. 4444 because the remote machine checks that and drops the connection otherwise. And I want that in the easy way.
The "hard way" is as follows.

Code: Select all

Structure ws_Rlinger
  onoff.l
  seconds.l
  EndStructure
Structure ws_sockaddr 
    ws_in_family.w                     
    ws_in_port.w                      
    ws_in_addr.l                      
    ws_in_zero.b[8]                    
  EndStructure                          
  ;
Structure ws_wsadata 
    ws_wversion.w                         ; +0 
    ws_whighversion.w                     ; +2 
    ws_description.b[257]                 ; +4 
    ws_systemstatus.b[129]                ; +261 
    ws_imaxsockets.w                      ; +390 
    ws_imaxudpdg.w                        ; +392 
    ws_vendorinfo.l                       ; +394 
  EndStructure    

Global LclEnd.ws_sockaddr  
Global RemEnd.ws_sockaddr
Global lng.ws_rlinger
Global ws_wsastartup.ws_wsadata 
#SOL_SOCKET = $FFFF              
#SO_LINGER = $80              ; linger on close if unsent data is present 
#SO_REUSEADDR =4    
#AF_INET = 2  
#IPPROTO_TCP = 6 
 
Procedure.l ws_iptobip(ip.s) 
  ; 
  ; convert dotted ip address to binairy form 
  ; 
  ProcedureReturn inet_addr_(ip) 
  ; 
EndProcedure 

WSAStartup_($101,@ws_wsastartup)   
 S = SOCKET_(#AF_INET,#SOCK_STREAM,#IPPROTO_TCP)    
          local_port.l = 4444
          local_bport.l = htons_(local_port)      ; port number to tcp/ip network byte order 
         local_ip.s = "10.0.0.8"
         local_bip.l = ws_iptobip(local_ip)
          PrintN("local port "+Str(local_port)) 
        ;  
        ; bind a socket (reserve socket and port for exclusive use by the application) 
        ; 
; that's the part for the extra 3 params    
         ; create a struct 
          lclend\ws_in_family = #AF_INET                     ; udp / tcp 
          lclend\ws_in_port = local_bport                   ; port 
    
  lclend\ws_in_addr=local_bip
          retval = bind_(S,@lclend,16)           ; and bind it - that's it!
          PrintN("bind "+Str(retval)) 
          retval=1
          lng\seconds=1   ; for quick reuse of the port
          lng\onoff=0 ; 

          setsockopt_ (S, #SOL_SOCKET, #SO_REUSEADDR, @retval, 4);
          setsockopt_ (S, #SOL_SOCKET, #SO_LINGER, @lng, SizeOf(lng));
         ; 
 ; end of part
          ; 
          ; *** connect to another server / port using an empty socket 
          ; 
        server_ip.s = "10.9.67.8"
        server_bip.l = ws_iptobip(server_ip)
           server_port.l = 7777
          server_bport.l = htons_(server_port)      ; port number to tcp/ip network byte order 
          PrintN("server port "+Str(server_port))        
          remend\ws_in_family = #AF_INET 
          remend\ws_in_port = server_bport 
          remend\ws_in_addr = server_bip 
          client_sockaddr_l.l = 16 
          retval = connect_(S,@remend,client_sockaddr_l) 
          ; 
          PrintN("connect "+Str(retval)) 
; Here we have my machine 10.0.0.8:4444 connected to 10.9.67.8:7777
; Compare it to OpenNetworkConnection("10.9.67.8", 7777,"10.0.0.8",4444,1)!
WSACleanup_()  
The winsock stuff is taken from this forum. The code may not be complete.
HTH
Last edited by HarryE on Fri Mar 03, 2006 8:17 am, edited 1 time in total.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post by Shannara »

Holy crap, your right. Here's another example:


Anybody with multiple connections, lets say a connection to a private lan, and a connection to the internet, two different ethernet chords, thus two different IP addresses.

The client program need to know which local address to use. This does make sense to me now. Can't believe I forgot that.. Thanks for the info HarryE
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

a TimeOut would come very handy!
Tranquil
MyTrial
Enthusiast
Enthusiast
Posts: 165
Joined: Thu Nov 30, 2006 11:47 am

Post by MyTrial »

The local port would be nice.
For a TFTP communication you need that.
Tranquil
Addict
Addict
Posts: 952
Joined: Mon Apr 28, 2003 2:22 pm
Location: Europe

Post by Tranquil »

And the possibility to set a IP Listening-List to a created server if your system has more then one IP.
Tranquil
Post Reply