source: fact/tools/SocketClient/SocketClient.cpp@ 16840

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