3 * Simple Winsock TCP server test.
4 * Later will be used as base for ReactOS telnetd
7 * 6-15-02 - Added Winsock support to UNIX tcp test
8 * 6-16-02 - Removed Unix support
9 * 6-17-02 - Added extra comments to code
21 #define SERVER_PORT 23
24 /* function readline */
27 int main (int argc, char *argv[]) {
29 WORD wVersionRequested;
32 int sd, newSd, cliLen;
34 struct sockaddr_in cliAddr, servAddr;
37 wVersionRequested = MAKEWORD(2, 2);
39 Status = WSAStartup(wVersionRequested, &WsaData);
41 printf("Could not initialize winsock dll.\n");
46 sd = socket(AF_INET, SOCK_STREAM, 0);
48 perror("cannot open socket ");
53 /* bind server port */
54 servAddr.sin_family = AF_INET;
55 servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
56 servAddr.sin_port = htons(SERVER_PORT);
58 if(bind(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))<0) {
59 perror("cannot bind port ");
69 To start test, Please telnet to localhost (127.0.0.1) port 23 \n
70 When connected input raw data followed by End of Line\n
71 Test is now running on TCP port %u\n",argv[0],SERVER_PORT);
73 cliLen = sizeof(cliAddr);
74 newSd = accept(sd, (struct sockaddr *) &cliAddr, &cliLen);
76 perror("cannot accept connection ");
82 memset(line,0x0,MAX_MSG);
84 /* receive segments */
85 while(read_line(newSd,line)!=ERROR) {
87 printf("%s: received from %s:TCP%d : %s\n", argv[0],
88 inet_ntoa(cliAddr.sin_addr),
89 ntohs(cliAddr.sin_port), line);
91 memset(line,0x0,MAX_MSG);
93 } /* while(read_line) */
100 /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING */
101 /* this function is experimental.. I don't know yet if it works */
102 /* correctly or not. Use Steven's readline() function to have */
103 /* something robust. */
104 /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING */
106 /* rcv_line is my function readline(). Data is read from the socket when */
107 /* needed, but not byte after bytes. All the received data is read. */
108 /* This means only one call to recv(), instead of one call for */
109 /* each received byte. */
110 /* You can set END_CHAR to whatever means endofline for you. (0x0A is \n)*/
111 /* read_lin returns the number of bytes returned in line_to_return */
112 int read_line(int newSd, char *line_to_return) {
114 static int rcv_ptr=0;
115 static char rcv_msg[MAX_MSG];
123 /* read data from socket */
124 memset(rcv_msg,0x0,MAX_MSG); /* init buffer */
125 n = recv(newSd, rcv_msg, MAX_MSG, 0); /* wait for data */
127 perror(" cannot receive data ");
130 printf(" connection closed by client\n");
137 /* if new data read on socket */
139 /* if another line is still in buffer */
141 /* copy line into 'line_to_return' */
142 while(*(rcv_msg+rcv_ptr)!=END_LINE && rcv_ptr<n) {
143 memcpy(line_to_return+offset,rcv_msg+rcv_ptr,1);
148 /* end of line + end of buffer => return line */
150 /* set last byte to END_LINE */
151 *(line_to_return+offset)=END_LINE;
156 /* end of line but still some data in buffer => return line */
158 /* set last byte to END_LINE */
159 *(line_to_return+offset)=END_LINE;
164 /* end of buffer but line is not ended => */
165 /* wait for more data to arrive on socket */