source: tools/SocketClient/SocketClient.cpp@ 20

Last change on this file since 20 was 15, checked in by ogrimm, 16 years ago
SocketClient checked in
File size: 3.0 KB
Line 
1/********************************************************************\
2
3 Generic example for remote control via a socket interface
4
5 Oliver Grimm, March 2009
6
7\********************************************************************/
8
9#include <iostream>
10#include <sys/socket.h>
11#include <netdb.h>
12#include <signal.h>
13
14#define MAX_COM_SIZE 10000
15
16int main(int argc, char *argv[]) {
17
18 char Buffer[MAX_COM_SIZE],ServerName[MAX_COM_SIZE]="eth-vme02";
19 int SocketDescriptor, DAQPort = 2000;
20 struct sockaddr_in SocketAddress;
21 fd_set ReadFileDescriptor;
22
23 printf("+++ Command line socket interface +++\n");
24 if (argc != 3) {
25 printf("Usage: %s <server name> <port>\n", argv[0]);
26 printf("Taking defaults: <%s> <%d>\n", ServerName, DAQPort);
27 }
28 else {
29 sprintf(ServerName,argv[1]);
30 DAQPort = atoi(argv[2]);
31 }
32
33 // Open socket descriptor
34 if ((SocketDescriptor = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
35 printf("Error: Could not open socket.");
36 exit(EXIT_FAILURE);
37 }
38
39 // Resolve hostname and try to connect to server
40 struct hostent *hostent = gethostbyname(ServerName);
41 if (hostent==0) {
42 printf("\nCould not resolve host name.\n");
43 close(SocketDescriptor);
44 exit(EXIT_FAILURE);
45 }
46
47 SocketAddress.sin_family = PF_INET;
48 SocketAddress.sin_port = htons((unsigned short) DAQPort);
49 SocketAddress.sin_addr = *(struct in_addr*) hostent->h_addr;
50
51 if (connect(SocketDescriptor, (struct sockaddr *) &SocketAddress, sizeof(SocketAddress))==-1) {
52 printf("Error: Could not connect to server %s (port %d)\n", ServerName, DAQPort);
53 close(SocketDescriptor);
54 exit(EXIT_FAILURE);
55 }
56 printf("\nConnected to %s (port %d).\n"
57 "Type 'exit' to quit.\n", ServerName, DAQPort);
58 signal(SIGPIPE,SIG_IGN); // Do not kill process if writing to closed socket
59
60 while(true) {
61 printf("> "); fflush(stdout);
62
63 // Wait for data either from terminal (stdin) or from socket
64 FD_ZERO(&ReadFileDescriptor);
65 FD_SET(SocketDescriptor, &ReadFileDescriptor);
66 FD_SET(STDIN_FILENO, &ReadFileDescriptor);
67 if (select(((int) SocketDescriptor)+1, &ReadFileDescriptor, NULL, NULL, NULL)==-1) {
68 perror("Error with select()");
69 break;
70 }
71 memset(Buffer, 0, sizeof(Buffer));
72 if (FD_ISSET(STDIN_FILENO, &ReadFileDescriptor)) { // Data from stdin
73 fgets(Buffer, MAX_COM_SIZE, stdin);
74 if (!strcmp(Buffer,"exit\n")) break;
75 if ((write(SocketDescriptor, Buffer, strlen(Buffer)))<1) // Write command to socket
76 printf("Error: Could not write command to socket.\n");
77 }
78 else if (FD_ISSET(SocketDescriptor, &ReadFileDescriptor)) { // Data from socket
79 memset(Buffer, 0, sizeof(Buffer));
80 if(read(SocketDescriptor, Buffer, MAX_COM_SIZE)==0) {
81 printf("Server not exisiting anymore, exiting...\n");
82 break;
83 }
84 printf("\r%s", Buffer);
85 }
86 }
87
88 // Close socket descriptor
89 if (!close(SocketDescriptor)) printf("Connection to %s closed.\n", ServerName);
90
91 exit(EXIT_SUCCESS);
92}
Note: See TracBrowser for help on using the repository browser.