1

Ok, I've got a problem. I have a small project (game) writen in Java (client - server). And everything works fine. But i want rewrite server to ANSI C. I start server, it waits for connection from clients. When I start clients, connections are established. And to this point everything works fine. But, when I'm trying send something to client from server - server sending, but client don't receve it. Any ideas why?

Server source:

#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; } 

Client class for comunicate:

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; } } 

Thanks in advance for help!

2
  • You can not assume send() sends as much as it was told. Neither can you assume recv() receives as much as it was told. Always check the return value and if >0 then match it against the size of data passed in theses calls. Then loop until all was sent/received. Please see the docs for details. Commented Jun 27, 2013 at 9:26
  • Use strlen(), NOT sizeof() Commented Jun 27, 2013 at 9:33

2 Answers 2

1

The problem seems to be the way ou try to determine the number of bytes to send: sizeof(sndBuff[100]). As sndBuff is char[100], sizeof(sndBuff[..]) is the size of a single char. So you always send one byte only (furhermore, 100 is not even a valid index). Just use strlen(sndBuff) to determine the number of bytes to send.

Edit The second point seens to be that your client reads unsing in.readLine() but you don't send any newlines. Try adding them.

Sign up to request clarification or add additional context in comments.

4 Comments

It's not working that way too. I forgot to correct this befor i posted a question here. But it's still not solving my problem.
maybe you can edit your question so we can see the code you are now working with
Done. The only change was that I'm using sizeof(sndBuff) instead sizeof(sndBuff[100]). strlen(sndBuff) also didnt work...
It works! I'm simple sending message like this "message\n". Thanks!
1

At java client you are using readline function to read from socket which will wait before giving output till new line comes or socket closes.I suggest you to append '\n' in your message you are sending from server.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.