TSocketStream
BlitzMax Forums/BlitzMax Programming/TSocketStream
| ||
SuperStrict Local Server_Host:String = "irc.blitzed.org" Local Server_IP:Int = HostIp(Server_Host) Local Server_Port:Int = 6667 If Server_IP = 0 Print "Could not resolve Servername!" End EndIf Local Server_Socket:TSocket = CreateTCPSocket() Local Server_Stream:TSocketStream = CreateSocketStream(Server_Socket, True) If ConnectSocket( Server_Socket, Server_IP, Server_Port) Print "Connected succesfully!" Local data:String data = "USER TestUser MaxIRC "+Server_Host+" :MaxTest" WriteLine(Server_Stream, data) data = "NICK TestUser" WriteLine(Server_Stream, data) While Not Eof(Server_Stream) data = ReadLine(Server_Stream) Print data Wend Else Print "STFU!" EndIf Problem: It never gets out of the While...Wend Is there any misinterpreting from my side, coz i looked into "socketstream.bmx" and there is written: ... Method Eof() If Not _socket Return True If _socket.Connected() Return False Close Return True End Method ... then i tried to get the number of readable byte by: Print SocketReadAvail(Server_Socket) but it returns everytime 0 - even if there IS something to read Any tips for me? :) tia robo |
| ||
The IRC protocol is asynchronous and there is no guarantee when you will actually receive a response from the server and you will also receive data from the connection without having sent anything yourself before. It is quite natural that your code never leaves the While-Loop; the server does not disconnect your client or close the connection. Instead, it will send "PING!" requests to your client which you should respond to with sending "PONG!" answers. If you don't do that, the server will run into a timeout and -then- disconnect. If you wait long enough, this also happens with your code. It basically comes down to that you have to adept your code to the asynchronous nature of the protocol. |
| ||
Yep, i know that it's async, so i have to poll it recently. But my prob in the above mentioned example is: How to determine the amount of chars to read from the Stream / Socket. In the meantime i tried following, also with no positive effect: SuperStrict Local Server_Host:String = "irc.blitzed.org" Local Server_IP:Int = HostIp(Server_Host) Local Server_Port:Int = 6667 If Server_IP = 0 Print "Could not resolve Servername!" End EndIf Local Server_Socket:TSocket = CreateTCPSocket() If ConnectSocket( Server_Socket, Server_IP, Server_Port) = False RuntimeError "No Connection!" EndIf Local Server_SocketStream:TSocketStream = CreateSocketStream(Server_Socket, True) Local Server_Stream:TStream = OpenStream(Server_SocketStream) If Server_Stream Print "Connected succesfully!" Local data:String data = "USER TestUser MaxIRC "+Server_Host+" :MaxTest" WriteLine(Server_Stream, data) data = "NICK TestUser" WriteLine(Server_Stream, data) SeekStream(Server_Stream,0) While StreamPos(Server_Stream)<StreamSize(Server_Stream) data = ReadLine(Server_Stream) Print StreamSize(Server_Stream) If(data<>"") Print data EndIf Wend CloseStream(Server_Stream) CloseStream(Server_SocketStream) CloseSocket(Server_Socket) Else Print "STFU!" EndIf |
| ||
The later the night.... ...anyway - now it seems to work, needed a simple timeout ;) Local Server_Host:String = "irc.blitzed.org" Local Server_IP:Int = HostIp(Server_Host) Local Server_Port:Int = 6667 Const timeout:Int = 200 If Server_IP = 0 Print "Could not resolve Servername!" End EndIf Local Server_Socket:TSocket = CreateTCPSocket() If ConnectSocket( Server_Socket, Server_IP, Server_Port) = False RuntimeError "No Connection!" EndIf Local Server_SocketStream:TSocketStream = CreateSocketStream(Server_Socket, True) Local Server_Stream:TStream = OpenStream(Server_SocketStream) If Server_Stream Print "Connected succesfully!" Local data:String data = "USER TestUser MaxIRC "+Server_Host+" :MaxTest" WriteLine(Server_Stream, data) data = "NICK TestUser" WriteLine(Server_Stream, data) Local end_time:Int = MilliSecs() + timeout While end_time > MilliSecs() While SocketReadAvail(Server_Socket)>0 'Print "Size: "+ StreamSize(Server_Stream) 'Print "Pos : "+ StreamPos(Server_Stream) data = ReadString(Server_Stream, SocketReadAvail(Server_Socket)) If(data<>"") Print data EndIf Wend Wend CloseStream(Server_Stream) CloseStream(Server_SocketStream) CloseSocket(Server_Socket) Else Print "STFU!" EndIf |