2

Im developing a client-server app. The client side is Java based, the server side is C++ in Windows.

Im trying to communicate them with Sockets, but im having some trouble.

I have succesfully communicated the client with a Java Server, to test if it was my client that was wrong, but its not, it seems like im not doing it right in the C++ version.

The java server goes like this:

    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;


    public class Server {

     public static void main(String[] args){
         boolean again = true;
         String mens;
      ServerSocket serverSocket = null;
      Socket socket = null;
      DataInputStream dataInputStream = null;
      DataOutputStream dataOutputStream = null;

      try {
       serverSocket = new ServerSocket(12321);
       System.out.println("Listening :12321");
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }

      while(again){
       try {
           System.out.println("Waiting connection...");
        socket = serverSocket.accept();
        System.out.println("Connected");
        dataInputStream = new DataInputStream(socket.getInputStream());
        dataOutputStream = new DataOutputStream(socket.getOutputStream());
        while (again){
            mens = dataInputStream.readUTF();
            System.out.println("MSG: " + mens);
            if (mens.compareTo("Finish")==0){
                again = false;
            }
        }
       } catch (IOException e) {
           System.out.println("End of connection");
        //e.printStackTrace();
       }
       finally{
        if( socket!= null){
         try {
          socket.close();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          //e.printStackTrace();
         }
        }

        if( dataInputStream!= null){
         try {
          dataInputStream.close();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }
        }

        if( dataOutputStream!= null){
         try {
          dataOutputStream.close();
         } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
         }
        }
       }
      }
      System.out.println("End of program");
     }
    }

The client just makes a connection and sends some messages introduced by the user.

Could you please give me a similar working server but in C++ (in Windows)? I can't make it work by myself.

Thanx.

Alex
  • 1,449
  • 4
  • 18
  • 28

1 Answers1

0

Your problem is that you are sending a java string which could take 1 or 2 bytes per character (see bytes of a string in java?)

You will need to send and receive in ascii bytes to make things easier, imagine data is your data string on the client side:

byte[] dataBytes = data.getBytes(Charset.forName("ASCII")); 

for (int lc=0;lc < dataBytes.length ; lc++)
{
    os.writeByte(dataBytes[lc]);
}

byte responseByte = 0;
char response = 0;

responseByte = is.readByte();
response  = (char)responseByte;

where is and os are the client side DataInputStream and DataOutputStream respectively.

You can also sniff your tcp traffic to see what's going on :)

Community
  • 1
  • 1
Victor Parmar
  • 5,719
  • 6
  • 33
  • 36