TCProblem

Blitz3D Forums/Blitz3D Beginners Area/TCProblem

Buggy(Posted 2006) [#1]
Since no one is answering my questions on the other thread, I created this one! For some reason, whenever I create a TCP server, once a client is created, the server only will receive one measly message from the client.

I thought maybe I coded it incorrectly, so I used the sample, but changed it to a repeating loop, and the same thing happened. Here is the sample:

This is the server code:

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
		
	Else 
		Print "No word from Apollo X yet ..."
		Delay 1000
	End If 
Wend

End


And here is the client code:

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
While Not KeyDown(1)
	WriteString strmGame,"Mission Control, this is Apollo X ..."
	Print "Completed sending message to Mission control..."
Wend

End


Can anyone help me?

I only have the first version of Blitz3D, so could there be an update for this?


jfk EO-11110(Posted 2006) [#2]
Of course you can download the latest Blitz3D version, just go to the Updates Section!

I am not sure if you can write multiple objects to one stream, but it works when you open and close the tcp stream of the client for each string you'll send. Not sure how slow this is, TCPTimeOut may help to have a minimal connection lag.

So the client would look like this:
while not keyhit(1)
 strm=OpenTCPStream("127.0.0.1",8080)
 if strm<>0
  writestring strm,"hello "+ millisecs()
 endif
 closeTCPstream(strm)
wend


I also think it looks like it would be much better to leave the stream open and continously write to it, maybe somebody can enlighten us here.

However, if you need a fast connection eg. for multiplayer games I would suggest not to use TCP, but UDP instead.


Buggy(Posted 2006) [#3]
That's what I discovered also, jfk, but someone told me that one stream can stay open the whole time and be written to. I just want to get the hang of multiplayer, without having to deal with lag problems.

Thanks.


jfk EO-11110(Posted 2006) [#4]
Ok, now I have managed to use the stream unlimited times, you need to replace in the server:

print readstring$(strm)

by

while eof(strm)=0
print readstring$(strm)
wend

The reason why is, if they gave you a plate full of food, you first have to eat it all to get the next plate. Or in tech terms: as long as there is still something in the buffer, you cannot receive a new object, so always make sure to read until there is EOF or a timeout.


jfk EO-11110(Posted 2006) [#5]
BTW: if you think about to use UDP you should know that other than in TCP you have to detect and correct transmision errors by your own. UDP is more kind of a raw data system (basicly thought to send connection error codes). The pro is it's a direct connection to the target, where TCP is rather a packet system where data is split into packets that may arrive in an order that is diffrent from the one that was sent.

Using UDP requires to develop your own checksum system that allows to reorder corrupted transmission.


Buggy(Posted 2006) [#6]
Thanks a bunch.

By the way, why is it that if I use CloseTCPStream each time and then re-open a new TCP stream, it works for a while, but then it says "Error: Stream does not exist"?


Buggy(Posted 2006) [#7]
Your system works much better, jfk, thanks a bunch.

However, this way - I'm actually using something similar to the samples but not exact - the client needs to be started first, and then the server, otherwise I get a "stream does not exist" error. And when I delay the server 1000 each loop but not the client, if I close out of the client, the server keeps on going until it caught up to the number of times the client sent the message. Since you're so good with this, any ideas?


Buggy(Posted 2006) [#8]
Something got messed up when I tried to send it coordinates instead of a set thing - look.

(This is my version)

Server
Graphics 640, 480

image = LoadImage("bullet.PNG")

game = CreateTCPServer(1)

If game <> 0

	Print "Server Started"
	
Else

	Print "An Error Has Occurred"
	
	WaitKey
	
	End
	
EndIf

SetBuffer BackBuffer()


While Not KeyDown(1)

	Cls
	
	x = AcceptTCPStream(game)

	
	
	If game <> 0
	
		Text 100, 0, "Connected"
		
	Else
	
		Text 100, 0, "Not Connected"
		
	EndIf
	
	DebugLog "Check"
	
	If x <> 0
		
		Print x
		
		Delay 1000
		
		While Eof(x) = 0
	
			imagex = x
			
			DebugLog "Mate"
			
		Wend
	
	EndIf	
				
	DrawImage image, imagex, 0
		
	
		
	
	
	
	
	Flip
	
	
	
Wend


Client
Graphics 640, 480

image = LoadImage("bullet.PNG")

clientgame = OpenTCPStream("127.0.0.1", 1)

If clientgame <> 0

	Print "Connection!"
	
Else

	Print "Error!"
	
EndIf

WaitKey

SetBuffer BackBuffer()


While Not KeyDown(1)
	

	
	Cls
	
	x = Rand(200, 400)

	
	
	WriteInt(clientgame, x)

	

	
	If clientgame <> 0
	
		Text 30, 0, "Connected"
	
	Else
	
		Text 30, 0, "No Connection"
		
	EndIf
	
	DrawImage image, x, 0
	
	Flip
	
	
	
Wend




End


Any help would be much appreciated.


Wings(Posted 2006) [#9]
hey are you hacking our mux

TCP port 1. The service assigned to TCP port 1 is tcpmux (for more info see RFC#1078). We know that some of the scans originated from sites which were root compromised.


LOL :)

pssst.. use server ports over 7000 or many firewall block you app.


psssst again.
client seam to send data and dont care if its arrived ?
tcp is a 2 way protocol. you shold indeed reply to all data you send.

like client says: Hey sirver give me another beer.
(server sends a beer)
client get the bear and says. Hey sirver give me anothe beer.
(server sends a beer)
((maby y gett the 2 way convensation its all taking place in one same pub))
((pub = stream))


in udp its wors.. i had to ad it..
client opens a Pub so server can reply his beer.
server opens a Pub so client can request beer.
now client go to the pub and say please send a beer to mine pub.
server call one of his dogs and give it a beer can.. and says to dog.. now go to the other Pub and deliver this can of beer to the owner.
the dog runs away and drinks the beer himself.. SLURP. and owner sirver wont notice untill IF the client notice and complains to pub..

(iam working on a simple example... stay tooned.)


jfk EO-11110(Posted 2006) [#10]
well - cheers then!

Buggy - yes, Port 1 is no good. Try as suggested, a port higher than 7000, eg. 8000. Don't worry too much about reserved services in that range.

2.) Add a delay in your client. Your client is currently sending data faster than the server can print it to the screen. Add a reasonable delay, like 100ms or something.

3.) Remove the pauses in the server to make it responf quickly: replace
strStream=AcceptTCPStream(svrGame)
If strStream Then 
 Print ReadString$(strStream)
 Delay 2000
Else 
 Print "No word from Apollo X yet ..."
 Delay 1000
endif

eg. by this:
strStream=AcceptTCPStream(svrGame)
If strStream Then 
 Print ReadString$(strStream)
 Delay 30
Else 
 Print "No word from Apollo X yet ..."
 Delay 30
endif


BTW you said you want to start the client first. so you have to send only when the connection is established.
try this client instead (edit the port to 8000 in the server too!):
Graphics 640, 480
image = LoadImage("bullet.PNG")
clientgame = OpenTCPStream("127.0.0.1", 8000)
If clientgame <> 0
 Print "Connection!"
Else
 Print "Error!"
EndIf
WaitKey()
SetBuffer BackBuffer()
While Not KeyDown(1)
 Cls
 x = Rand(200, 400)
 If clientgame <> 0
  Text 30, 0, "Connected"
  WriteInt(clientgame, x) ; send only when connected
 Else
  Text 30, 0, "No Connection"
  clientgame = OpenTCPStream("127.0.0.1", 8000) ; try again!
 EndIf
 DrawImage image, x, 0
 Flip
Wend
End


Basicly I think it would be better to receive an answer from the server after each transmission, to make sure to detect when the server is no longer online.


Buggy(Posted 2006) [#11]
Thanks so much... you seem to be the only one who's helping!


Dreamora(Posted 2006) [#12]
Using port 6112 is a quite usefull solution. Thats the Blizzard Battle.net port which many firewalls have special rules for preset :-)


Buggy(Posted 2006) [#13]
For some reason, the line:

While eof(x)

     ....

Wend


is giving me some difficulties. It seems to run forever, without exiting the loop.


Buggy(Posted 2006) [#14]
x always seems to be 271.


jfk EO-11110(Posted 2006) [#15]
probably you need to have a closer look at the manual. of course you need to read the stream there.

while not eof(x)
my_data=readint(x)
wend

tho I have to say X is a bad name for a stream. You should use something more descriptive, like eg:

stream=AcceptTCPStream(game)

and then:

while eof(stream)=0
x=readint(stream)
wend

so now you can use x as the thing it was meant to be, a coordinate, and you won't confuse it with a stream handle any longer.


Wings(Posted 2006) [#16]
Here is a simple an ultra fast tcp engine for you.

http://server.voidrpg.com/network test2.rar

include source and free to use as you wish.. you may even sell it :) )