]>
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()) {
114 case wxSocketBase::EVT_READ
:
117 sock
->Read((char *)&c
, 1);
122 case wxSocketBase::EVT_LOST
:
123 printf("Destroying socket\n");
124 wxPendingDelete
.Append(sock
);
129 printf("OnSockRequest Exiting\n");
130 sock
->SetNotify(wxSocketBase::REQ_READ
| wxSocketBase::REQ_LOST
);
133 void MyFrame::OnSockRequestServer(wxSocketEvent
& evt
)
135 /* this routine gets called from within the
136 waiting socket thread, i.e. here we are
137 not in the main GUI thread and thus we
138 must not call any GUI function here. */
139 /* Wrong ! This routine is called by the main GUI thread
140 because the main GUI thread received a signal from the other
141 thread using wxEvent::ProcessThreadEvent */
144 wxSocketServer
*server
= (wxSocketServer
*) evt
.Socket();
146 printf("OnSockRequestServer OK\n");
147 printf("OnSockRequest (Main = %d) (event = %d)\n",wxThread::IsMain(), evt
.SocketEvent());
149 sock2
= server
->Accept();
154 sock2
->SetFlags(wxSocketBase::SPEED
);
156 sock2
->SetEventHandler(*this, SKDEMO_SOCKET
);
157 server
->SetNotify(wxSocketBase::REQ_ACCEPT
);
160 // My frame Constructor
161 MyFrame::MyFrame(wxFrame
*frame
):
162 wxFrame(frame
, -1, "wxSocket sample (server)", wxDefaultPosition
,
169 wxSocketHandler::Master();
171 sock
= new wxSocketServer(addr
);
172 wxSocketHandler::Master().Register(sock
);
173 sock
->SetNotify(wxSocketBase::REQ_ACCEPT
);
174 sock
->SetEventHandler(*this, SKDEMO_SOCKET_SERV
);
175 sock
->SetFlags(wxSocketBase::SPEED
);
188 // Intercept menu commands
189 void MyFrame::Menu_Exit(wxCommandEvent
& WXUNUSED(event
))
194 void MyFrame::ExecTest1(wxSocketBase
*sock_o
)
196 char *buf
= new char[50];
199 l
= sock_o
->Read(buf
, 50).LastCount();
200 sock_o
->Write(buf
, l
);
203 void MyFrame::UpdateStatus(int incr
)
207 sprintf(s
, "%d clients connected", nb_clients
);