1. wxEuphoria--is_connected(atom socket)

About the function is_connected() in wxEuphoria:

Is this the function to use to find out whether the computer is connected
to the internet?  If NO, then what is the function to use for this purpose?

If YES, then:

Am I correct in guessing that it returns 1 or 0? 1 is connected, 0 is not?

What should I use for a socket?

(I don't even know what a socket is, even after reading some
documentation about it.)

new topic     » topic index » view message » categorize

2. Re: wxEuphoria--is_connected(atom socket)

Jerry Story wrote:
> 
> About the function is_connected() in wxEuphoria:
> 
> Is this the function to use to find out whether the computer is connected
> to the internet?  If NO, then what is the function to use for this purpose?
> 
> If YES, then:
> 
> Am I correct in guessing that it returns 1 or 0? 1 is connected, 0 is not?
> 
> What should I use for a socket?
> 
> (I don't even know what a socket is, even after reading some
> documentation about it.)
> 


is_connected() tells you whether the socket itself is connected with another
socket.  A socket can be either a server or a client.  A server waits and 
listens for a client to ask to connect.  To see if you're connected to the
internet, you could try to connect with some well known site (e.g., google).

I'm definitely not an expert on networking.  I think most of my knowledge is
contained in the server.exw and client.exw demos, which somehow managed to
elude inclusion in v0.5.0.  I'll fix that for the next release.  They're in
v0.4.0, though, and I've appended them below

-------------------------------------------------------
-- begin server.exw:
without warning
include wxEuphoria.e
include wxButton.e
include wxText.e
with trace
include wxNet.e

constant
main		= create( wxFrame, {0, -1, "Network Server Demo"}),
server_data	= create( wxTextCtrl, {main, -1, "", -1, -1, -1, -1,
wxTE_MULTILINE})

object void
atom server, listen, sip, cip, sstream, cstream, http
server = 0


procedure socket_event( atom this, atom event_type, atom id, atom event )
	sequence text
	atom socket
	
	event_type = get_socket_event( event )

	if event_type = wxSOCKET_INPUT then
		socket = socket_from_event( event )
		set_socket_notify( socket, {wxSOCKET_LOST_FLAG})
		set_flags( socket, wxSOCKET_NOWAIT )
		text = socket_read( socket )
		set_flags( socket, wxSOCKET_WAITALL )
set_text( server_data, sprintf("%s(#%08x): \"%s\"\n",{get_text_value(
server_data ), socket, text} ))
set_socket_notify( socket, {wxSOCKET_INPUT_FLAG, wxSOCKET_CONNECTION_FLAG,
wxSOCKET_LOST_FLAG})
		
	elsif event_type = wxSOCKET_CONNECTION then
		socket = socket_accept(server, 0)
set_socket_notify( socket, {wxSOCKET_INPUT_FLAG, wxSOCKET_CONNECTION_FLAG,
wxSOCKET_LOST_FLAG})
		set_event_handler( main, -1, wxEVT_SOCKET, routine_id("socket_event"))
		socket_event_handler( socket, main, -1 )
set_text( server_data, sprintf( "%sConnection: #%08x\n",{get_text_value(
server_data ), socket}))
		
	elsif event_type = wxSOCKET_LOST then
		socket = socket_from_event( event )
set_text( server_data, sprintf( "%sConnection Lost: #%08x\n",{get_text_value(
server_data ), socket}))
		delete_instance( socket )
		
	end if
	
end procedure

procedure setup()

	sip = create( wxIPV4address, {})
	void = set_ip_service( sip, 3000 )
	server = create( wxSocketServer, sip )

	set_socket_notify( server, {wxSOCKET_CONNECTION_FLAG, wxSOCKET_LOST_FLAG})
	set_event_handler( main, -1, wxEVT_SOCKET, routine_id("socket_event"))
	socket_event_handler( server, main, -1 )	
end procedure
setup()

wxMain( main )
-- end server.exw

-----------------------------------------------
-- begin client.exw
without warning
include wxEuphoria.e
include wxButton.e
include wxText.e
include wxSizer.e
with trace
include wxNet.e

constant
main		= create( wxFrame, {0, -1, "Network Client Demo"}),
win			= create( wxPanel, main ),
client_data	= create( wxTextCtrl, {win, -1, "", -1, -1, -1, -1,
wxTE_MULTILINE}),
send		= create( wxButton, {win, -1, "Send Data via Sockets"})


object void
atom client, cip

procedure send_sockets( atom this, atom event_type, atom id, atom event )
	sequence text
	if client then
		text = get_text_value( client_data )
		socket_write( client, text )
	end if
end procedure
set_event_handler( send, -1, wxEVT_COMMAND_BUTTON_CLICKED,
routine_id("send_sockets"))




procedure setup()
	atom sizer
	sizer = create( wxBoxSizer, wxVERTICAL )
	add_window_to_sizer( sizer, client_data, 1, wxTOP + wxGROW, 10 )
	add_window_to_sizer( sizer, send, 0, wxTOP, 10 )

	set_sizer( win, sizer )

	cip = create( wxIPV4address, {})
	void = set_ip_host( cip, "127.0.0.1" )
	void = set_ip_service( cip, 3000 )
	client = create( wxSocketClient, wxSOCKET_WAITALL)
	if socket_connect( client, cip, 1 ) = 0 then
		void = message_box( "Could not connect!", "Error", wxICON_ERROR )
		client = 0
	end if	
end procedure
setup()

wxMain( main )
-- end client.exw
----------------------------------------------------


Matt Lewis

new topic     » goto parent     » topic index » view message » categorize

3. Re: wxEuphoria--is_connected(atom socket)

Ahh, the age old question, What is a socket, and why do they call it a socket?

Well, in the simplistic of terms, we'll use a Regular phone as an example.

Think of a Client, as you calling a friend.  Your friend would be considered the
server.  Basically, it's up to your friend weither he wants to talk with you or
not, so he would either answer the phone, or not answer it.  This terminology
applies to a server.  When you develop a client, it places a call to the server,
requesting a confrence.  Depending on weither or not the server is listening for
the call, or willing to accept your client's call, it'll answer it, and begin
the transmission of data, or in the case of the Phone call, pick up, and start
talking.

Why is it Called a socket?

Well, it's called a socket, because it was the ideal back in 88 or so, that
when you make a connection between two computers, your basically plugging
one computer into another, and that other one has to have a "socket" that the
connection can fit into.  And the same follows for the socket that you use
today in TCP/IP Communications.  Basically the socket will either connect,
if it can fit into the socket on the server, or it'll fail.  Simple as that,
eh?

Basically, to determin weither or not your connected to the internet, as
pre-suggested by Matt, you follow the method of something like this:

include wxEuphoria.e
include wxNet.e

object void

constant cip = create( wxIPV4address, {} ),
         client = create( wxSocketClient, wxSOCKET_WAITALL )

void = set_ip_host(cip, "64.233.167.104") -- ping www.google.com
void = set_ip_service(cip, 80) -- Web Server Port
if socket_connect(client,cip,1) = 0 then
    puts(1,"You are not connected to the internet!")
else
    delete_instance(client)
    puts(1,"You are connected to the internet!")
end if
machine_proc(26,0) -- wait_key()


That would be the easy example, and un-tested, as I'm still not familiar
with wxEuphoria yet.

Mario Steele
http://enchantedblade.trilake.net
Attaining World Dominiation, one byte at a time...

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu