Help with Chisock

New to FreeBASIC? Post your questions here.
Post Reply
Topito216
Posts: 18
Joined: Dec 11, 2009 13:13
Location: Spain

Help with Chisock

Post by Topito216 »

Hello to all.

First of all. Thanks to all members of this comunity open source because is wonderfull the power of this lenguaje.

My level of knowledge is basic but I did some programs to modify text files.
Now I'd like send theses files or the data inside them by tcp from a computer to another. Windows xp and a local ethernet net with ip 192.168.0.2 and 192.168.0.3 for example.

I have started to learn the library chisock because I have readed that it's the simpliest way to comunicate.

I have problems to build a simple program that send information an other that receive it. I have compiled the following example in order to learn but it's imposible.

Code: Select all

#include "chisock.bi"

using chi

dim as socket sock

var socket_result = sock.client( "www.google.com", socket.socket.PORT.HTTP )
if( socket_result ) then print translate_error( socket_result )

sock.put_HTTP_request( "www.google.com/search?q=FreeBASIC" )

'' Good luck reading this - google uses no whitespace :)
print sock.get_until( "" )
sleep 
I got the error:
connect_to_google.bas(7) error 89: No matching overloaded function, CLIENT() in 'var socket_result = sock.client( "www.google.com", socket.socket.PORT.HTTP )'

Build error(s)


I dont konw whats the problem.

By other side:

I suppose the steps for to build a prgram that sends information is:

Receiver
Create connection by TCP client.
Listening untiil it receive.
Close TCP client connection

Sender
Create connection by TCP server.
Send information.
Close TCP server connection.


Someone could write me a simply example sender program an other receiver program in order I learn how it works.

I have tried to implement the following function in order to connect to a webpage but I dont know how.

Code: Select all

#include "chisock.bi"
Using chi




'function client( byref server as string,ByVal port as integer ) as integer
	
	'o Create a TCP client with the hostname "server" on the port "port"
	

		Dim as chi.socket foo
		if( foo.client( "www.google.com", 80 ) <> chi.SOCKET_OK ) then print "Error!": End If
		
		if( foo.client( "www.google.com", 80 ) = chi.SOCKET_OK )Then Print foo.client( "www.google.com", 80 )
		
	'* Returns "chi.SOCKET_OK" on success, else error.
'End Function

Sleep

I think it must be very easy to do a comunicaton using sockets o chisock but my low knowledge of functions, OOP, and librarys make me imposible to make a program that it works.

I supose the client program will be something simila to this:

Code: Select all

#include "chisock.bi"
Using chi




'function client( byref server as string,ByVal port as integer ) as integer
	
	'o Create a TCP client with the hostname "server" on the port "port"


Dim as chi.socket foo
foo.client( "192.168.0.35",23)
		
Dim as string message
if( foo.get( message ) = TRUE ) Then print "I got: " & message:End if


I supose the server program will be something simila to this:

Code: Select all

#include "chisock.bi"
Using chi




'function client( byref server as string,ByVal port as integer ) as integer
	
	'o Create a TCP client with the hostname "server" on the port "port"


Dim as chi.socket foo
foo.server(23)



		
		'' I'm gonna give 'em a message!
Dim as string message = "Hello my fellow earthling!"
if( foo.put( message ) = FALSE ) Then Print "That didn't go over well. :("

My problem is that there is not or I cant find a manual and reading the examples text files is too dificult to mo to discover what does every command.

For example it will be good a program that send data to the microsoft software "Hyperterminal" tha I have in my laptop.

Thanks in advance.


Javier.
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

Did you get another error before that one like this....
(Untitled).bas(7) error 8: Undefined symbol, socket in 'var socket_result = sock.client( "www.google.com", socket.socket.PORT.HTTP )'
Change the port constant to socket.PORT.HTTP

-Vince
Topito216
Posts: 18
Joined: Dec 11, 2009 13:13
Location: Spain

little by little

Post by Topito216 »

Thanks.

Now that program works. I am learning little by little because I have to investigate but I could do my two first program (sender and receiver).

Client.

Code: Select all

#include "chisock.bi"
using chi

#include "dir.bi" '' provides constants to match the attributes against
'Const attrib_mask = fbNormal Or fbHidden Or fbSystem Or fbDirectory ' = &h37
dim as socket sock
Dim As UInteger out_attr '' unsigned integer to hold retrieved attributes
Dim As String fname '' file/directory name returned with
Dim As String outname
Dim As String cadena
Dim As Integer filecount, dircount
Dim txt As String
Screen 20
WindowTitle "Programa cliente para enviar archivos de Gravimetria"
Print "Programa cliente para enviar archivos de Gravimetria"
Input "Nombre y extension de los archivos (ejemplo *.059): ",cadena
fname = Dir(cadena, , out_attr) '' Get first file name/attributes, according to supplied file spec and attribute mask
var res = sock.client( "localhost", 10000 )
if( res ) Then	print translate_error( res )end If
Do Until Len(fname) = 0 '' loop until Dir returns empty string
If (fname <> ".") And (fname <> "..") Then '' ignore current and parent directory entries

Open fname For Input As #1
Do Until EOF(1)               '' loop until we have reached the end of the file
    Line Input #1,txt               '' read a line of text ...
   sock.put(txt)
   Sleep 1
   
   Loop
Close #1


    End If
    
fname = Dir(out_attr) '' find next name/attributes
Loop

Print "Terminado"
Sleep 1000
Server:

Code: Select all

#include "chisock.bi"

'' start this, then start tcp_client

using chi

Screen 20
WindowTitle "Programa servidor que recibe archivos de Gravimetria"
Print "Programa servidor para recibir archivos de Gravimetria"





dim as socket sock,sock2
Dim txt As String
var res = sock.server( 10000 )
if( res ) Then	print translate_error( res )End if

' res = sock2.server( 10000 )
'if( res ) then	print translate_error( res )End if

sock.listen( )


do
	if( sock.get(txt) ) Then  Print txt:	end if
	
	'Sleep 100
loop until multikey(1)
Thanks again.
Post Reply