AcceptTCPStream code example

BlitzPlus Forums/BlitzPlus Programming/AcceptTCPStream code example

Pineapple(Posted 2009) [#1]
I ran it as 2 separate programs as expected, and the server never receives the client's message. The client gives the message connected successfully, but the server always says it's waiting. I need to know the correct way to do the code for something I'm trying to program. Thanks.


; CreateTCPServer, CloseTCPServer, AcceptTCPStream Example 
; This code is in two parts, and needs to be run seperately on the same machine 

; --- Start first code set --- 
; Create a server and listen for push 

svrGame=CreateTCPServer(8080) 

If svrGame<>0 Then 
Print "Server started successfully." 
Else 
Print "Server failed to start." 
End 
End If 

While Not KeyHit(1) 
strStream=AcceptTCPStream(svrGame) 
If strStream Then 
Print ReadString$(strStream) 
Delay 2000 
End 
Else 
Print "No word from Apollo X yet ..." 
Delay 1000 
End If 
Wend 

End 

; --- End first code set --- 


; --- Start second code set --- 
; Copy this code to another instance of Blitz Basic 
; Run the above code first, then run this ... they will 'talk' 

; Create a Client and push data 

strmGame=OpenTCPStream("127.0.0.1",8080) 

If strmGame<>0 Then 
Print "Client Connected successfully." 
Else 
Print "Server failed to connect." 
WaitKey 
End 
End If 

; write stream to server 
WriteString strmGame,"Mission Control, this is Apollo X ..." 
Print "Completed sending message to Mission control..." 

; --- End second code set --- 



Sauer(Posted 2009) [#2]
This code works fine for me, I replaced the Delay 2000 with an Input("Message Recieved!") to wait for an enter key press, but all the networking mechanics seem to work fine.


Pineapple(Posted 2009) [#3]
The client is never receiving the data sent by the server. I tried getting the client to read from another server for a game that's on my computer but it still read nothing but a big, fat 0.

Server:
Print "server test."

Global server=CreateTCPServer(8080)

If Not server Print "Server failed to initiate."

out=OpenTCPStream("127.0.0.1",8080)
WriteByte out,1
WriteString out,"test server"
WriteString out,"Test!!"
WriteByte out,1
;CloseTCPStream out


Repeat

	WriteByte out,50

	in=AcceptTCPStream(server)
	If in Then
		Print "Input to server: "
		Repeat
			Write Chr(ReadByte(in))
		Until Eof(in)
	EndIf

Forever

CloseTCPServer server



Client:
testserver=OpenTCPStream("127.0.0.1",8080) 
Repeat
Print ReadByte(testserver)
Until Eof(testserver)
Input
CloseTCPStream testserver



Wings(Posted 2010) [#4]
Wings is back.. cause he is hacking windows api.. anyway..


here is your server working.




And the client almost same..