]>
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"
28 #if defined(__WXMOTIF__) || defined(__WXGTK__)
29 #include "mondrian.xpm"
32 // Define a new application type
33 class MyApp
: public wxApp
40 // Define a new frame type
41 class MyFrame
: public wxFrame
48 MyFrame(wxFrame
*frame
);
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
);
57 #define SKDEMO_QUIT 101
58 #define SKDEMO_SOCKET_SERV 102
59 #define SKDEMO_SOCKET 103
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
)
69 // `Main program' equivalent, creating windows and returning main app frame
70 bool MyApp::OnInit(void)
72 // Create the main frame window
73 MyFrame
*frame
= new MyFrame(NULL
);
76 frame
->SetIcon(wxICON(mondrian
));
79 wxMenu
*file_menu
= new wxMenu
;
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
);
86 // Make a panel with a message
87 (void)new wxPanel(frame
, 0, 0, 300, 100);
92 // Return the main frame window
96 extern wxList wxPendingDelete
;
98 void MyFrame::OnSockRequest(wxSocketEvent
& evt
)
100 wxSocketBase
*sock
= evt
.Socket();
102 wxPrintf(_T("OnSockRequest OK\n"));
103 wxPrintf(_T("OnSockRequest (event = %d)\n"),evt
.SocketEvent());
104 switch (evt
.SocketEvent()) {
108 sock
->Read((char *)&c
, 1);
114 wxPrintf(_T("Destroying socket\n"));
115 wxPendingDelete
.Append(sock
);
120 wxPrintf(_T("Invalid event !\n"));
122 wxPrintf(_T("OnSockRequest Exiting\n"));
125 void MyFrame::OnSockRequestServer(wxSocketEvent
& evt
)
128 wxSocketServer
*server
= (wxSocketServer
*) evt
.Socket();
130 wxPrintf(_T("OnSockRequestServer OK\n"));
131 wxPrintf(_T("OnSockRequest (event = %d)\n"), evt
.SocketEvent());
133 sock2
= server
->Accept(FALSE
);
138 sock2
->SetFlags(wxSocketBase::NONE
);
140 sock2
->SetEventHandler(*this, SKDEMO_SOCKET
);
141 sock2
->SetNotify(wxSOCKET_INPUT_FLAG
| wxSOCKET_LOST_FLAG
);
144 // My frame Constructor
145 MyFrame::MyFrame(wxFrame
*frame
):
146 wxFrame(frame
, -1, "wxSocket sample (server)", wxDefaultPosition
,
154 sock
= new wxSocketServer(addr
);
155 sock
->SetNotify(wxSOCKET_CONNECTION_FLAG
);
156 sock
->SetEventHandler(*this, SKDEMO_SOCKET_SERV
);
157 sock
->SetFlags(wxSocketBase::SPEED
);
169 // Intercept menu commands
170 void MyFrame::Menu_Exit(wxCommandEvent
& WXUNUSED(event
))
175 void MyFrame::ExecTest1(wxSocketBase
*sock_o
)
177 char *buf
= new char[50];
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
);
188 void MyFrame::UpdateStatus(int incr
)
192 wxSprintf(s
, _T("%d clients connected"), nb_clients
);