Overview of Computer Networks

Start from the beginning
                                        

16 #include <netdb.h>

17

18

19 /* this is needed for the disk read */

20 #include <fcntl.h>

21

22

23 #define WPSPORT 2000 /* server port number */

24 #define BUFSIZE 1000

25

26

27 int ClntDescriptor, /* socket descriptor to client */

28 SrvrDescriptor; /* socket descriptor for server */

29

30

31 char InBuf[BUFSIZE], /* messages from client */

32 OutBuf[BUFSIZE]; /* messages to client */

33

34

35 Write()

36

37 { int FD,NB;

38

39 FD = open("tmp.client",O_RDONLY);

40 NB = read(FD,OutBuf,BUFSIZE);

41 write(ClntDescriptor,OutBuf,NB);

42 unlink("tmp.client");

43 }

44

45

46 Respond()

47

48 { memset(OutBuf,0,sizeof(OutBuf)); /* clear buffer */

49 if (!strcmp(InBuf,"w"))

50 system("w > tmp.client");

51 else if (!strcmp(InBuf,"ps"))

52 system("ps -ax > tmp.client");

53 else

54 system("echo 'invalid command' > tmp.client");

15

55 Write();

56 }

57

58

59 main(argc,argv)

60 int argc; char **argv;

61

63 { struct sockaddr_in BindInfo;

66

67 /* create an Internet TCP socket */

68 SrvrDescriptor = socket(AF_INET,SOCK_STREAM,0);

73

74 /* bind it to port 2000 (> 1023, to avoid the

75 "well-known ports"), allowing connections from

76 any NIC */

77 BindInfo.sin_family = AF_INET;

78 BindInfo.sin_port = WPSPORT;

79 BindInfo.sin_addr.s_addr = INADDR_ANY;

80 bind(SrvrDescriptor,&BindInfo,sizeof(BindInfo));

86

87 /* OK, set queue length for client calls */

88 listen(SrvrDescriptor,5);

91

You've reached the end of published parts.

⏰ Last updated: May 09, 2009 ⏰

Add this story to your Library to get notified about new parts!

Overview of Computer NetworksWhere stories live. Discover now