最初のパス
どのような問題が発生しているのかわかりません。私はあなたのコード(かなり良い形でした - よくできていました;私は多くの悪い問題を含む多くのコードを見てきました)を取り、それをコンパイルして実行しました。
$ nc localhost 8888
Connection Accepted
ClientIP:127.0.0.1
Handler Assigned
Hello you have been accepted!
Greeting! I am your Connection Handler
What do you want to do
01
User Entered:01
Changing LED Pin- 0 Status- 1
01
21
User Entered:21
Changing LED Pin- 2 Status- 1
21
31
User Entered:31
Changing LED Pin- 3 Status- 1
31
we wish you a merry Christmas
User Entered:we wish you a merry Christmas
Changing LED Pin- 71 Status- 53
we wish you a merry Christmas
Client Disconnected
$
実行されたコードは次のとおりです。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
//#include <wiringPi.h>
void *connection_handler(void *);
void lightLED(int pin, int status);
int maxConnections = 1;
int totalConnections = 0;
int main(void)
{
int socket_desc, new_socket, c, *new_sock;
struct sockaddr_in server, client;
char *message;
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1)
{
printf("Could not create socket");
return 1;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
if ( bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
{
puts("bind failed");
return 1;
}
puts("bind done");
if (listen(socket_desc, 3) != 0)
{
perror("listen() failed");
return 1;
}
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while ((new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)))
{
if (new_socket > 0)
{
if (totalConnections < maxConnections)
totalConnections++;
else
{
message = "Sorry Maximum Users Reached\n";
write(new_socket, message, strlen(message));
puts("Too many Users");
close(new_socket);
continue;
}
}
puts("Connection Accepted");
char *client_ip = inet_ntoa(client.sin_addr);
//int client_port = ntohs(client.sin_port);
printf("ClientIP:%s\n", client_ip);
message = "Hello you have been accepted!\n";
write(new_socket, message, strlen(message));
pthread_t sniffer_thread;
new_sock = malloc(1 * sizeof(int)); // Oops!
if (new_sock == 0) { perror("out of memory"); return 1; }
*new_sock = new_socket;
if (pthread_create( &sniffer_thread, NULL, connection_handler, (void*) new_sock) <0)
{
perror("Could not create thread");
return 1;
}
puts("Handler Assigned");
}
if (new_socket<0)
{
perror("accept failed");
return 1;
}
return 0;
}
void *connection_handler(void *socket_desc)
{
int sock = *(int*)socket_desc;
int read_size;
char *message, client_message[2000];
message = "Greeting! I am your Connection Handler\n";
write(sock, message, strlen(message));
message = "What do you want to do\n";
write(sock, message, strlen(message));
while ((read_size = recv(sock, client_message, 2000, 0)) > 0)
{
write(sock, client_message, strlen(client_message));
printf("User Entered:%s\n", client_message);
int pin = client_message[0]-'0';
int status = client_message[1]-'0';
lightLED(pin, status);
}
if (read_size == 0)
{
puts("Client Disconnected\n");
fflush(stdout);
totalConnections--;
}
else if (read_size == -1)
{
perror("recv Failed");
}
free(socket_desc);
return 0;
}
void lightLED(int pin, int status)
{
// if (wiringPiSetup() == -1)
// {
// puts("wiringPi Error");
// exit(1);
// }
printf("Changing LED Pin- %d Status- %d\n", pin, status);
}
それでも問題が解決しない場合は、クライアント コードに問題がある可能性があります。ご覧のとおり、クライアントの代理としてnetcat
( ) を使用しました。nc
ピンが 73 でステータスが 53 であったにもかかわらず、'we wish you a merry Christmas' が有効なコマンドとして受け入れられたことに注意してください。これは、実際の LED では機能しない可能性があります。
のエラーチェックを追加しmalloc()
、より正確な量のスペースを割り当てたことに注意してください ( sizeof(int)
1 バイトだけではありません)。また、エラーが発生しなかったかのように続行するのではなく、報告されたエラー状態の後に多かれ少なかれ適切なエラーが返されるようにしました。
また、コメントで強調表示されている問題のいくつかを修正していwrite()
ません。null 終了を確認して依存しないなどです。これらは引き続き対処する必要があります。
私のテストは、GCC 4.7.1 を搭載した Mac OS X 10.7.5 で行われました。
gcc -O3 -g -std=c99 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition server.c -o server
セカンドパス
別のテスト実行 — null で終了していない入力の問題を示します。
$ nc localhost 8888
Connection Accepted
ClientIP:127.0.0.1
Hello you have been accepted!
Handler Assigned
Greeting! I am your Connection Handler
What do you want to do
this is a long string - what will you do with it?
User Entered:this is a long string - what will you do with it?
Changing LED Pin- 68 Status- 56
this is a long string - what will you do with it?
01
User Entered:01
s is a long string - what will you do with it?
Changing LED Pin- 0 Status- 1
01
s is a long string - what will you do with it?
Client Disconnected
$
telnet
の代わりに実行するとnc
、あなたが見たような誤動作が発生したと思います:
$ telnet localhost 8888
Trying 127.0.0.1...
Connection Accepted
ClientIP:127.0.0.1
Handler Assigned
Connected to localhost.
Escape character is '^]'.
Hello you have been accepted!
Greeting! I am your Connection Handler
What do you want to do
Would you like a biscuit?
User Entered:Would you like a biscuit?
Changing LED Pin- 39 Status- 63
Would you like a biscuit?
93
User Entered:93
d you like a biscuit?
Changing LED Pin- 9 Status- 3
93
d you like a biscuit?
Intriguing
User Entered:Intriguing
ke a biscuit?
Changing LED Pin- 25 Status- 62
Intriguing
ke a biscuit?
Bye
User Entered:Bye
guing
ke a biscuit?
Changing LED Pin- 18 Status- 73
Bye
guing
ke a biscuit?
User Entered:ye
guing
ke a biscuit?
Changing LED Pin- -44 Status- 73
ye
guing
ke a biscuit?
^CUser Entered:????guing
ke a biscuit?
Changing LED Pin- -49 Status- -60
User Entered:???guing
ke a biscuit?
Changing LED Pin- -49 Status- -53
?guing
ke a biscuit?
User Entered:??guing
ke a biscuit?
...continued attempts with control-C (interrupt)...
...and control-D (EOF) not producing anything useful...
^]
telnet> qConnection closed.
Client Disconnected
$
そのため、telnet
誤解を招いている可能性があります...サーバーに何も問題がなく、クライアント ( telnet
) が期待どおりに動作していないだけです。
更新されたコード
更新されたサーバー コードとの会話:
$ nc localhost 8888
Connection Accepted
ClientIP: 127.0.0.1
Handler Assigned
Hello you have been accepted!
Greetings! I am your Connection Handler
What do you want to do
13
User Entered:13
Changing LED Pin 1 status 3
13
21
User Entered:21
Changing LED Pin 2 status 1
21
elephants?
User Entered:elephants?
Changing LED Pin 53 status 60
elephants?
21
User Entered:21
Changing LED Pin 2 status 1
21
quit
User Entered:quit
Changing LED Pin 65 status 69
quit
Client Disconnected
$
更新されたサーバー コード
このバージョンは長さに注意を払い、文字列が null で終了するようにします。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
//#include <wiringPi.h>
void *connection_handler(void *);
void lightLED(int pin, int status);
static void write_sock(int sock, const char *msg);
int maxConnections = 1;
int totalConnections = 0;
int main(void)
{
int socket_desc, new_socket, c, *new_sock;
struct sockaddr_in server, client;
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1)
{
printf("Could not create socket");
return 1;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
{
puts("bind failed");
return 1;
}
puts("bind done");
if (listen(socket_desc, 3) != 0)
{
perror("listen() failed");
return 1;
}
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while ((new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)))
{
if (new_socket > 0)
{
if (totalConnections < maxConnections)
totalConnections++;
else
{
write_sock(new_socket, "Sorry Maximum Users Reached\n");
puts("Too many Users");
close(new_socket);
continue;
}
}
puts("Connection Accepted");
char *client_ip = inet_ntoa(client.sin_addr);
//int client_port = ntohs(client.sin_port);
printf("ClientIP: %s\n", client_ip);
write_sock(new_socket, "Hello you have been accepted!\n");
pthread_t sniffer_thread;
new_sock = malloc(1 * sizeof(int)); // Oops!
if (new_sock == 0) { perror("out of memory"); return 1; }
*new_sock = new_socket;
if (pthread_create(&sniffer_thread, NULL, connection_handler, (void *)new_sock) < 0)
{
perror("Could not create thread");
return 1;
}
puts("Handler Assigned");
}
if (new_socket < 0)
{
perror("accept failed");
return 1;
}
return 0;
}
// Avoid repetition - use functions!
static void write_sock(int sock, const char *msg)
{
int len = strlen(msg);
if (write(sock, msg, len) != len)
{
perror("short write on socket");
exit(1);
}
}
void *connection_handler(void *socket_desc)
{
int sock = *(int*)socket_desc;
int read_size;
char client_message[2000];
write_sock(sock, "Greetings! I am your Connection Handler\n");
write_sock(sock, "What do you want to do\n");
while ((read_size = recv(sock, client_message, 2000, 0)) > 0)
{
client_message[read_size] = '\0';
write_sock(sock, client_message);
printf("User Entered:%s\n", client_message);
int pin = client_message[0]-'0';
int status = client_message[1]-'0';
lightLED(pin, status);
}
if (read_size == 0)
{
puts("Client Disconnected\n");
fflush(stdout);
totalConnections--;
}
else if (read_size == -1)
{
perror("recv Failed");
}
free(socket_desc);
return 0;
}
void lightLED(int pin, int status)
{
// if (wiringPiSetup() == -1)
// {
// puts("wiringPi Error");
// exit(1);
// }
printf("Changing LED Pin %d status %d\n", pin, status);
}
関数を使用して、繰り返されるコードをカプセル化することに注意してくださいwrite_sock()
(これには、コードを 1 回記述するだけでよいという副次的な利点があるため、使用するたびに正しいコードにすることができます)。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
//#include <wiringPi.h>
void *connection_handler(void *);
void lightLED(int pin, int status);
static void write_sock(int sock, const char *msg);
int maxConnections = 1;
int totalConnections = 0;
int main(void)
{
int socket_desc, new_socket, c, *new_sock;
struct sockaddr_in server, client;
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1)
{
printf("Could not create socket");
return 1;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
if (bind(socket_desc, (struct sockaddr *)&server, sizeof(server)) < 0)
{
puts("bind failed");
return 1;
}
puts("bind done");
if (listen(socket_desc, 3) != 0)
{
perror("listen() failed");
return 1;
}
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while ((new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)))
{
if (new_socket > 0)
{
if (totalConnections < maxConnections)
totalConnections++;
else
{
write_sock(new_socket, "Sorry Maximum Users Reached\n");
puts("Too many Users");
close(new_socket);
continue;
}
}
puts("Connection Accepted");
char *client_ip = inet_ntoa(client.sin_addr);
//int client_port = ntohs(client.sin_port);
printf("ClientIP: %s\n", client_ip);
write_sock(new_socket, "Hello you have been accepted!\n");
pthread_t sniffer_thread;
new_sock = malloc(1 * sizeof(int)); // Oops!
if (new_sock == 0) { perror("out of memory"); return 1; }
*new_sock = new_socket;
if (pthread_create(&sniffer_thread, NULL, connection_handler, (void *)new_sock) < 0)
{
perror("Could not create thread");
return 1;
}
puts("Handler Assigned");
}
if (new_socket < 0)
{
perror("accept failed");
return 1;
}
return 0;
}
// Avoid repetition - use functions!
static void write_sock(int sock, const char *msg)
{
int len = strlen(msg);
if (write(sock, msg, len) != len)
{
perror("short write on socket");
exit(1);
}
}
void *connection_handler(void *socket_desc)
{
int sock = *(int*)socket_desc;
int read_size;
char client_message[2000];
write_sock(sock, "Greetings! I am your Connection Handler\n");
write_sock(sock, "What do you want to do\n");
while ((read_size = recv(sock, client_message, 2000, 0)) > 0)
{
client_message[read_size] = '\0';
write_sock(sock, client_message);
printf("User Entered:%s\n", client_message);
int pin = client_message[0]-'0';
int status = client_message[1]-'0';
lightLED(pin, status);
}
if (read_size == 0)
{
puts("Client Disconnected\n");
fflush(stdout);
totalConnections--;
}
else if (read_size == -1)
{
perror("recv Failed");
}
free(socket_desc);
return 0;
}
void lightLED(int pin, int status)
{
// if (wiringPiSetup() == -1)
// {
// puts("wiringPi Error");
// exit(1);
// }
printf("Changing LED Pin %d status %d\n", pin, status);
}