私はTCP経由で整数を送信しています。これを正しく機能させるために数日間取り組んできましたが、非常に近いです。私がやっていることは、USB ジョイスティックから入力を受け取り、軸の値をクライアントに送信することです。私はそれが機能していますが、私の問題は常に 251 で停止することです。つまり、軸の値がクライアントの画面に出力されるたびに 1 ずつ増加する変数を設定するということです。私は遊んでみましMAXRCVLEN
たが、それでも 251 で停止し続けます。誰かが私のコードを見て、ここで何か間違っているかどうかを確認できますか?
これはserver.c
ファイルです:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
//joysick libraries
#include <sys/ioctl.h>
#include <stdio.h>
#include <fcntl.h>
#include <linux/joystick.h>
//Joysick device
#define JOY_DEV "/dev/input/js0"
//TCP Port number
#define PORTNUM 2343
int main(int argc, char *argv[])
{
//TCP connect
struct sockaddr_in dest; /* socket info about the machine connecting to us */
struct sockaddr_in serv; /* socket info about our server */
int mysocket; /* socket used to listen for incoming connections */
socklen_t socksize = sizeof(struct sockaddr_in);
memset(&serv, 0, sizeof(serv)); /* zero the struct before filling the fields */
serv.sin_family = AF_INET; /* set the type of connection to TCP/IP */
serv.sin_addr.s_addr = htonl(INADDR_ANY); /* set our address to any interface */
serv.sin_port = htons(PORTNUM); /* set the server port number */
mysocket = socket(AF_INET, SOCK_STREAM, 0);
/* bind serv information to mysocket */
bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
/* start listening, allowing a queue of up to 1 pending connection */
listen(mysocket, 1);
int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
//TCP variable
char msg1[10];
//Joystick variables
int xAxis;
int yAxis;
int xSpeed;
int ySpeed;
//Joystick initialize
int joy_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x;
char *button=NULL, name_of_joystick[80];
struct js_event js;
if( ( joy_fd = open( JOY_DEV , O_RDONLY)) == -1 )
{
printf( "Couldn't open joystick\n" );
return -1;
}
//Get number of axes, buttons and name of joystick. Print results to screen
ioctl( joy_fd, JSIOCGAXES, &num_of_axis );
ioctl( joy_fd, JSIOCGBUTTONS, &num_of_buttons );
ioctl( joy_fd, JSIOCGNAME(80), &name_of_joystick );
axis = (int *) calloc( num_of_axis, sizeof( int ) );
button = (char *) calloc( num_of_buttons, sizeof( char ) );
printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n"
, name_of_joystick
, num_of_axis
, num_of_buttons );
//Use non blocking mode for joystick
fcntl( joy_fd, F_SETFL, O_NONBLOCK ); /* use non-blocking mode */
//infinite loop for reading joystick
while(1) {
//loop for tcp connection
while(consocket){
/* read the joystick state */
read(joy_fd, &js, sizeof(struct js_event));
/* see what to do with the event */
switch (js.type & ~JS_EVENT_INIT)
{
case JS_EVENT_AXIS:
axis [ js.number ] = js.value;
break;
case JS_EVENT_BUTTON:
button [ js.number ] = js.value;
break;
}
//Give msg1 variable the value of axis[0]
sprintf(msg1, "%d", axis[0]);
//TCP send
char msg[] = "Hello";
printf("Value is: %d\n", axis[0]);
//printf("Incoming connection from %s - sending welcome\n", inet_ntoa(dest.sin_addr));
send(consocket, msg1, strlen(msg1), 0);
consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
printf("Value X is: %s\n", msg1);
printf(" \r");
fflush(stdout);
}
close(consocket);
close(mysocket);
return EXIT_SUCCESS;
}
//Joystick close
close( joy_fd ); /* too bad we never get here */
return 0;
}
これはclient.c
ファイルです
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define MAXRCVLEN 10000
#define PORTNUM 2343
int main(int argc, char *argv[])
{
int i = 0;
while(1){
char buffer[MAXRCVLEN + 1]; /* +1 so we can add null terminator */
int len, mysocket;
struct sockaddr_in dest;
mysocket = socket(AF_INET, SOCK_STREAM, 0);
memset(&dest, 0, sizeof(dest)); /* zero the struct */
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr("192.168.254.16"); /* set destination IP number */
dest.sin_port = htons(PORTNUM); /* set destination port number */
connect(mysocket, (struct sockaddr *)&dest, sizeof(struct sockaddr));
len = recv(mysocket, buffer, MAXRCVLEN, 0);
/* We have to null terminate the received data ourselves */
buffer[len] = '\0';
printf("Received %s (%d bytes).\n", buffer, len);
printf("Number is: %d", i);
i++;
//close(mysocket);
}
return EXIT_SUCCESS;
}