わかりました、問題があります。Java (クライアント - サーバー) で書かれた小さなプロジェクト (ゲーム) があります。そして、すべてがうまくいきます。しかし、サーバーを ANSI C に書き換えたいのです。サーバーを起動すると、クライアントからの接続を待ちます。クライアントを起動すると、接続が確立されます。そして、この時点まで、すべてが正常に機能しています。しかし、サーバーからクライアントに何かを送信しようとすると、サーバーは送信しますが、クライアントはそれを受信しません。理由はありますか?
サーバーソース:
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <winsock2.h>
DWORD WINAPI receive_cmds(LPVOID *lpParam) {
SOCKET *players = (SOCKET *) lpParam;
SOCKET player1 = (SOCKET)players[0];
SOCKET player2 = (SOCKET)players[1];
char recvBuff[100];
char sndBuff[100];
int nReadBytes;
int turn = 1;
int res;
printf("Utworzono nowy pokoj.\r\n");
strcpy(sndBuff,"PLAYER1");
printf("%s\n",sndBuff);
if((res = send(player1,sndBuff,sizeof(sndBuff),0)) == 0) {
printf("Gracz 1 rozlaczony.\n");
closesocket(player1);
closesocket(player2);
printf("Zamykam pokoj.\n");
ExitThread(0);
}
strcpy(sndBuff,"TURN1");
printf("%s\n",sndBuff);
if((res = send(player1,sndBuff,sizeof(sndBuff),0)) == 0) {
printf("Gracz 1 rozlaczony.\n");
closesocket(player1);
closesocket(player2);
printf("Zamykam pokoj.\n");
ExitThread(0);
}
strcpy(sndBuff,"PLAYER2");
printf("%s\n",sndBuff);
if((res = send(player2,sndBuff,sizeof(sndBuff),0)) == 0) {
printf("Gracz 2 rozlaczony.\n");
closesocket(player1);
closesocket(player2);
printf("Zamykam pokoj.\n");
ExitThread(0);
}
strcpy(sndBuff,"TURN0");
printf("%s\n",sndBuff);
if((res = send(player2,sndBuff,sizeof(sndBuff),0)) == 0) {
printf("Gracz 2 rozlaczony.\n");
closesocket(player1);
closesocket(player2);
printf("Zamykam pokoj.\n");
ExitThread(0);
}
while(TRUE) {
if(turn == 1) {
nReadBytes = recv(player1, recvBuff, sizeof(recvBuff), 0);
if(nReadBytes == 0) {
printf("Gracz 1 rozlaczony.\n");
closesocket(player1);
closesocket(player2);
printf("Zamykam pokoj.\n");
ExitThread(0);
} else {
printf("Odczytalem dane od gracza 1.\n");
printf("%s\n",recvBuff);
}
}
}
}
int main(void) {
DWORD thread;
WSADATA wsaData;
SOCKET sock;
SOCKET players[2];
struct sockaddr_in from;
struct sockaddr_in server;
int ret;
int fromlen;
printf("Serwer oczekuje na polaczenia.\r\n");
if((ret = WSAStartup(0x101,&wsaData)) != 0) {
return 0;
}
server.sin_family=AF_INET;
server.sin_addr.s_addr=INADDR_ANY;
server.sin_port=htons(5000);
if((sock=socket(AF_INET,SOCK_STREAM,0)) == INVALID_SOCKET) {
return 0;
}
if(bind(sock,(struct sockaddr*)&server,sizeof(server)) != 0) {
return 0;
}
if(listen(sock,5) != 0) {
return 0;
}
fromlen = sizeof(from);
while(TRUE) {
players[0] = accept(sock,(struct sockaddr*)&from,&fromlen);
printf("Gracz 1 polaczony\r\n");
players[1] = accept(sock,(struct sockaddr*)&from,&fromlen);
printf("Gracz 2 polaczony\r\n");
CreateThread(NULL, 0,receive_cmds,(LPVOID)players, 0, &thread);
}
closesocket(sock);
WSACleanup();
return 0;
}
通信用のクライアント クラス:
package game;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
public class Connection extends Thread{
private static final int PORT = 5000;
private static InetAddress IP;
private static Socket socket = null;
private BufferedReader in;
private PrintWriter out;
private Game game;
public Connection(Game game){
this.game = game;
try {
IP = InetAddress.getByName(game.getAddr());
socket = new Socket(IP,PORT);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
} catch (Exception e) {
e.printStackTrace();
}
}
public void initGame() {
String message;
System.out.println("Polaczono.");
message = rcv();
System.out.println(message);
if((message.contains("PLAYER1")) == true) {
Game.player = 1;
} else if((message.contains("PLAYER2")) == true) {
Game.player = 2;
}
message = rcv();
System.out.println(message);
if((message.contains("TURN1")) == true) {
Game.turn = true;
} else if((message.contains("TURN0")) == true) {
Game.turn = false;
}
}
public void listen() {
if(Game.turn == false) {
String message = rcv();
System.out.println("Msg rcv: " + message);
if(message.contains("CREATE")) {
System.out.println("CREATE");
Game.unitList.add(new Unit(
Integer.parseInt(message.substring(6,8)),
Integer.parseInt(message.substring(8,10)),
Integer.parseInt(message.substring(10,12)),
Integer.parseInt(message.substring(12,14))));
for(Unit u : Game.unitList) {
u.writeUnit();
}
}
if(message.contains("DELETE")) {
System.out.println("DELETE");
for(Unit u : Game.unitList) {
u.writeUnit();
}
for(Unit u : Game.unitList) {
if(u.getX() == Integer.parseInt(message.substring(6,8))
&& u.getY() == Integer.parseInt(message.substring(8,10))) {
Game.unitList.remove(u);
}
}
System.out.println("Po usunieciu:");
for(Unit u : Game.unitList) {
u.writeUnit();
}
}
if(message.contains("MOVE")) {
System.out.println("MOVE");
for(Unit u : Game.unitList) {
u.writeUnit();
}
for(Unit u : Game.unitList) {
if(u.getX() == Integer.parseInt(message.substring(4,6))
&& u.getY() == Integer.parseInt(message.substring(6,8))) {
System.out.println("Znalazlem jednostke do przesuniecia");
u.setX(Integer.parseInt(message.substring(8,10)));
u.setY(Integer.parseInt(message.substring(10,12)));
}
}
}
if(message.contains("OCCUPY")) {
System.out.println("OCCUPY");
for(Building u : Game.buildingList) {
if(u.getX() == Integer.parseInt(message.substring(4,6))
&& u.getY() == Integer.parseInt(message.substring(6,8))) {
u.setOwner(Integer.parseInt(message.substring(8,10)));
u.setSS();
}
}
}
if(message.contains("LOSE")) {
System.out.println("LOSE");
LoseWindow loseWindow = new LoseWindow();
}
if(message.contains("TURN1")){
Game.turn = true;
}
}
}
public void snd(String s) {
out.println(s);
}
public String rcv() {
String message = null;
try {
message = in.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return message;
}
public void closeConnection() {
try {
socket.close();
System.out.println("Zakonczono polaczenie.");
} catch (IOException e) {
e.printStackTrace();
}
}
public Socket getSocket() {
return socket;
}
}
助けてくれてありがとう!