"Fossies" - the Fresh Open Source Software Archive 
Member "netxms-3.8.166/src/server/tools/nxadm/comm.cpp" (23 Feb 2021, 2423 Bytes) of package /linux/misc/netxms-3.8.166.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "comm.cpp" see the
Fossies "Dox" file reference documentation.
1 /*
2 ** NetXMS - Network Management System
3 ** Local administration tool
4 ** Copyright (C) 2003-2015 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** $module: comm.cpp
21 **
22 **/
23
24 #include "nxadm.h"
25
26 /**
27 * Max message size
28 */
29 #define MAX_MSG_SIZE 4194304
30
31 /**
32 * Global data
33 */
34 SOCKET g_hSocket = -1;
35 DWORD g_dwRqId = 1;
36
37 /**
38 * Message receiver
39 */
40 static SocketMessageReceiver *s_receiver = NULL;
41
42 /**
43 * Connect to server
44 */
45 BOOL Connect()
46 {
47 struct sockaddr_in sa;
48
49 // Create socket
50 g_hSocket = CreateSocket(AF_INET, SOCK_STREAM, 0);
51 if (g_hSocket == INVALID_SOCKET)
52 {
53 printf("Error creating socket\n");
54 return FALSE;
55 }
56
57 // Fill in address structure
58 memset(&sa, 0, sizeof(sa));
59 sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
60 sa.sin_family = AF_INET;
61 sa.sin_port = htons(LOCAL_ADMIN_PORT);
62
63 // Connect to server
64 if (connect(g_hSocket, (struct sockaddr *)&sa, sizeof(sa)) == -1)
65 {
66 printf("Cannot establish connection with server\n");
67 closesocket(g_hSocket);
68 g_hSocket = -1;
69 return FALSE;
70 }
71
72 // Initialize receiver
73 s_receiver = new SocketMessageReceiver(g_hSocket, 4096, MAX_MSG_SIZE);
74 return TRUE;
75 }
76
77 /**
78 * Disconnect from server
79 */
80 void Disconnect()
81 {
82 delete_and_null(s_receiver);
83 if (g_hSocket != -1)
84 {
85 shutdown(g_hSocket, 2);
86 closesocket(g_hSocket);
87 g_hSocket = -1;
88 }
89 }
90
91 /**
92 * Send message to server
93 */
94 void SendMsg(NXCPMessage *pMsg)
95 {
96 NXCP_MESSAGE *pRawMsg;
97
98 pRawMsg = pMsg->serialize();
99 SendEx(g_hSocket, pRawMsg, ntohl(pRawMsg->size), 0, NULL);
100 free(pRawMsg);
101 }
102
103 /**
104 * Receive message
105 */
106 NXCPMessage *RecvMsg()
107 {
108 MessageReceiverResult result;
109 return s_receiver->readMessage(INFINITE, &result);
110 }