[Gambas-user] IRC, telnet etc

Daniel Campos daniel.campos at ...338...
Sun Feb 29 14:45:37 CET 2004


> 
> PUBLIC SUB MySock_Read()
> 
>   DIM S AS String
>   IF MySock.Status=Net.Connected THEN
>       READ #MySock,S,Lof(MySock)
>       getline(S)
>  END IF
> END
> 
> PRIVATE SUB getline(S AS String)
> intc AS Integer
> stra AS String
> strr AS String
> 
> S = strr & S
> FOR intc = 1 TO Len(S)
> 'look for spaces or carriage returns in a line of received text:
> IF Mid(S, intc, 1) = Chr(13) OR Mid(S, intc, 1) = Chr(10) THEN
> stra = Mid(S, 1, intc)
> 'IF the data IS one complete line, add to the listbox/buffer:
> ListBox1.Add (stra)
> S = Mid(S, intc + 1)
> intc = 1
> END IF
> NEXT
> strr = S
>  
> END
> 

Well, to find a complete line wasting less resources, you could use
Peek() method, which reads data from socket, but keeping it in the
buffer for the next time:

PUBLIC SUB MySock_Read()

	DIM S As String
	IF MySock.Status=Net.Connected THEN
		S=MySock.Peek() ' We read current buffer
		' If there's a carry return...
		IF INSTR(S,Chr(13)) or INSTR(S,Chr(10)) THEN 
			' we read just the line, keeping the
			' rest in the buffer
			LINE INPUT #MySock,S
			getline(S)
			' you don't have to test Chr(13) again
 			' in getline()
		END IF
	END IF
END 

You could also add a routine to test the lenght of the buffer, to
discard it if it is too long (to prevent your program of beeing crashed
by a malicious IRC hacker, for instance)

Regards,

Daniel Campos






More information about the User mailing list