socket between java client and c++ server

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • happyvalley

    socket between java client and c++ server

    Hi,
    don't know if it is the place to ask this question

    I have a c++ server and a java client communicate via socket

    in Java client
    // create a client socket
    Socket clientSocket = new Socket("localho st", 27015);
    PrintWriter out = new PrintWriter(cli entSocket.getOu tputStream(),
    true);

    String userInput = "amsg";
    System.out.prin tln("send to server:" + userInput.lengt h() + " :" +
    userInput);
    out.println(use rInput);

    in c++ server
    // code to starup the connection
    int recvbuflen = 255;
    char recvbuf[255];
    int iResult = recv(ClientSock et, recvbuf, recvbuflen, 0);
    cout<<"iResult = "<<iResult<<end l;


    the msg sendout in java client "amsg" is 4, while, in the c++ server,
    it says the msg length = 6.
    it always +2.
    when I send the msg from a java client to a java server via socket,
    there is no such problem

    any suggestion. thanks

  • andrewmcdonagh

    #2
    Re: socket between java client and c++ server



    On Dec 14, 10:57 pm, "happyvalle y" <nittanymount.. .@gmail.comwrot e:
    Hi,
    don't know if it is the place to ask this question
    >
    I have a c++ server and a java client communicate via socket
    >
    in Java client
    // create a client socket
    Socket clientSocket = new Socket("localho st", 27015);
    PrintWriter out = new PrintWriter(cli entSocket.getOu tputStream(),
    true);
    >
    String userInput = "amsg";
    System.out.prin tln("send to server:" + userInput.lengt h() + " :" +
    userInput);
    out.println(use rInput);
    >
    in c++ server
    // code to starup the connection
    int recvbuflen = 255;
    char recvbuf[255];
    int iResult = recv(ClientSock et, recvbuf, recvbuflen, 0);
    cout<<"iResult = "<<iResult<<end l;
    >
    the msg sendout in java client "amsg" is 4, while, in the c++ server,
    it says the msg length = 6.
    it always +2.
    when I send the msg from a java client to a java server via socket,
    there is no such problem
    >
    any suggestion. thanks
    Its sending the \eol \cr chars as well... (or in Java's speak '\n'
    - because you used out.println(... ) instead of out.print() )

    Comment

    • happyvalley

      #3
      Re: socket between java client and c++ server


      andrewmcdonagh wrote:
      On Dec 14, 10:57 pm, "happyvalle y" <nittanymount.. .@gmail.comwrot e:
      Hi,
      don't know if it is the place to ask this question

      I have a c++ server and a java client communicate via socket

      in Java client
      // create a client socket
      Socket clientSocket = new Socket("localho st", 27015);
      PrintWriter out = new PrintWriter(cli entSocket.getOu tputStream(),
      true);

      String userInput = "amsg";
      System.out.prin tln("send to server:" + userInput.lengt h() + " :" +
      userInput);
      out.println(use rInput);

      in c++ server
      // code to starup the connection
      int recvbuflen = 255;
      char recvbuf[255];
      int iResult = recv(ClientSock et, recvbuf, recvbuflen, 0);
      cout<<"iResult = "<<iResult<<end l;

      the msg sendout in java client "amsg" is 4, while, in the c++ server,
      it says the msg length = 6.
      it always +2.
      when I send the msg from a java client to a java server via socket,
      there is no such problem

      any suggestion. thanks
      >
      Its sending the \eol \cr chars as well... (or in Java's speak '\n'
      - because you used out.println(... ) instead of out.print() )
      thanks a lot, seems print() doesnot work, should be write()

      Comment

      Working...