]> git.saurik.com Git - wxWidgets.git/blob - samples/wxsocket/server.cpp
small changes here and there
[wxWidgets.git] / samples / wxsocket / server.cpp
1 /*
2 * File: server.cpp
3 * Purpose: wxSocket: server demo
4 * Author: LAVAUX Guilhem
5 * Created: June 1997
6 * CVS Id: $Id$
7 * Copyright: (C) 1997, LAVAUX Guilhem
8 */
9
10 #ifdef __GNUG__
11 #pragma implementation
12 #pragma interface
13 #endif
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #ifndef WX_PRECOMP
23 #include "wx/wx.h"
24 #endif
25
26 #include "wx/socket.h"
27
28 #if defined(__WXMOTIF__) || defined(__WXGTK__)
29 #include "mondrian.xpm"
30 #endif
31
32 // Define a new application type
33 class MyApp: public wxApp
34 { public:
35 bool OnInit(void);
36 };
37
38 class MyServer;
39
40 // Define a new frame type
41 class MyFrame: public wxFrame
42 {
43 DECLARE_EVENT_TABLE()
44 public:
45 wxSocketServer *sock;
46 int nb_clients;
47
48 MyFrame(wxFrame *frame);
49 virtual ~MyFrame();
50 void Menu_Exit(wxCommandEvent& evt);
51 void OnSockRequest(wxSocketEvent& evt);
52 void OnSockRequestServer(wxSocketEvent& evt);
53 void ExecTest1(wxSocketBase *sock_o);
54 void UpdateStatus(int incr);
55 };
56
57 #define SKDEMO_QUIT 101
58 #define SKDEMO_SOCKET_SERV 102
59 #define SKDEMO_SOCKET 103
60
61 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
62 EVT_MENU(SKDEMO_QUIT, MyFrame::Menu_Exit)
63 EVT_SOCKET(SKDEMO_SOCKET_SERV, MyFrame::OnSockRequestServer)
64 EVT_SOCKET(SKDEMO_SOCKET, MyFrame::OnSockRequest)
65 END_EVENT_TABLE()
66
67 IMPLEMENT_APP(MyApp)
68
69 // `Main program' equivalent, creating windows and returning main app frame
70 bool MyApp::OnInit(void)
71 {
72 // Create the main frame window
73 MyFrame *frame = new MyFrame(NULL);
74
75 // Give it an icon
76 frame->SetIcon(wxICON(mondrian));
77
78 // Make a menubar
79 wxMenu *file_menu = new wxMenu;
80
81 file_menu->Append(SKDEMO_QUIT, "E&xit");
82 wxMenuBar *menu_bar = new wxMenuBar;
83 menu_bar->Append(file_menu, "File");
84 frame->SetMenuBar(menu_bar);
85
86 // Make a panel with a message
87 (void)new wxPanel(frame, 0, 0, 300, 100);
88
89 // Show the frame
90 frame->Show(TRUE);
91
92 // Return the main frame window
93 return TRUE;
94 }
95
96 extern wxList wxPendingDelete;
97
98 void MyFrame::OnSockRequest(wxSocketEvent& evt)
99 {
100 wxSocketBase *sock = evt.Socket();
101
102 wxPrintf(_T("OnSockRequest OK\n"));
103 wxPrintf(_T("OnSockRequest (event = %d)\n"),evt.SocketEvent());
104 switch (evt.SocketEvent()) {
105 case wxSOCKET_INPUT:
106 unsigned char c;
107
108 sock->Read((char *)&c, 1);
109 if (c == 0xbe)
110 ExecTest1(sock);
111
112 break;
113 case wxSOCKET_LOST:
114 wxPrintf(_T("Destroying socket\n"));
115 wxPendingDelete.Append(sock);
116 UpdateStatus(-1);
117 return;
118 break;
119 default:
120 wxPrintf(_T("Invalid event !\n"));
121 }
122 wxPrintf(_T("OnSockRequest Exiting\n"));
123 }
124
125 void MyFrame::OnSockRequestServer(wxSocketEvent& evt)
126 {
127 wxSocketBase *sock2;
128 wxSocketServer *server = (wxSocketServer *) evt.Socket();
129
130 wxPrintf(_T("OnSockRequestServer OK\n"));
131 wxPrintf(_T("OnSockRequest (event = %d)\n"), evt.SocketEvent());
132
133 sock2 = server->Accept(FALSE);
134 if (sock2 == NULL)
135 return;
136
137 UpdateStatus(1);
138 sock2->SetFlags(wxSocketBase::NONE);
139 sock2->Notify(TRUE);
140 sock2->SetEventHandler(*this, SKDEMO_SOCKET);
141 sock2->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
142 }
143
144 // My frame Constructor
145 MyFrame::MyFrame(wxFrame *frame):
146 wxFrame(frame, -1, "wxSocket sample (server)", wxDefaultPosition,
147 wxSize(300, 200))
148 {
149 wxIPV4address addr;
150 addr.Service(3000);
151
152 // Init all
153
154 sock = new wxSocketServer(addr);
155 sock->SetNotify(wxSOCKET_CONNECTION_FLAG);
156 sock->SetEventHandler(*this, SKDEMO_SOCKET_SERV);
157 sock->SetFlags(wxSocketBase::SPEED);
158 sock->Notify(TRUE);
159 nb_clients = 0;
160 CreateStatusBar(1);
161 UpdateStatus(0);
162 }
163
164 MyFrame::~MyFrame()
165 {
166 delete sock;
167 }
168
169 // Intercept menu commands
170 void MyFrame::Menu_Exit(wxCommandEvent& WXUNUSED(event))
171 {
172 Close(TRUE);
173 }
174
175 void MyFrame::ExecTest1(wxSocketBase *sock_o)
176 {
177 char *buf = new char[50];
178 size_t l;
179
180 l = sock_o->Read(buf, 50).LastCount();
181 sock_o->Write(buf, l);
182 l = sock_o->Read(buf, 50).LastCount();
183 sock_o->Write(buf, l);
184
185 delete[] buf;
186 }
187
188 void MyFrame::UpdateStatus(int incr)
189 {
190 wxChar s[30];
191 nb_clients += incr;
192 wxSprintf(s, _T("%d clients connected"), nb_clients);
193 SetStatusText(s);
194 }