server.c 와 client.c로 만들고 있습니다
다만 client.c로 입력을 할때마다 송신이 안됩니다.
이게 진짜 이상한게 어떨때는 2번째 턴에 안되고 어떨때는 3번쨰 턴에 안되는데
chatgpt한테 물어봐도 어떻게 안되더라구요
혹시 어떻게 해야 하는지 아시나요...?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define PORT 8888
#define SIZE 3
#define BUFFER_SIZE 1024
void print_board(char tic[3][3], int client_socket) {
char message[BUFFER_SIZE];
sprintf(message, "| %c | %c | %c |\n---------------\n| %c | %c | %c |\n---------------\n| %c | %c | %c |\n",
tic[0][0], tic[0][1], tic[0][2], tic[1][0], tic[1][1], tic[1][2], tic[2][0], tic[2][1], tic[2][2]);
send(client_socket, message, strlen(message), 0);
}
int check_win(char tic[3][3], char mark) {
for (int i = 0; i < SIZE; i++) {
if ((tic[i][0] == mark && tic[i][1] == mark && tic[i][2] == mark) ||
(tic[0][i] == mark && tic[1][i] == mark && tic[2][i] == mark)) {
return 1;
}
}
if ((tic[0][0] == mark && tic[1][1] == mark && tic[2][2] == mark) ||
(tic[0][2] == mark && tic[1][1] == mark && tic[2][0] == mark)) {
return 1;
}
return 0;
}
int main() {
int server_fd, client_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
int a, b;
char tic[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}};
char buffer[BUFFER_SIZE];
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("소켓 생성 실패");
exit(EXIT_FAILURE);
}
printf("ip와 port 바인딩\n");
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("바인딩 실패");
exit(EXIT_FAILURE);
}
printf("리스닝 시작\n");
if (listen(server_fd, 3) < 0) {
perror("리스닝 실패");
exit(EXIT_FAILURE);
}
printf("서버가 시작되었습니다. 포트 %d에서 대기 중...\n", PORT);
while (1) {
if ((client_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("연결 수락 실패");
exit(EXIT_FAILURE);
}
printf("클라이언트와 연결되었습니다.\n");
while (1) {
print_board(tic, client_socket);
send(client_socket, "어디에 찍을래? ex) 1 3\n", strlen("어디에 찍을래? ex) 1 3\n"), 0);
int bytes_received = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
if (bytes_received <= 0) {
perror("클라이언트 입력 수신 실패");
break;
}
buffer[bytes_received] = '\0';
printf("수신된 데이터: %s\n", buffer); // 수신된 데이터 출력
sscanf(buffer, "%d %d", &a, &b);
if (a < 1 || a > SIZE || b < 1 || b > SIZE) {
send(client_socket, "1 ~ 3인 숫자를 입력하세요\n", strlen("1 ~ 3인 숫자를 입력하세요\n"), 0);
continue;
}
a--; b--;
if (tic[a][b] != ' ') {
send(client_socket, "이미 선택된 자리입니다.\n", strlen("이미 선택된 자리입니다.\n"), 0);
continue;
}
tic[a][b] = 'X';
print_board(tic, client_socket);
if (check_win(tic, 'X')) {
send(client_socket, "X가 이김\n", strlen("X가 이김\n"), 0);
break;
}
if (tic[0][0] != ' ' && tic[0][1] != ' ' && tic[0][2] != ' ' &&
tic[1][0] != ' ' && tic[1][1] != ' ' && tic[1][2] != ' ' &&
tic[2][0] != ' ' && tic[2][1] != ' ' && tic[2][2] != ' ') {
send(client_socket, "무승부임\n", strlen("무승부임\n"), 0);
break;
}
// O의 차례
print_board(tic, client_socket);
send(client_socket, "어디에 찍을래? ex) 1 3\n", strlen("어디에 찍을래? ex) 1 3\n"), 0);
bytes_received = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
if (bytes_received <= 0) {
perror("클라이언트 입력 수신 실패");
break;
}
buffer[bytes_received] = '\0';
printf("수신된 데이터: %s\n", buffer); // 수신된 데이터 출력
sscanf(buffer, "%d %d", &a, &b);
if (a < 1 || a > SIZE || b < 1 || b > SIZE) {
send(client_socket, "1 ~ 3인 숫자를 입력하세요\n", strlen("1 ~ 3인 숫자를 입력하세요\n"), 0);
continue;
}
a--; b--;
if (tic[a][b] != ' ') {
send(client_socket, "이미 선택된 자리입니다.\n", strlen("이미 선택된 자리입니다.\n"), 0);
continue;
}
tic[a][b] = 'O';
print_board(tic, client_socket);
if (check_win(tic, 'O')) {
send(client_socket, "O가 이김\n", strlen("O가 이김\n"), 0);
break;
}
if (tic[0][0] != ' ' && tic[0][1] != ' ' && tic[0][2] != ' ' &&
tic[1][0] != ' ' && tic[1][1] != ' ' && tic[1][2] != ' ' &&
tic[2][0] != ' ' && tic[2][1] != ' ' && tic[2][2] != ' ') {
send(client_socket, "무승부임\n", strlen("무승부임\n"), 0);
break;
}
}
close(client_socket);
}
close(server_fd);
return 0;
}
실행환경은 ubuntu입니다...
클라이언트에서 read 연속으로 두번 해서 문제(52줄 -> 37줄). 다른 프로토콜 보면 보통 데이터 길이를 먼저 보내는데, 이런 이유.