Please login or register.

Login with username, password and session length

News:

Registration is only required if you want to post, and is not needed to read any posts. To avoid excess 'spam' accounts, all accounts where no posts have been made will be deleted after two weeks. Please register again if you wish to post.

Author Topic: Sockets  (Read 13187 times)

May 14, 2011, 03:38:13 PM
  • Member
  • **
  • Posts: 1
    • View Profile
Hello.

I am working on a project which i need to get the location information of ownship. What i am trying to do is that I find a simple server client code in Blitz3D and put it into Bridge Command source code, bc4.bb

What i did is i put client code into Bridge Command and run server code and Bridge Command in order to see if it connects successfully. Although it works on Blitz3D IDE (both server and client code), it doesnt work on source code.

Link to socket code : http://www.blitzbasic.com/Community/posts.php?topic=50025

What should i do? Any help would be nice.

Thanks in advance.

May 18, 2011, 09:06:35 PM
Reply #1
  • Administrator
  • Member
  • *****
  • Posts: 146
    • View Profile
Which code on that page are you trying to use? It appears that most of the code posted is for BlitzMax rather than Blitz3d.

I have implemented similar communication between Bridge Command and the Map Controller. For this, I decided to use UDP rather than TCP for this. TCP guarantees eventual delivery of the packet, and in the right order. This means that each packet may be resent many times, resulting in a significant slow-down. With UDP, you have none of this overhead, so the process is much faster, but there is no guarantee that the packets will come through in the right order (or at all).

I decided that getting some packets through quickly was more important, but you do need to discard any packets that come through in the wrong order.

As an example, here is some simple, bi-directional code.

Code: [Select]
AppTitle "Sender"

;set up address and ports (note that if the sender and client run on different computers, then only 1 port is required)
ipAddress = dotToInt("127.0.0.1")
udpSendPort = 40002
udpRecvPort = 40001

;make stream
udpStream=CreateUDPStream(udpRecvPort)
UDPTimeouts 30 ;wait 30ms to receive see if any message comes in

i=0

While Not KeyHit(1)

i=i+1

If udpStream <> 0
WriteString udpStream, "Test send " + Str(i)

SendUDPMsg(udpStream, ipAddress, udpSendPort)

;receive too
recvIP = RecvUDPMsg(udpStream)

If recvIP <> 0
Print ReadString(udpStream)
EndIf

Else
Print "No stream created"
EndIf

Wend

CloseUDPStream udpStream

End

Function DotToInt%(ip$)
ip$=Trim$(ip$)

ip1=split$(ip$,1,".")
ip2=split$(ip$,2,".")
ip3=split$(ip$,3,".")
ip4=split$(ip$,4,".")

Return ip1 Shl 24 + ip2 Shl 16 + ip3 Shl 8 + ip4
End Function

Function Split$(in$,n,delim$)
;splits string by one character delimiter
;returns nth component

For i = 1 To n
;find location of next delimiter
;find part to left of delimiter
pos=Instr(in$,delim$)
If pos=0
;no further delimiter found
If i<>n
lh$="" ;there is no nth part of the list, so return ""
Else
lh$=in$ ;we have found the nth part, which is the last in the list
EndIf
Exit ;break out of loop
EndIf

lh$ = Left(in$,pos-1)
;discard part to left of delimiter
in$ = Right(in$,Len(in$)-pos)
Next
Return lh$
End Function

Code: [Select]
AppTitle "Receiver"

;set up address and ports (note that if the sender and client run on different computers, then only 1 port is required)
ipAddress = dotToInt("127.0.0.1")
udpSendPort = 40001
udpRecvPort = 40002

;make stream
udpStream=CreateUDPStream(udpRecvPort)
UDPTimeouts 30 ;wait 30ms to receive see if any message comes in

i=0

While Not KeyHit(1)

i = i + 1

If udpStream <> 0
recvIP = RecvUDPMsg(udpStream)

If recvIP <> 0
Print ReadString(udpStream)
EndIf

;send a message too
WriteString udpStream, "Test reply " + Str(i)
SendUDPMsg(udpStream, ipAddress, udpSendPort)

Else
Print "No stream created"
EndIf

Wend

CloseUDPStream udpStream

End

Function DotToInt%(ip$)
ip$=Trim$(ip$)

ip1=split$(ip$,1,".")
ip2=split$(ip$,2,".")
ip3=split$(ip$,3,".")
ip4=split$(ip$,4,".")

Return ip1 Shl 24 + ip2 Shl 16 + ip3 Shl 8 + ip4
End Function

Function Split$(in$,n,delim$)
;splits string by one character delimiter
;returns nth component

For i = 1 To n
;find location of next delimiter
;find part to left of delimiter
pos=Instr(in$,delim$)
If pos=0
;no further delimiter found
If i<>n
lh$="" ;there is no nth part of the list, so return ""
Else
lh$=in$ ;we have found the nth part, which is the last in the list
EndIf
Exit ;break out of loop
EndIf

lh$ = Left(in$,pos-1)
;discard part to left of delimiter
in$ = Right(in$,Len(in$)-pos)
Next
Return lh$
End Function