私は、一度に1つのクライアントが接続し、コマンドを実行してから閉じることができるサーバを持っています。サーバは "現在接続されている" IPアドレスの配列リストを保持し、誰がコマンドを受け入れるかを知っています。 問題は、同じクライアントであるかのように扱われる同様のIPアドレスで動作する2つのクライアントがあることです。サーバは、異なるIPを持つ2つのクライアントが同じであると考えます。1クライアント(Cソケットプログラミング)
例: クライアントAのIPアドレスは255.255.255.153です。私はそれを接続するために言う、すべて正常に動作します。 クライアントBのIPアドレスは255.255.255.156です。私はそれを接続するように指示し、サーバーはそれがすでに接続されていると言います。
私のstrcmpは間違っていますか?それは別のものですか?助けてください。
// Variable Declarations
int SIZE = 10; // Max number of agents
char *agents[SIZE], // List of current connections
*times[SIZE]; // List of connection times
char buffer[MAXBUF];
int bytes_read = 0;
int total_bytes_read = 0;
int found = 0, // Bool flag
i;
struct tm ts;
struct timeval connected[SIZE],
current,
difference;
time_t TIME;
// Initialize array to NULL
for(i = 0; i < SIZE; i++) {
agents[i] = NULL;
}
// Infinite Loop
while (true) {
struct sockaddr_in client;
int clientSocket;
socklen_t clientLength = sizeof(client);
memset(&client, 0, clientLength);
clientSocket = accept(sd, (struct sockaddr *)&client, &clientLength);
memset(buffer,0,MAXBUF);
bytes_read = read(clientSocket, buffer, MAXBUF);
if (bytes_read < 0)
break;
fprintf(stdout,"\nRead %d bytes: [%s]\r\n", bytes_read,buffer);
char *connectedIP = inet_ntoa(client.sin_addr);
// Option 1:
if ((strcmp(buffer, "#JOIN")) == 0) { // Join list of connected agents
found = 0;
// Get current time for log
TIME = time(NULL);
ts = *localtime(&TIME);
// Print message to log
char buf[80];
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
fprintf(log, "%s: Received a \"#JOIN\" action from agent \"%s\"", buf, connectedIP);
fprintf(log, "\n\n");
fflush(log);
printf("\n%s Joining", connectedIP);
fflush(stdout);
// Handle #JOIN request appropriately
for (i = 0; i < SIZE; i++) {
printf("\nAgent[%d] == %s", i, agents[i]);
if (agents[i] == NULL) {
}
else if (strcmp(agents[i], connectedIP) == 0){ // Agent found in list
found = 1;
printf("\n%s is equal to %s", agents[i], connectedIP);
fflush(stdout);
// Write to agent
char response[] = "#ALREADY MEMBER";
write (clientSocket, response, strlen(response));
// Get time for log
TIME = time(NULL);
ts = *localtime(&TIME);
// Write to log
char buf[80];
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
fprintf(log, "%s: Responded to agent \"%s\" with \"#ALREADY MEMBER\"", buf, connectedIP);
fprintf(log, "\n\n");
fflush(log);
i = SIZE;
}
}
if (found == 0) { // Save IP to list/queue
for(i = 0; i < SIZE; i++) {
if (agents[i] == NULL) {
// Save IP to array
agents[i] = connectedIP;
printf("\nagents[%d] = %s\n", i, connectedIP);
fflush(stdout);
// Save time to arrays
gettimeofday(&connected[i], NULL);
TIME = time(NULL);
times[i] = TIME;
ts = *localtime(&TIME);
// Write to log
char buf[80];
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
fprintf(log, "%s: Responded to agent \"%s\" with \"#OK\"", buf, connectedIP);
fprintf(log, "\n\n");
fflush(log);
// Write to agent
char response[] = "#OK";
write (clientSocket, response, strlen(response));
i = SIZE;
}
}
}
memset(&client, 0, clientLength);
}
あなたは 'agents'配列がどのように定義されているか、AFAICSを表示しません。それを適切に定義してIPv4アドレスを文字列として格納すると、 'strcmp()'はOKであるはずです。しかし、各アドレスを格納するために16バイトを許可する必要があります。 3単位の'192 ' '192 \ 0'のいずれかです。したがって、表示されていないコードには多くのヒンジがあります。 MCVE([MCVE])の作成方法をお読みください。あなたが示すものはMCVEではありません。 –
「エージェント」宣言を追加しました。コメントは残念です。修正されました。 – Carr
あなたは 'agents [i] = connectedIP;'を保存します。宣言されていない変数ですが、すべてのエージェントに同じスペースを使用している可能性があります。彼らはすべて同じだと思う。私は 'strdup(connectedIP)'のようなものを期待しています。しかしそれはまだMCVEではないので、人々があなたに手伝ってもらうのは便利ではありません。 –