Can't read from stream

BlitzMax Forums/BlitzMax Beginners Area/Can't read from stream

daqx(Posted 2006) [#1]
Hi,

when I execute the following code, BlitzMax prints this error message: Error reading from stream. What's wrong?

[CODE]
Strict

' **************************************************
' **************************************************


Function ipint:Int(umwandeln$)
Local teil1:Int = Int(Instr(umwandeln$,"."))
Local teil2:Int = Int(Instr(umwandeln$,".",teil1+1))
Local teil3:Int = Int(Instr(umwandeln$,".",teil2+1))
Local temp1:Int = Int(Mid(umwandeln$,1,teil1-1))
Local temp2:Int = Int(Mid(umwandeln$,teil1+1,teil2-1))
Local temp3:Int = Int(Mid(umwandeln$,teil2+1,teil3-1))
Local temp4:Int = Int(Mid(umwandeln$,teil3+1))
Local zurueck:Int = (temp1 Shl 24) + (temp2 Shl 16) + (temp3 Shl 8) + temp4
Return zurueck
End Function

' **************************************************
' **************************************************


Local UDP:TSocket
Local Stream:TSocketStream
Local Temp:String
Local Char:Byte
Local TempTime:Int

Const timeout:Int = 1500

' **************************************************
' **************************************************

UDP = CreateUDPSocket()
Stream = CreateSocketStream(UDP)
BindSocket(UDP,27015)

If Not UDP Then Notify("Error: Socket konnte nicht initialisiert werden.")

If Not ConnectSocket(UDP,ipint("85.14.231.10"),27015) Then Notify("Error: Keine Verbindung zum Server.")

' **************************************************
' **************************************************

Local bank:TBank = CreateBank(0)
Local strm:TBankStream = CreateBankStream(bank)

strm.WriteByte($FF)
strm.WriteByte($FF)
strm.WriteByte($FF)
strm.WriteByte($FF)
strm.WriteByte($54)
strm.WriteByte($53)
strm.WriteByte($6F)
strm.WriteByte($75)
strm.WriteByte($72)
strm.WriteByte($63)
strm.WriteByte($65)
strm.WriteByte($20)
strm.WriteByte($45)
strm.WriteByte($6E)
strm.WriteByte($67)
strm.WriteByte($69)
strm.WriteByte($6E)
strm.WriteByte($65)
strm.WriteByte($20)
strm.WriteByte($51)
strm.WriteByte($75)
strm.WriteByte($65)
strm.WriteByte($72)
strm.WriteByte($79)
strm.WriteByte($00)

WriteBank(bank,Stream,0,bank.Size())

bank = Null
strm.Close()
strm = Null

TempTime = MilliSecs()

While MilliSecs()-TempTime < timeout

If UDP.ReadAvail() > 0 Then

Local bt:Byte = Stream.ReadByte()

Print Chr(bt)

End If

Wend

Print "Timeout"

CloseStream(Stream)
CloseSocket(UDP)
[/CODE]


Byteemoz(Posted 2006) [#2]
I think you have to use "recvfrom_" when using udp-sockets:
http://blitzmax.com/Community/posts.php?topic=60822#678788
-- Byteemoz

Edit: This seems to work:
...

While MilliSecs()-TempTime < timeout

   If UDP.ReadAvail() > 0 Then
   
      Print (UDPReadByte(UDP))
   
   End If 

Wend

Print "Timeout"

CloseStream(Stream)
CloseSocket(UDP)

Function UDPReadByte:Int(Socket:TSocket)
	Local buf:Byte
	Local sender_ip:Int, sender_port:Int
	recvfrom_ ..
		Socket._socket, Varptr buf, 1, .. ' sockethandle, buffer, buffersize
		0, .. ' flags
		sender_ip, sender_port
		
	Return buf
EndFunction