Parameters:ip$=Address of stream
port=TCP/IP Port Number
Description:Use this command to open up a TCP/IP stream to the designated server and port. If the open command was successful, the command returns a stream handle. Otherwise it returns 0.
You can use this for a multitude of different 'internet' options. Obviously to contact a TCP/IP host outside your own network, you'll need to be connected to the Internet.
The IP address can be in the form of 22.214.171.124 or "www.domain.com".
; OpenTCPStream/CloseTCPStream Example Print "Connecting..." tcp=OpenTCPStream( "www.blitzbasement.com",80 ) If Not tcp Print "Failed.":WaitKey:End Print "Connected! Sending request..." WriteLine tcp,"GET http://www.blitzbasement.com HTTP/1.0" WriteLine tcp,Chr$(10) If Eof(tcp) Print "Failed.":WaitKey:End Print "Request sent! Waiting for reply..." While Not Eof(tcp) Print ReadLine$( tcp ) Wend If Eof(tcp)=1 Then Print "Success!" Else Print "Error!" CloseTCPStream tcp WaitKey End
| This seems to work with more web pages (including php):|
; OpenTCPStream/CloseTCPStream/ReadAvail Example Print "Connecting..." tcp=OpenTCPStream( "www.blitzbasic.com",80 ) If Not tcp Print "Failed.":WaitKey:End Print "Connected! Sending request..." WriteLine tcp,"GET http://www.blitzbasic.com HTTP/1.0" WriteLine tcp,"HOST: www.blitzbasic.com" WriteLine tcp,"" If Eof(tcp) Print "Failed.":WaitKey:End Print "Request sent! Waiting for reply..." While Not Eof(tcp) Print ReadLine$( tcp ) Print "Bytes available:" + ReadAvail(tcp) Wend If Eof(tcp)=1 Then Print "Success!" Else Print "Error!" CloseTCPStream tcp WaitKey
| An excellent Demo of TCP usage can be found here.|
Many thanks to Semar.
| What does the localport parameter do and how do you use it? |
you must provide a port number in order to communicate with a server.
In the above example, a connection is extabilished with a website (www.blitzbasement.com). On that website, there's a web server which 'listens' to connection from clients on a specified port. Usually the standard port for a web server is always 80, that's why the command tcp=OpenTCPStream( "www.blitzbasement.com",80 ) uses it.
This is how an internet browser works, he attempts to connect to the address you type on the address text box, and usually use always the port 80.
Since any pc can use several ports, usually up to 65536, it's very important to know which port to connect to, in order to communicate with the running server.
It's like you want to send a letter to a friend. Not only you must provide the street name (read: IP address of the pc hosting the server program), but also the street number (read: the port where the server is listening on).
Hope it helps,
If you want, try the chat server program that Tin-cat mentioned in this thread, and run the code using different port numnbers.
| No. In the command there is another parameter.|
OpenTCPStream( ip$, port[, localport] )
What does the localport parameter do?
| LocalPort listens only to connections on the local machine (ie only connections comming from your computer)|
which will allow you to test client/server programs you use localport.
| Localport wouldn't be needed very often (you can normally let the operating system pick a port - see exceptions)|
But what it's for is when you make an outgoing connection from your computer, you can specify the source port you are coming from (your source IP address is of course the address of your computer)
Sometimes, remote machines check the source port (as well as IP). They do this because back in the day, UNIX hosts guaranteed that source ports under 1024 were under the control of the 'root'/admin of the server. So there was an implicit authenticity or security guarantee being provided.
Some protcols then (I think DNS, DHCP, identd) will specify source ports (like UDP 53, UDP 67(68?), and TCP 113 respectively) - In those cases, if you don't give the right source port, the remote host will not believe it's genuine, and/or it would not be RFC compliant, and host-based firewalls would have a hard time matching up the session state when a stateless UDP packet "reply" from an external host doesn't match their expectations in their state table.
Anyway, most of the time it doesn't matter.
When you go outbound from your machine, like to a website, you can see that your OS provides whatever next open source port it wants, could be a strange number like 4468. Maybe the next connection will be 4469 or 4470, etc, usually counting upwards like that.
To see it, you can use a program like Microsoft's TCPView tool (or netstat -an)
I should add that your instinct should be that if the protocol uses UDP, it would probably need to specify a localport. (It might not)
Blitz3D Manual Forum
BlitzPlus Equivalent Command