TCP Coding with external app

Blitz3D Forums/Blitz3D Programming/TCP Coding with external app

Jaime(Posted 2006) [#1]
Hi!

I've given a job to revive an old application a company had, but they don't have the server, just the client. While I was clear it was hard to do, I really want to give it a try.

So, my approach was emulate the functions the "server" had to do, and I thought it was going to be possible with CreateServer and AcceptTCPStream, but I've been able only to receive the data into my server, but anything I send back to the client is being ignored.

Every example I've found use WriteLine or WriteString, and it's not useful for me because I need to work at byte and word level. The funny part is ReadAvail always report 0, even when LData$ returns data.

I've coded something like this:

svrLogin = CreateTCPServer(18000)
strStreamLogin	= AcceptTCPStream(svrLogin)
<Repeat here>
   LData$ = Trim(Str(ReadShort(strStreamLogin)))
<Repeat end>

That part works, I get a sequence of data sent by the client, but after that I create a bank and then I fill it with some pokes (byte, word and float depending the size) and finally I send the data like this:

PacketData = CreateBank(50)
<some pokes here>
WriteBytes PacketData,strStreamLogin,1,50

And nothing! It's just like the client isn't recognizing anything, in fact, Blitz3D seems to not sending anything (because the Client status doesn't change, the transmit meter don't move a pixel even if I use a WriteByte inside a 'Forever' loop)

Am I doing something wrong?

Thanks in advance!


Carolinaaa(Posted 2006) [#2]
I don't know if it has something to do with your problem or not, but i will explain it to you for the case it has got relation; at least with UDP, i was unable to send data if the sendudpmsg command was placed in a function (no errors showed, just that it didn't send data); i had to place it in the main body of the program for it to work.


Also, if the client is placed in a computer of the company, it is probably behind a router or a proxy; maybe that the administrator of the net has to open expressely that port in concrete or forward it to a concrete computer.

I have had the same problem with routers, the server is directly accesible with a public ip, but when a client behind a router connects to the server, the server logs the connection and sends an answer to the client, in many cases the client behind a router doesn't receive the server's answer, even when it is supposed that if the client sent a request previously, the firewall would allow the answer, but in many cases it is not truth, the firewall of the router blocks the answer from the server. The only solution is opening the ports used.


Jaime(Posted 2006) [#3]
Hi! Thanks for your reply :)

Since they don't allow me to bring the app to my home, I need to code the server inside the company, so I work locally (I set the client to connect to 127.0.0.1) and just to be sure, I opened the port 18000 in Windows Firewall.

I haven't included the code inside functions because I was testing B3D capabilities to connect to the client, but it's nice to know about that problem before I go into it :)

It's very strange, I've even tested with a packet sniffer... it reports the Client sending data, but it's not receiving anything... :(


Damien Sturdy(Posted 2006) [#4]
Perhaps they have "Local Echo" off? (I think I saw that somewhere...)


Jaime(Posted 2006) [#5]
Hi.. I came back

Every thing I tried failed, so I moved to VBasic Net to do some tests (haven't used it before) and I used this code:

Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Class TCPSrv
    Shared Sub Main()
        Const portNumber As Integer = 18000
        Dim MyServer As IPAddress = CType(Dns.GetHostEntry("127.0.0.1").AddressList(0), IPAddress)

        Dim myTCPListener As New TcpListener(MyServer, portNumber)
        myTCPListener.Start()
        Console.WriteLine("Waiting for connection...")
        Try
            'Accept the pending client connection and return a TcpClient initialized for communication. 
            Dim tcpClient As TcpClient = myTCPListener.AcceptTcpClient()
            Console.WriteLine("Connection accepted.")
            ' Get the stream
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            ' Read the stream into a byte array
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            ' Return the data received from the client to the console.
            Dim clientdata As String = Encoding.ASCII.GetString(bytes)
            Console.WriteLine(("Client sent: " + clientdata))
            Dim responseString As String = "Connected to server."
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            Console.WriteLine(("Message Sent /> : " + responseString))
            'Any communication with the remote client using the TcpClient can go here.
            'Close TcpListener and TcpClient.
            tcpClient.Close()
            myTCPListener.Stop()
            Console.WriteLine("exit")
            Console.ReadLine()
        Catch e As Exception
            Console.WriteLine(e.ToString())
            Console.ReadLine()
        End Try
    End Sub
End Class


And it works!! The client application receives the data I'm sending back... So... Is it a Blitz3D bug?? :(


Wings(Posted 2006) [#6]
C andblitz are verry difirent.

WriteBytes command is sending raw data.
You will have to send exactly the bytes in right order.
or client will not recon it.

like find a document of how data is suppose to transmitt.

iam usaly coding voidrpg.com and usaly i have 1 static size of packets.

thoe this C thing looks pretty easy to learn :)


Jaime(Posted 2006) [#7]
Hi! I know the exactly the bytes order I need to send, and I'm monitoring the Client app with a packet sniffer which shows me when and what data I'm sending... And I'm sure Blitz it's not sending anything through the TCP Stream.

My guess is that another Blitz app can read the data, but a standard TCP app created in other language cannot, just like if it were only Blitz compatible... Who knows (well, Mark should ;)


Wings(Posted 2006) [#8]
hmm iam only using writebyteS its send instant. 1ms :)