Sockets in new release

BlitzMax Forums/BlitzMax Programming/Sockets in new release

Does anybody have a working example of a client/server app for multiplay ?

Sockets is very fuzzy to me :(

***EDIT***

These programs work now.
to test on a single PC
1. save server.bmx and client.bmx
2. open 2 seperate instances of BMAX
3. run server first, the run client
4. type into client and hit enter...

or... run server on a Linux box and client on a MAC

I'll build on this when i get more time




SERVER
'Server
Graphics 320,240,0
Local My_server:TSocket=CreateTCPSocket()
BindSocket My_server,12345
SocketListen My_server


Repeat 
	Local client:TSocket=SocketAccept( My_server )
	If client
		stream:Tsocketstream = CreateSocketStream(client)
		Print DottedIP(SocketRemoteIP(client))+" is Connected"
		
		Repeat
		
			foo$ = ReadLine$(stream)
			DrawText foo$,0,0
			
			Flip ; Cls
		Until KeyHit(KEY_ESCAPE)
		
	EndIf
	
	
	
	
	
	Flip ; Cls
Until KeyHit(KEY_ESCAPE)




CLIENT
'Const LOCALHOST = 2130706433
Graphics 320,240,0

Global LHOST = Dotted_to_integer("127.0.0.1")


Local client:TSocket=CreateTCPSocket()
ConnectSocket client,LHOST,12345

If client
	stream:Tsocketstream = CreateSocketStream(client)
	' WriteLine(stream,"Hello World")
Else 
	Print "cant connect to server"
EndIf

Local sendstring$ = ">"
Repeat
		
	keytimer:-1
	If keytimer < 0
		stroke = GetChar()
		
		Select stroke 
			Case 0
				' do nothing
			Case KEY_BACKSPACE
				sendstring$ = sendstring$[0..Len(sendstring)-1]
			Case KEY_RETURN 
				WriteLine(stream,sendstring$)
				sendstring$ = ">"
				'sendstring$:+"[R]"
			Default sendstring$:+Chr(stroke)
		End Select
		keytimer = 10
	EndIf
	
	DrawText sendstring$,0,0
	

	


Flip ; Cls

Until KeyHit(KEY_ESCAPE)

Function Dotted_to_integer(Dotted_ip$)
		Dotted_ip$:+"."
		Local octet:String[4]
		For i = 0 To 3
			dot =  Dotted_ip$.find(".") ; octet[i] =  Dotted_ip$[0..dot]
			Dotted_ip$ = Dotted_ip$[dot+1..]
		Next
	Return (octet[0].toint() Shl 24) + (octet[1].toint() Shl 16) + (octet[2].toint() Shl 8) + octet[3].toint()
End Function 







ah thanks so much man.

I will give this a bash

hmmm, it just hangs :(

Ok Deux, I'll take a look at the code and fix it.

some pointers:
1. 127.0.0.1 is referred to as LOCALHOST or LOOPBACK. It is an IP address of your machine. This address is very handy for testing.

2. IP addresses are usually referred to by dotted ip notation. "1.2.3.4" . BlitzMAX uses Integers to refer to the same thing. 127.0.0.1 = 2130706433
I threw together the little conversion function to make it easier to present a dotted IP to Blitmax. Each OCTET is one byte (8 bits). In binary notation it could be written like this
11111111.10101010.10101010.00011111
each octet ranges from 0 to 255 decimal or 00000000 to 11111111 binary. I will add a range check to my func to test for this
Function Dotted_to_integer(Dotted_ip$)
		Dotted_ip$:+"."
		Local octet:String[4]
		For i = 0 To 3
			dot =  Dotted_ip$.find(".") ; octet[i] =  Dotted_ip$[0..dot]
			Dotted_ip$ = Dotted_ip$[dot+1..]
		Next
	Return (octet[0].toint() Shl 24) + (octet[1].toint() Shl 16) + (octet[2].toint() Shl 8) + octet[3].toint()
End Function 

My_IP$ = "127.0.0.1"

Print MY_IP$+" = "+Dotted_to_integer(MY_IP$)



I had the client/server stuff working fine then screwed around with it.(it was late) I will fix it later today and post

*** UPDATED*** see my first porst above

(digging this up because I'm doing multiplayer stuff)

The code in this post was hanging for me - you need to change
foo$ = ReadLine$(stream)
to
If SocketReadAvail(client) Then foo$ = ReadLine$(stream)

readline halts while it waits for input if there's nothing available, I think.