first understand that a string and an array can not be used the same way.
you can not print an array of bytes and a string the same way or read them the same way. Arrays of bytes have to be read or printed one byte at a time unlike strings. You can access each byte of the string through the corresponding index and print its "byte value". if you want to see the actual character you have to convert the byte to a char such as:
this will print 74 and the letter "J"
with this in mind I have somewhat fixed your code to work first with bytes:
and then with strings:
As you can see the one with string is a lot simpler but only because it is done by the compiler, unlike the one with bytes that everything has to be coded.
note that I have not tested so there might be some logic errors.
[edited]
look at this code:
http://www.blitzmax.com/Community/posts.php?topic=81750#921145
In principle it does the same thing but with tbanks and is alot simpler
you can not print an array of bytes and a string the same way or read them the same way. Arrays of bytes have to be read or printed one byte at a time unlike strings. You can access each byte of the string through the corresponding index and print its "byte value". if you want to see the actual character you have to convert the byte to a char such as:
name:string = "Jesse" print name[0] print chr(name[0])
this will print 74 and the letter "J"
with this in mind I have somewhat fixed your code to work first with bytes:
SuperStrict Graphics 640, 480 Local client:OClient = OClient.Create("127.0.0.1", 6121) While Not KeyHit(KEY_ESCAPE) client.onRecieve() Wend Rem Function ToString(bytes:Byte[]) Return string.bytestostring(bytes,bytes.length) End Function EndRem Type OClient Field host:String Field port:Int Field socket:TSocket Field stream:TSocketstream Field buffer:TBytes Function Create:OClient(host:String, port:Int) Local client:OClient = New OClient client.host = host client.port = port client.socket = CreateTCPSocket() ConnectSocket(client.socket, HostIp(host), port) If client Then client.stream = CreateSocketStream(client.socket) Return client Else Return Null End If End Function Method onPacketReceive(packetType:Byte, packetData:Byte[]) Local str:String For Local i:Int = 0 Until packetData.length str :+ Chr(packetData[i]) Next Print "Received packet type: " + packetType + " with data: " + str End Method Method onRecieve() If SocketReadAvail(Self.socket) > 0 Self.buffer.Append(Self._ReadBytes()) Local size:Int = SizeOf(Self.buffer.bytes) If size > 3 Then Local packetSize:Short = Self.buffer.ReadShort(1) If size - 3 >= packetSize Then Local packetType:Byte = Self.buffer.ReadByte(0) Local packetData:Byte[] = Self.buffer.ReadBytes(3, packetSize) Self.buffer.Pop(size) Self.onPacketReceive(packetType, packetData) End If End If End If End Method Method _ReadBytes:Byte[]() Local bytes:Byte[] While SocketReadAvail(Self.socket) Local l:Int = SocketReadAvail(Self.socket) bytes = New Byte[l] For Local i:Int = 0 Until l bytes[i] = ReadByte(Self.stream) Next Wend Return bytes End Method End Type Type TBytes Field bytes:Byte[] Method Write(bytes:Byte[]) Self.bytes = bytes End Method Method Append(bytes:Byte[]) Self.bytes = Self.bytes + bytes End Method Method Pop(length:Int) Self.bytes = Self.bytes[length..(SizeOf(Self.bytes)-length)] End Method Method Read:Byte[]() Return Self.bytes End Method Method ReadBytes:Byte[](offset:Int, length:Int) Local temp:Byte[] = New Byte[length] For Local i:Int = offset Until offset+length temp[i-offset] = Self.bytes[i] Next Return temp End Method Method ReadByte:Byte(offset:Int) Return Self.bytes[offset] End Method Method ReadShort:Short(offset:Int) Local str:String For Local i:Int = offset To offset+1 str :+ Chr(bytes[i]) Next Return Short(str) End Method Method ReadInt:Int(offset:Int) Local str:String For Local i:Int = offset To offset+3 str :+ Chr(bytes[i]) Next Return Int(str) End Method Method ReadLong:Long(offset:Int) Local str:String For Local i:Int = offset To offset+3 str :+ Chr(bytes[i]) Next Return Long(str) End Method Method ReadFloat:Float(offset:Int) Local str:String For Local i:Int = offset To offset+3 str :+ Chr(bytes[i]) Next Return Float(str) End Method Method ReadDouble:Double(offset:Int) Local str:String For Local i:Int = offset To offset+7 str :+ Chr(bytes[i]) Next Return Double(str) End Method End Type
and then with strings:
SuperStrict Graphics 640, 480 Local client:OClient = OClient.Create("127.0.0.1", 6121) While Not KeyHit(KEY_ESCAPE) client.onRecieve() Wend Rem Function ToString(bytes:Byte[]) Local str:String Local char:String Local i:Int = 0 While i< bytes.length char = Chr(bytes[i..i+1]) str = str + char i = i + 1 Wend Return str End Function EndRem Type OClient Field host:String Field port:Int Field socket:TSocket Field stream:TSocketstream Field buffer:TBytes Function Create:OClient(host:String, port:Int) Local client:OClient = New OClient client.host = host client.port = port client.socket = CreateTCPSocket() ConnectSocket(client.socket, HostIp(host), port) If client Then client.stream = CreateSocketStream(client.socket) Return client Else Return Null End If End Function Method onPacketReceive(packetType:String, packetData:String) Print "Received packet type: " + packetType + " with data: " + packetData[..SizeOf(packetData)] End Method Method onRecieve() If SocketReadAvail(Self.socket) > 0 Self.buffer.Append(Self._ReadBytes()) Local size:Int = SizeOf(Self.buffer.bytes) If size > 3 Then Local packetSize:Int = Self.buffer.ReadShort(1) If size - 3 >= packetSize Then Local packetType:String = Chr(Self.buffer.ReadByte(0)) Local packetData:String = Self.buffer.ReadBytes(3, packetSize) Self.buffer.Pop(size) Self.onPacketReceive(packetType, packetData) End If End If End If End Method Method _ReadBytes:String() Local bytes:String While SocketReadAvail(Self.socket) > 0 bytes = bytes + Chr(ReadByte(Self.stream)) Wend Return bytes End Method End Type Type TBytes Field bytes:String Method Write(bytes:Byte) Self.bytes = bytes End Method Method Append(bytes:String) Self.bytes = Self.bytes + bytes End Method Method Pop(length:Int) Self.bytes = Self.bytes[length..(SizeOf(Self.bytes)-length)] End Method Method Read:String() Return Self.bytes End Method Method ReadBytes:String(offset:Int, length:Int) Return Self.bytes[offset..(offset+length)] End Method Method ReadByte:Byte(offset:Int) Return Self.bytes[offset] End Method Method ReadShort:Short(offset:Int) Return Short(Self.bytes[offset..(offset+2)]) End Method Method ReadInt:Int(offset:Int) Return Int(Self.bytes[offset..(offset+4)]) End Method Method ReadLong:Long(offset:Int) Return Long(Self.bytes[offset..(offset+4)]) End Method Method ReadFloat:Float(offset:Int) Return Float(Self.bytes[offset..(offset+4)]) End Method Method ReadDouble:Double(offset:Int) Return Double(Self.bytes[offset..(offset+8)]) End Method End Type
As you can see the one with string is a lot simpler but only because it is done by the compiler, unlike the one with bytes that everything has to be coded.
note that I have not tested so there might be some logic errors.
[edited]
look at this code:
http://www.blitzmax.com/Community/posts.php?topic=81750#921145
In principle it does the same thing but with tbanks and is alot simpler