fgms 0.11.8
The
FlightGear MultiPlayer Server
project
client.c
Go to the documentation of this file.
1 /*
2  * client.c - a test client
3  *
4  * Author: Gabor Toth <tgbp@freemail.hu>
5  * License: GPL
6  *
7  * $Log: client.c,v $
8  * Revision 1.2 2006/05/10 21:22:34 koversrac
9  * Comment with author and license has been added.
10  *
11  */
12 
13 #include "fgt_common.h"
14 #include "wrappers.h"
15 #include "fgt_error.h"
16 
17 static char *server_address = (char *)SERVER_ADDRESS;
18 static uint32_t server_port = SERVER_PORT;
19 
20 #ifdef _MSC_VER
21 #if !defined(NTDDI_VERSION) || !defined(NTDDI_VISTA) || (NTDDI_VERSION < NTDDI_VISTA) // if less than VISTA, provide alternative
22 #ifndef EAFNOSUPPORT
23 #define EAFNOSUPPORT 97 /* not present in errno.h provided with VC */
24 #endif
25 int inet_aton(const char *cp, struct in_addr *addr)
26 {
27  addr->s_addr = inet_addr(cp);
28  return (addr->s_addr == INADDR_NONE) ? -1 : 0;
29 }
30 int inet_pton(int af, const char *src, void *dst)
31 {
32  if (af != AF_INET)
33  {
34  errno = EAFNOSUPPORT;
35  return -1;
36  }
37  return inet_aton (src, (struct in_addr *)dst);
38 }
39 #endif // #if (NTDDI_VERSION < NTDDI_VISTA)
40 #endif // _MSC_VER
41 
42 static void net_exit ( void )
43 {
44 #ifdef _MSC_VER
45  /* Clean up windows networking */
46  if ( WSACleanup() == SOCKET_ERROR ) {
47  if ( WSAGetLastError() == WSAEINPROGRESS ) {
48  WSACancelBlockingCall();
49  WSACleanup();
50  }
51  }
52 #endif
53 }
54 
55 
56 int net_init ()
57 {
58 #ifdef _MSC_VER
59  /* Start up the windows networking */
60  WORD version_wanted = MAKEWORD(1,1);
61  WSADATA wsaData;
62 
63  if ( WSAStartup(version_wanted, &wsaData) != 0 ) {
64  printf("Couldn't initialize Winsock 1.1\n");
65  return(1);
66  }
67 #endif
68 
69  atexit( net_exit ) ;
70  return(0);
71 }
72 
73 
74 int main (int argc, char **argv)
75 {
76  char msg[MAXLINE];
77  int len;
78  char *s;
79  int sockfd;
80  struct sockaddr_in serveraddr;
81 
82  if (net_init())
83  return 1;
84 
85  sockfd=Socket(AF_INET, SOCK_STREAM, 0);
86 
87  bzero(&serveraddr,sizeof(serveraddr));
88  printf("fgt_client connecting to %s, on port %u\n", server_address, server_port);
89 
90  serveraddr.sin_family = AF_INET;
91  serveraddr.sin_port = htons(server_port);
92 
93  inet_pton(AF_INET, server_address, &serveraddr.sin_addr);
94 
95  Connect(sockfd, (SA *) &serveraddr, sizeof(serveraddr));
96 
97  bzero(msg,MAXLINE);
98 
99  while ( strncmp(msg,"QUIT\n",MAXLINE) != 0 )
100  {
101  s = fgets(msg,MAXLINE,stdin);
102  len = strlen(msg);
103  if (len>0)
104  {
105  msg[len]='\0';
106  if ( SWRITE(sockfd,msg,len) != len ) {
107  printf("ERROR: Write socket FAILED!\n");
108  break; /* out of here */
109  }
110  }
111  }
112 
113  Close(sockfd);
114  exit (0);
115 }
116 
#define INADDR_NONE
Definition: netSocket.cxx:58
int net_init()
Definition: client.c:56
int Close(int sockfd)
Definition: wrappers.c:35
static void net_exit(void)
Definition: client.c:42
int main(int argc, char **argv)
Definition: client.c:74
#define MAXLINE
Definition: fg_common.hxx:179
#define SWRITE
Definition: fgt_common.h:245
static char * server_address
Definition: client.c:17
#define bzero(ptr, n)
Definition: fg_common.hxx:122
int Connect(int sockfd, const struct sockaddr *serveraddr, socklen_t addrlen)
Definition: wrappers.c:44
#define SOCKET_ERROR
Definition: netSocket.h:80
#define SA
Definition: fg_common.hxx:190
static uint32_t server_port
Definition: client.c:18
#define SERVER_ADDRESS
Definition: fgt_config.h:38
#define SERVER_PORT
Definition: fgt_config.h:42
int Socket(int family, int type, int protocol)
Definition: wrappers.c:73