1

好的,我有问题。我有一个用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;
}
}

提前感谢您的帮助!

4

2 回答 2

1

问题似乎是您尝试确定要发送的字节数的方式:sizeof(sndBuff[100])。由于 sndBuff 是 char[100],sizeof(sndBuff[..])是单个 char 的大小。所以你总是只发送一个字节(此外,100 甚至不是一个有效的索引)。只需用于strlen(sndBuff)确定要发送的字节数。

编辑 第二点被认为是您的客户读取 unsingin.readLine()但您没有发送任何换行符。尝试添加它们。

于 2013-06-27T09:18:42.910 回答
1

在 java 客户端,您正在使用 readline 函数从套接字读取,该函数将在给出输出之前等待新行到来或套接字关闭。我建议您在从服务器发送的消息中附加 '\n'。

于 2017-09-09T21:38:51.337 回答