]>
git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/server.cpp
3 * Purpose: wxSocket: server demo
4 * Author: LAVAUX Guilhem
7 * Copyright: (C) 1997, LAVAUX Guilhem
11 #pragma implementation
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
26 #include "wx/socket.h"
27 #include "wx/thread.h"
29 #if defined(__WXMOTIF__) || defined(__WXGTK__)
30 #include "mondrian.xpm"
33 // Define a new application type
34 class MyApp
: public wxApp
41 // Define a new frame type
42 class MyFrame
: public wxFrame
49 MyFrame(wxFrame
*frame
);
51 void Menu_Exit(wxCommandEvent
& evt
);
52 void OnSockRequest(wxSocketEvent
& evt
);
53 void OnSockRequestServer(wxSocketEvent
& evt
);
54 void ExecTest1(wxSocketBase
*sock_o
);
55 void UpdateStatus(int incr
);
58 #define SKDEMO_QUIT 101
59 #define SKDEMO_SOCKET_SERV 102
60 #define SKDEMO_SOCKET 103
62 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
63 EVT_MENU(SKDEMO_QUIT
, MyFrame::Menu_Exit
)
64 EVT_SOCKET(SKDEMO_SOCKET_SERV
, MyFrame::OnSockRequestServer
)
65 EVT_SOCKET(SKDEMO_SOCKET
, MyFrame::OnSockRequest
)
70 // `Main program' equivalent, creating windows and returning main app frame
71 bool MyApp::OnInit(void)
73 // Create the main frame window
74 MyFrame
*frame
= new MyFrame(NULL
);
77 frame
->SetIcon(wxICON(mondrian
));
80 wxMenu
*file_menu
= new wxMenu
;
82 file_menu
->Append(SKDEMO_QUIT
, "E&xit");
83 wxMenuBar
*menu_bar
= new wxMenuBar
;
84 menu_bar
->Append(file_menu
, "File");
85 frame
->SetMenuBar(menu_bar
);
87 // Make a panel with a message
88 (void)new wxPanel(frame
, 0, 0, 300, 100);
93 // Return the main frame window
97 extern wxList wxPendingDelete
;
99 void MyFrame::OnSockRequest(wxSocketEvent
& evt
)
101 /* this routine gets called from within the
102 waiting socket thread, i.e. here we are
103 not in the main GUI thread and thus we
104 must not call any GUI function here. */
105 /* Wrong ! This routine is called by the main GUI thread
106 because the main GUI thread received a signal from the other
107 thread using wxEvent::ProcessThreadEvent */
109 wxSocketBase
*sock
= evt
.Socket();
111 printf("OnSockRequest OK\n");
112 printf("OnSockRequest (event = %d)\n",evt
.SocketEvent());
113 switch (evt
.SocketEvent()) {
117 sock
->Read((char *)&c
, 1);
123 printf("Destroying socket\n");
124 wxPendingDelete
.Append(sock
);
129 printf("OnSockRequest Exiting\n");
132 void MyFrame::OnSockRequestServer(wxSocketEvent
& evt
)
134 /* this routine gets called from within the
135 waiting socket thread, i.e. here we are
136 not in the main GUI thread and thus we
137 must not call any GUI function here. */
138 /* Wrong ! This routine is called by the main GUI thread
139 because the main GUI thread received a signal from the other
140 thread using wxEvent::ProcessThreadEvent */
143 wxSocketServer
*server
= (wxSocketServer
*) evt
.Socket();
145 printf("OnSockRequestServer OK\n");
146 printf("OnSockRequest (Main = %d) (event = %d)\n",wxThread::IsMain(), evt
.SocketEvent());
148 sock2
= server
->Accept();
153 sock2
->SetFlags(wxSocketBase::NONE
);
155 sock2
->SetEventHandler(*this, SKDEMO_SOCKET
);
156 sock2
->SetNotify(GSOCK_INPUT_FLAG
| GSOCK_LOST_FLAG
);
159 // My frame Constructor
160 MyFrame::MyFrame(wxFrame
*frame
):
161 wxFrame(frame
, -1, "wxSocket sample (server)", wxDefaultPosition
,
169 sock
= new wxSocketServer(addr
);
170 sock
->SetNotify(GSOCK_CONNECTION_FLAG
);
171 sock
->SetEventHandler(*this, SKDEMO_SOCKET_SERV
);
172 sock
->SetFlags(wxSocketBase::SPEED
);
185 // Intercept menu commands
186 void MyFrame::Menu_Exit(wxCommandEvent
& WXUNUSED(event
))
191 void MyFrame::ExecTest1(wxSocketBase
*sock_o
)
193 char *buf
= new char[50];
196 l
= sock_o
->Read(buf
, 50).LastCount();
197 sock_o
->Write(buf
, l
);
200 void MyFrame::UpdateStatus(int incr
)
204 sprintf(s
, "%d clients connected", nb_clients
);