Re: wxEuphoria--is_connected(atom socket)
- Posted by Matt Lewis <matthewwalkerlewis at gmail.com> Apr 05, 2005
- 437 views
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