Logo ROOT   6.14/05
Reference Guide
udpserver.c
Go to the documentation of this file.
1 /* fpont 12/99 */
2 /* pont.net */
3 /* udpserver.c */
4 
5 /* Converted to echo client/server with select() (timeout option).
6  See testTUDPSocket.C */
7 /* Compile with: gcc udpserver.c -o udpserver
8 /* on Windows: cl -nologo -Z7 -MD -GR -EHsc udpserver.c */
9 /* 3/30/05 John Schultz */
10 
11 #include <stdlib.h>
12 #include <sys/types.h>
13 #ifdef _WIN32
14 #include <winsock2.h>
15 #include <windows.h>
16 #else
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
20 #include <netdb.h>
21 #include <unistd.h> /* close() */
22 #endif
23 #include <stdio.h>
24 #include <string.h> /* memset() */
25 
26 #ifdef _WIN32
27 #pragma comment(lib,"Ws2_32.lib")
28 #endif
29 
30 #define LOCAL_SERVER_PORT 1500
31 #define MAX_MSG 100
32 
33 int main(int argc, char *argv[]) {
34 
35  int sd, rc, n, flags;
36  unsigned cliLen;
37  struct sockaddr_in cliAddr, servAddr;
38  char msg[MAX_MSG];
39 
40 #ifdef _WIN32
41  WSADATA wsaData = {0};
42  int iResult = 0;
43  /* Initialize Winsock */
44  iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
45  if (iResult != 0) {
46  wprintf(L"WSAStartup failed: %d\n", iResult);
47  return -1;
48  }
49  /* socket creation */
50  sd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
51 #else
52  /* socket creation */
53  sd=socket(AF_INET, SOCK_DGRAM, 0);
54 #endif
55  if (sd<0) {
56  printf("%s: cannot open socket \n",argv[0]);
57  exit(1);
58  }
59 
60  /* bind local server port */
61  servAddr.sin_family = AF_INET;
62  servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
63  servAddr.sin_port = htons(LOCAL_SERVER_PORT);
64  rc = bind (sd, (struct sockaddr *) &servAddr, sizeof(servAddr));
65  if (rc<0) {
66  printf("%s: cannot bind port number %d \n", argv[0], LOCAL_SERVER_PORT);
67  exit(1);
68  }
69  printf("%s: waiting for data on port UDP %u\n", argv[0], LOCAL_SERVER_PORT);
70 
71 /* BEGIN jcs 3/30/05 */
72  flags = 0;
73 /* END jcs 3/30/05 */
74 
75  /* server infinite loop */
76  while (1) {
77 
78  /* init buffer */
79  memset(msg,0x0,MAX_MSG);
80 
81  /* receive message */
82  cliLen = sizeof(cliAddr);
83  n = recvfrom(sd, msg, MAX_MSG, flags, (struct sockaddr *) &cliAddr,
84  &cliLen);
85 
86  if (n<0) {
87  printf("%s: cannot receive data \n",argv[0]);
88  continue;
89  }
90  /* print received message */
91  printf("%s: from %s:UDP%u : %s \n", argv[0],
92  inet_ntoa(cliAddr.sin_addr), ntohs(cliAddr.sin_port), msg);
93 
94 /* BEGIN jcs 3/30/05 */
95 
96 #ifdef _WIN32
97  Sleep(1000);
98 #else
99  sleep(1);
100 #endif
101  sendto(sd, msg, n, flags, (struct sockaddr *)&cliAddr, cliLen);
102 
103 /* END jcs 3/30/05 */
104 
105  }/* end of server infinite loop */
106  return 0;
107 }
int main(int argc, char *argv[])
Definition: udpserver.c:33
static constexpr double L
#define LOCAL_SERVER_PORT
Definition: udpserver.c:30
R__EXTERN C unsigned int sleep(unsigned int seconds)
#define MAX_MSG
Definition: udpserver.c:31
const Int_t n
Definition: legend1.C:16