
-----------------------------------
loonyli
Tue Mar 09, 2004 10:24 pm

help with Net module
-----------------------------------
i'm trying to write a program that communicates with 3 other computers via TCP/IP using the Net module.  this is fine with 1 connection between 2 computers.  however, the trouble arises when i try to open more than 1 connection (1 each to the rest of the computers).  it appears that the client program (the one that uses the Net.OpenConnection) does not want to connect with the host computer.  the port numbers and the stream numbers should be set in a way that there should not be any conflicts between connections.  is it due to errors in my code, or is it due to the limitations of the Turing language that causes this incapability in establishing multiple connections in the same program?

-----------------------------------
recneps
Wed Mar 10, 2004 1:31 pm


-----------------------------------
Limits of turing language, it can only connect 2, so what you gotta do is have the sever take a connection, then the port number goes up by one, then when the server sends/gets data, it checks all the streams (one stream per port) Understand? i believe there is a chat prog in source or submissioin that uses this method.

-----------------------------------
Tony
Wed Mar 10, 2004 7:23 pm


-----------------------------------
:lol: that's not the limit of turing... port can host only 1 connection at a time :P

the idea here is managing multiple streams on different ports and organizing server/client setup/ info relay

-----------------------------------
loonyli
Wed Mar 10, 2004 9:26 pm


-----------------------------------
i've tried using different ports and different streams for the 3 separate connections. maybe there's something wrong in my coding.  is it possible to have the server "listen" on 3 different ports simultaneously (w/ difference processes)? and is there a way for the client program to test each connection to see if it is used or not? (i.e. client tests ports 10000, 10001, 10002 in order until it finds an unused port that the server is listening to)

and the example chat program sets up only 1 connection between 2 computers.

-----------------------------------
jonos
Wed Mar 10, 2004 9:29 pm


-----------------------------------
if you post your code, then we may be able to know what is the problem. and id suggest not using processes, well firstly because tony will say something against them and thats not good cause hes boss, and secondly i think it will scramble up things if you are sending them across the net (im pretty sure i saw that in a program once.)

-----------------------------------
Tony
Wed Mar 10, 2004 9:44 pm


-----------------------------------
haha :lol: don't use processes :evil:

main reason being is that since processes radomly pick which procedure to actually execute, you'll end up listening to just 1/3 ports at a time, randomly and chances are that noone will be able to connect.

the idea here is to listen to a single connection port and once connection is established to listen to the next available port.

Then to check for messages, you just forloop through all of the connections, checking if there're any characters/lines to read

-----------------------------------
loonyli
Thu Mar 11, 2004 3:04 pm


-----------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This is the server code
%%%%%%%%%%%%%%%%%%%%%%%%%

var netStream : array 1 .. 3 of int
var netAddress : array 1 .. 3 of string
const netPort : array 1 .. 3 of int := init (10000, 10001, 10002)

put "Waiting for Connections..."

for i : 1 .. 3
    netStream (i) := Net.WaitForConnection (netPort (i), netAddress (i))
    
    put "Connected to ", netAddress (i), " via port ", netPort (i)
end for

put "All connections established."

%%%%%%%%%%%%%%%%%%%%%%
%this is the client code
%%%%%%%%%%%%%%%%%%%%%%%%%%

var netStream : int
var netAddress : string
const netPort : array 1 .. 3 of int := init (10000, 10001, 10002)

put "Enter address to connect to: " ..
get netAddress

for i : 1 .. 3
    netStream := Net.OpenConnection (netAddress, netPort (i))

    if netStream > 0 then

        put "Connected to ", netAddress, " via port ", netPort (i)
        exit          % exit program when connected
    else
        put "Failed to connect to server via port ", netPort (i)
    end if

    if i not= 3 then
        put "Trying to connect via port ", netPort (i + 1)
    end if
end for

if netStream 