Info on Multiplayer and Gnet?
BlitzMax Forums/BlitzMax Programming/Info on Multiplayer and Gnet?
| ||
I'm looking for some good info on the TCP/UDP and GNet commands. I've looked in archives and didn't see anything. |
| ||
Well, here is my quick tutorial on Gnet: http://www.blitzbasic.com/Community/posts.php?topic=51171 for tcp/udp socket programming, you have this one: http://beej.us/guide/bgnet/ Explains a lot and is just great, imo. |
| ||
Or if you want to it simple: TNet-Basic Starting Tutorial for TNet |
| ||
Sorry I wasn't more specific. Anyone have some info on how to use the "existing" commands in BMax to get me started using UDP? |
| ||
Here's something I started messing with. FYI I don't know exactly what method works. I know how it worked in Blitz3D and it seems to have changed slightly. Can someone give me some pointers? Strict Graphics 640,480,0 Const backlog:Int = 10 ' Create UDP Socket Global iSocket:TSocket = CreateUDPSocket() BindSocket(iSocket,0) While Not KeyHit(KEY_ESCAPE) Cls If KeyHit(KEY_UP) Local out:Int = WriteStream(iSocket) WriteLine out,"hi" EndIf knock = SocketListen(iSocket,backlog) If knock Then Print "hello";End DrawText DottedIP(SocketLocalIP(iSocket)),5,5 DrawText SocketLocalPort(iSocket),5,25 Flip Wend End |
| ||
I don't use streams for UDP so I can't help you with that, if that is really what you want, but I guess you simply want to send data, in which this might be your solution. With UDP I send packages, the opposite with TCP, where I split the stream up into packages. Here is the command for sending UDP with the core blitzmax commands (nothing to do with TNet, except this is what TNet uses): sendto_( Socket._socket, Bytes:Byte Ptr, Byte_length, flags, Dest_IP, Dest_Port ) Socket: The socket you want to use, with UDP you can use one socket for ALL your messages on one port. _socket is the field in the Socket type that the sendto_ function wants. Bytes: a Byte Ptr to the first byte of the data you want to send. Byte_Length: The length of the data to send. flags: If you don't know this, don't use it. Dest_IP: The ip in int form of the receiver of your message Dest_Port: The port on the receiving computer you want to send to. You will save a lot of work if you use TNet, and in the next version you will be able to use blitz sockets combined with TNet. SocketDataSendTo( Socket:TSocket, Data$, Dest_IP, Dest_Port). |
| ||
I want to learn to do this myself but there's no examples of how to successfully use the sockets in the documentation. I don't necessarily want to use Streams, I just want to use whatever works fast for a multiplayer game. I know for UDP it's connectionless and you just make a packet and shoot it to the IP/Port. I'm just trying to get that working in it's most basic form. Where is the sendto_() command in the docs? I try to use sendto_() and it says "Identifier Not Found". |
| ||
So my line would look like?sendto_(iSocket:TSocket,0,5,0,"127.0.0.1",SocketLocalPort(iSocket)) But if the message I want to send is contained in SEND$ how do I actually send it? |
| ||
sendto_( iSocket, varptr SEND, len(SEND), 0, HostIP("127.0.0.1"), SocketLocalPort(iSocket) ) I think. Never used it myself. |
| ||
Tried that and is says: Unable to Convert 'TSocket' to 'Int' So it looks like the first spot can't be a TSocket? |
| ||
http://www.blitzbasic.com/Community/posts.php?topic=50093 Don't think it's been added yet but still works fine here. |
| ||
Thanks for the help so far guys but I'm still stuck banging my head against the wall. :( I try to put my socket in the first spot of sendto_() and it says it needs to be and Integer. :( :( :( |
| ||
Try going from SuperStrict to Strict. You can only explicitely convert object types in SuperStrict mode. Alternatively (and I'm only guessing here), you can do the explicit conversion by using Int(iSocket:TSocket). |
| ||
Socket._socket Let us say your socket is like this: Global iSocket:TSocket iSocket = CreateSocket() local Bytes[2] Bytes[0] = 65 'Letter A Bytes[1] = 66 'Letter B sendto_( iSocket._socket,Bytes,Bytes.length,0,HostIp("127.0.0.1"), SocketWhichYouWantToSendToOnYourLocalComputer ) Should send "AB" to your own computer because you use 127.0.0.1 and then to whatever port you specify. |
| ||
Thanks for the info Wave. Are you using Banks to convert strings to bytes? |
| ||
no Also right now TNet only support 8bit Strings. |
| ||
I guess you want to receive to: recvfrom_( Socket._socket, Bytes, BytesToReceive , flags, Sender_IP , Sender_Port ) |
| ||
How does this look for preparing a packet for transmittal?chat:Packet = Packet.Create("Can you hear me Dr. Watson?") Print chat.UnPack() Type Packet Field bytes[] Field size:Int Function Create:Packet(msg:String) Local p:Packet = New Packet p.size = Len(msg) p.bytes = New Int[p.size] For Local i = 0 To p.size-1 p.bytes[i] = Asc( Mid$( msg,i+1,1 ) ) Next Return p End Function Method UnPack:String() Local msg:String For Local i:Int = 0 To size-1 msg = msg + Chr$( bytes[i] ) Next Return msg End Method Method Send() 'sendto_(iSocket._socket,p.bytes,p.size,0,p.IP,p.Port) End Method End Type |
| ||
This is how I would have "packed" the package to bytes. Also when you do the Asci/byte math: For Local i = 0 To p.size-1 p.bytes[i] = Asc( Mid$( msg,i+1,1 ) ) Next This is the new BlitzMax Way. Nicer and easier to read once you get used to it. For Local i = 0 To p.size-1 p.bytes[i] = msg[i] Next Also I also think you can use Until instead: For Local i = 0 until p.size p.bytes[i] = msg[i] Next |