-
Notifications
You must be signed in to change notification settings - Fork 0
/
xsrv.c
73 lines (61 loc) · 1.6 KB
/
xsrv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
int main( argc, argv )
int argc;
char **argv;
{
int tSize, srvfd, tStatus, cltfd ;
struct sockaddr_in tAddr;
char tBuffer[256];
srvfd = socket(AF_INET, SOCK_STREAM, 0);
if (srvfd < 0) {
printf("Failed to allocate server socket\n");
exit(0);
}
bzero( &tAddr, sizeof( struct sockaddr_in ));
tAddr.sin_family = AF_INET;
tAddr.sin_addr.s_addr = htonl( INADDR_ANY );
tAddr.sin_port = htons( 5555 );
tStatus = bind( srvfd, (struct sockaddr *) &tAddr, sizeof(tAddr));
if (tStatus < 0) {
printf("Failed to bind server socket\n");
exit(0);
}
tStatus = listen( srvfd, 5 );
if (tStatus < 0) {
printf("Failed to listen server socket\n");
exit(0);
}
while( 1 == 1 ) {
cltfd = accept( srvfd, NULL, NULL );
if (cltfd < 0) {
printf("accept failed \n");
exit(0);
}
printf("Inbound connection\n");
while( cltfd != 0 ) {
tSize = read( cltfd, tBuffer, sizeof( tBuffer));
if (tSize <= 0) {
printf("*** Read failure *** status: %d socket: %d\n", tSize, cltfd);
cltfd = 0;
break;
}
tBuffer[ tSize ] = '\0';
printf("[read] %s\n", tBuffer );
tSize = write( cltfd, tBuffer, strlen( tBuffer));
if (tSize < 0) {
printf("*** Send failure ***\n");
cltfd = 0;
break;
}
}
}
}