Base Construct works
[hochregal.git] / src / testclient.c
blob8ff24e427e4bbd74033eb74b793df1bba77cce34
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <stdio.h>
4 #include <netinet/in.h>
5 #include <arpa/inet.h>
6 #include <unistd.h>
7 #include <pthread.h>
8 #include <string.h>
10 #pragma pack (1)
11 struct commands
13 unsigned int xon :1;
14 unsigned int xdir :1;
15 unsigned int yon :1;
16 unsigned int ydir :1;
17 unsigned int zon :1;
18 unsigned int zdir :1;
19 unsigned int grep :1;
21 #pragma pack()
24 #pragma pack (2)
25 struct sensors
27 unsigned int x :4;
28 unsigned int y :4;
29 unsigned int z :2;
31 #pragma pack()
33 struct commands my_commands;
34 struct sensors my_sensors;
36 /***************CLIENT****************/
38 void *Receive(void *my_socket)
40 int rc, socket=(int) my_socket;
41 char rec[sizeof(my_sensors)];
43 printf("\n<Receive> started");
45 while(1)
47 rc=recv(socket, (struct sensors *)&my_sensors, sizeof(my_sensors),0);
48 printf("\n%d Bytes empfangen", rc);
49 printf("sensors x: %d", (int)my_sensors.x);
51 pthread_exit(NULL);
54 void *Parser(void *my_socket)
56 int socket=(int) my_socket;
57 char input[18];
58 int len;
59 int test=23;
61 printf("\n<Parser> started");
62 printf("\n~>");
63 scanf("%18s", input);
65 fflush(stdin);
67 if(strstr(input, "startx"))
69 my_commands.xon=1;
70 printf("\nxon %d", my_commands.xon);
72 if(strstr(input, "forward"))
74 printf("forward");
75 my_commands.xdir=1;
77 else
79 printf("backward");
80 my_commands.xdir=0;
82 printf("\nxon %d", my_commands.xon);
85 else
87 printf("no");
88 my_commands.xon=0;
91 printf("\nxon %d", my_commands.xon);
92 printf(" xdir %d", my_commands.xdir);
93 printf(" yon %d", my_commands.yon);
94 printf(" ydir %d", my_commands.ydir);
95 printf(" zon %d", my_commands.zon);
96 printf(" zdir %d", my_commands.zdir);
97 printf(" grep %d", my_commands.grep);
99 len=send(socket, (struct commands *)&my_commands, sizeof(my_commands),0);
100 printf("\nsent %d bytes",len);
101 fflush(stdout);
102 pthread_exit(NULL);
106 int main(int argc, char *arcv[])
108 int socket_nmbr;
109 int length, rc;
110 struct sockaddr_in addresinfo;
111 unsigned short int portnmbr = 5000;
112 char ip_addres[] = "127.0.0.1";
114 pthread_t parser, receive;
117 my_commands.xon = 0;
118 my_commands.xdir = 0;
119 my_commands.yon = 0;
120 my_commands.ydir = 0;
121 my_commands.zon = 0;
122 my_commands.zdir = 0;
123 my_commands.grep = 0;
125 printf("\n Client gestartet");
127 socket_nmbr = socket(AF_INET, SOCK_STREAM, 0);
128 addresinfo.sin_family = AF_INET;
129 addresinfo.sin_addr.s_addr = inet_addr(ip_addres);
130 addresinfo.sin_port = htons(portnmbr);
131 length = sizeof(addresinfo);
133 if (!connect(socket_nmbr, (struct sockaddr *)&addresinfo, length))
135 printf("\n Client: Verbindungsaufbau erfolgreich an");
136 printf(" IP %s - Port %d", ip_addres, portnmbr);
139 rc = pthread_create(&parser, NULL, Parser, (void *) socket_nmbr);
140 if (rc){
141 printf("ERROR; return code from pthread_create() send is %d\n", rc);
145 rc = pthread_create(&receive, NULL, Receive, (void *) socket_nmbr);
146 if (rc){
147 printf("ERROR; return code from pthread_create() send is %d\n", rc);
150 while(1)
153 close(socket_nmbr);
154 printf("\n Client beendet");
155 pthread_exit(NULL);
156 return 0;