]>
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 wxPrintf(_T("OnSockRequest OK\n"));
112 wxPrintf(_T("OnSockRequest (event = %d)\n"),evt
.SocketEvent());
113 switch (evt
.SocketEvent()) {
117 sock
->Read((char *)&c
, 1);
123 wxPrintf(_T("Destroying socket\n"));
124 wxPendingDelete
.Append(sock
);
129 wxPrintf(_T("Invalid event !\n"));
131 wxPrintf(_T("OnSockRequest Exiting\n"));
134 void MyFrame::OnSockRequestServer(wxSocketEvent
& evt
)
136 /* this routine gets called from within the
137 waiting socket thread, i.e. here we are
138 not in the main GUI thread and thus we
139 must not call any GUI function here. */
140 /* Wrong ! This routine is called by the main GUI thread
141 because the main GUI thread received a signal from the other
142 thread using wxEvent::ProcessThreadEvent */
145 wxSocketServer
*server
= (wxSocketServer
*) evt
.Socket();
147 wxPrintf(_T("OnSockRequestServer OK\n"));
148 wxPrintf(_T("OnSockRequest (Main = %d) (event = %d)\n"),wxThread::IsMain(), evt
.SocketEvent());
150 sock2
= server
->Accept();
155 sock2
->SetFlags(wxSocketBase::NONE
);
157 sock2
->SetEventHandler(*this, SKDEMO_SOCKET
);
158 sock2
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
161 // My frame Constructor
162 MyFrame::MyFrame(wxFrame
*frame
):
163 wxFrame(frame
, -1, "wxSocket sample (server)", wxDefaultPosition
,
171 sock
= new wxSocketServer(addr
);
172 sock
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
173 sock
->SetEventHandler(*this, SKDEMO_SOCKET_SERV
);
174 sock
->SetFlags(wxSocketBase::SPEED
);
187 // Intercept menu commands
188 void MyFrame::Menu_Exit(wxCommandEvent
& WXUNUSED(event
))
193 void MyFrame::ExecTest1(wxSocketBase
*sock_o
)
195 char *buf
= new char[50];
198 l
= sock_o
->Read(buf
, 50).LastCount();
199 sock_o
->Write(buf
, l
);
200 l
= sock_o
->Read(buf
, 50).LastCount();
201 sock_o
->Write(buf
, l
);
206 void MyFrame::UpdateStatus(int incr
)
210 wxSprintf(s
, _T("%d clients connected"), nb_clients
);