저는 두 사람의 게임을하는 간단한 서버를 C로 작성하려고합니다. 들어오는 연결을 확인하고, player1이없는 경우, player1의 파일 디스크립터를 저장하고 (나중에 보내고받을 때 사용), player2가없는 경우에도 동일한 작업을 수행합니다. 나는이 루프를 내가 수정 한 Here에서 설정했다. 내 문제는 하나로부터 받고, 다른 사람에게 보내고 싶지만 내 과제가 유효하지 않은 것 같습니다. player2에 보내려고하면 실패하거나 쓰레기를 보냅니다. 때로는 player1에 보내는 것이 서버로 다시 전송됩니다 (?). 선택을 올바르게 사용하고 파일 설명자를 올바르게 반복합니까? 모든 의견을 주시면 감사하겠습니다.이 소켓/파일 설명자 할당이 유효하지 않은 이유는 무엇입니까?
// add the listener to the master set
FD_SET(listener, &master);
// keep track of the biggest file descriptor
fdmax = listener; // so far, it's this one
// main loop
while (1) {
read_fds = master; // copy it
if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
error("select");
}
// run through the existing connections looking for data to read
for(i = 0; i <= fdmax; i++) {
//This indicates that someone is trying to do something
if (FD_ISSET(i, &read_fds)) {
if (i == listener) {
addrlen = sizeof remoteaddr;
newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen);
if (newfd == -1) {
error("accept");
} else {
FD_SET(newfd, &master);
if (newfd > fdmax) {
fdmax = newfd;
}
/* If we have the maximum number of players, we tell if that it's busy */
if (players >= 2) {
toobusy(fdmax); close(fdmax); FD_CLR(fdmax, &master);
} else {
//Problem here?
if (player1_fd == -1) {
player1_fd = newfd;
}
if ((player1_fd != -1) && (player2_fd == -1)) {
player2_fd = newfd;
}
players++;
if (players == 2) {
sendhandles(); //says two players exist
}
}
}
} else {
//Possible problems here
if (i == player1_fd || i == player2_fd) {
receive(i); //Processes the messages
}
}
}
}
}
예, 새로운 것이 었습니다. 감사! – thomascirca