ReactOS  0.4.12-dev-918-g6c6e7b8
srltest.c
Go to the documentation of this file.
1 /* srltest.c */
2 
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/time.h>
6 #include <sys/socket.h>
7 #include <sys/wait.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdlib.h>
14 #include <time.h>
15 #include <ctype.h>
16 #ifdef CAN_USE_SYS_SELECT_H
17 # include <sys/select.h>
18 #endif
19 
20 #include "sio.h"
21 
22 static void
23 ServeOneClient(int sockfd, struct sockaddr_in *cliAddr)
24 {
25  char buf[80], cliAddrStr[64];
26  char bbuf[320];
27  int nread, nwrote, i;
28  SReadlineInfo srl;
29 
30  printf("subserver[%d]: started, connected to %s.\n", (int) getpid(),
31  AddrToAddrStr(cliAddrStr, sizeof(cliAddrStr), cliAddr, 1, "<%h:%p>")
32  );
33 
34  if (InitSReadlineInfo(&srl, sockfd, bbuf, sizeof(bbuf), 5) < 0) {
35  fprintf(stderr, "subserver[%d]: InitSReadlineInfo error: %s\n",
36  (int) getpid(), strerror(errno));
37  exit(1);
38  }
39  for (;;) {
40  nread = SReadline(&srl, buf, sizeof(buf));
41  if (nread == 0) {
42  break;
43  } else if (nread == kTimeoutErr) {
44  printf("subserver[%d]: idle\n", (int) getpid());
45  continue;
46  } else if (nread < 0) {
47  fprintf(stderr, "subserver[%d]: read error: %s\n",
48  (int) getpid(), strerror(errno));
49  break;
50  }
51  for (i=0; i<nread; i++)
52  if (islower(buf[i]))
53  buf[i] = toupper(buf[i]);
54  nwrote = SWrite(sockfd, buf, nread, 15);
55  if (nwrote < 0) {
56  fprintf(stderr, "subserver[%d]: write error: %s\n",
57  (int) getpid(), strerror(errno));
58  break;
59  }
60  }
61  (void) SClose(sockfd, 10);
62  printf("subserver[%d]: done.\n", (int) getpid());
63  exit(0);
64 } /* ServeOneClient */
65 
66 
67 
68 static void
70 {
71  int sockfd, newsockfd;
72  struct sockaddr_in cliAddr;
73  int pid;
74 
75  sockfd = SNewStreamServer(port, 3, kReUseAddrYes, 3);
76  if (sockfd < 0) {
77  perror("Server setup failed");
78  exit(1);
79  }
80 
81  printf("server[%d]: started.\n", (int) getpid());
82  for(;;) {
83  while (waitpid(-1, NULL, WNOHANG) > 0) ;
84  newsockfd = SAccept(sockfd, &cliAddr, 5);
85  if (newsockfd < 0) {
86  if (newsockfd == kTimeoutErr)
87  printf("server[%d]: idle\n", (int) getpid());
88  else
89  fprintf(stderr, "server[%d]: accept error: %s\n",
90  (int) getpid(), strerror(errno));
91  } else if ((pid = fork()) < 0) {
92  fprintf(stderr, "server[%d]: fork error: %s\n",
93  (int) getpid(), strerror(errno));
94  exit(1);
95  } else if (pid == 0) {
96  ServeOneClient(newsockfd, &cliAddr);
97  exit(0);
98  } else {
99  /* Parent doesn't need it now. */
100  (void) close(newsockfd);
101  }
102  }
103 } /* Server */
104 
105 
106 void
107 main(int argc, char **argv)
108 {
109  int port;
110 
111  if (argc < 2) {
112  fprintf(stderr, "Usage: %s <port>\n", argv[0]);
113  exit(2);
114  }
115  port = atoi(argv[1]);
116  Server(port);
117  exit(0);
118 } /* main */
static int argc
Definition: ServiceArgs.c:12
_In_ ULONG_PTR _In_ ULONG _Out_ ULONG_PTR * pid
Definition: winddi.h:3835
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
int InitSReadlineInfo(SReadlineInfo *, int, char *, size_t, int, int)
Definition: SReadline.c:24
#define kReUseAddrYes
Definition: sio.h:45
int errno
#define WNOHANG
Definition: linux.h:1333
#define SAccept
Definition: sio.h:81
static void Server(int port)
Definition: srltest.c:69
#define argv
Definition: mplay32.c:18
const char * strerror(int err)
Definition: compat_str.c:23
void main(int argc, char **argv)
Definition: srltest.c:107
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_CRTIMP void __cdecl perror(_In_opt_z_ const char *_ErrMsg)
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
smooth NULL
Definition: ftsmooth.c:416
#define kTimeoutErr
Definition: sio.h:58
int SWrite(int, const char *const, size_t, int, int)
Definition: SWrite.c:90
int SReadline(SReadlineInfo *, char *const, size_t)
Definition: SReadline.c:76
static void ServeOneClient(int sockfd, struct sockaddr_in *cliAddr)
Definition: srltest.c:23
int toupper(int c)
Definition: utclib.c:881
_CRTIMP int __cdecl getpid(void)
int SNewStreamServer(const int, const int, const int, int)
Definition: SNew.c:34
int SClose(int sfd, int tlen)
Definition: SClose.c:9
#define islower(c)
Definition: acclib.h:72
#define close
Definition: acwin.h:74
char * AddrToAddrStr(char *const dst, size_t dsize, struct sockaddr_in *const saddrp, int dns, const char *fmt)
Definition: StrAddr.c:136
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)
FILE * stderr
void exit(int exitcode)
Definition: _exit.c:33
USHORT port
Definition: uri.c:227
#define printf
Definition: config.h:203