]>
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. */
106 wxSocketBase
*sock
= evt
.Socket();
108 printf("OnSockRequest OK\n");
109 printf("OnSockRequest (event = %d)\n",evt
.SocketEvent());
110 switch (evt
.SocketEvent()) {
111 case wxSocketBase::EVT_READ
:
114 sock
->Read((char *)&c
, 1);
119 case wxSocketBase::EVT_LOST
:
120 printf("Destroying socket\n");
121 wxPendingDelete
.Append(sock
);
125 printf("OnSockRequest Exiting\n");
126 sock
->SetNotify(wxSocketBase::REQ_READ
| wxSocketBase::REQ_LOST
);
129 void MyFrame::OnSockRequestServer(wxSocketEvent
& evt
)
131 /* this routine gets called from within the
132 waiting socket thread, i.e. here we are
133 not in the main GUI thread and thus we
134 must not call any GUI function here. */
137 wxSocketServer
*server
= (wxSocketServer
*) evt
.Socket();
139 printf("OnSockRequestServer OK\n");
140 printf("OnSockRequest (event = %d)\n",evt
.SocketEvent());
142 sock2
= server
->Accept();
146 sock2
->SetFlags(wxSocketBase::SPEED
);
148 sock2
->SetEventHandler(*this, SKDEMO_SOCKET
);
149 server
->SetNotify(wxSocketBase::REQ_ACCEPT
);
152 // My frame Constructor
153 MyFrame::MyFrame(wxFrame
*frame
):
154 wxFrame(frame
, -1, "wxSocket sample (server)", wxDefaultPosition
,
161 wxSocketHandler::Master();
163 sock
= new wxSocketServer(addr
);
164 wxSocketHandler::Master().Register(sock
);
165 sock
->SetNotify(wxSocketBase::REQ_ACCEPT
);
166 sock
->SetEventHandler(*this, SKDEMO_SOCKET_SERV
);
167 sock
->SetFlags(wxSocketBase::SPEED
);
180 // Intercept menu commands
181 void MyFrame::Menu_Exit(wxCommandEvent
& WXUNUSED(event
))
186 void MyFrame::ExecTest1(wxSocketBase
*sock_o
)
188 char *buf
= new char[50];
191 l
= sock_o
->Read(buf
, 50).LastCount();
192 sock_o
->Write(buf
, l
);
195 void MyFrame::UpdateStatus(int incr
)
199 sprintf(s
, "%d clients connected", nb_clients
);