Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
206 views
in Technique[技术] by (71.8m points)

C++ Client unable to communicate with Java server

I have written the client in Java is able to communicate with the server in Java. C++ Client is not working with server written in java. Can someone please help me to resolve this problem. I have provided the code of all the 3(Java server, Java Client, C++ Client)

C++ Client:

#include <iostream>
#include <conio.h>
#include <winsock2.h>
#include<string>



int main(int argc, char* argv[])
{
   WSAData version;        //We need to check the version.
    WORD mkword=MAKEWORD(2,2);
    int what=WSAStartup(mkword,&version);
    if(what!=0){
    std::cout<<"This version is not supported! - 
"<<WSAGetLastError()<<std::endl;
    }
    else{
    std::cout<<"Good - Everything fine!
"<<std::endl;
    }

    SOCKET u_sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(u_sock==INVALID_SOCKET)
    std::cout<<"Creating socket fail
";

    else
    std::cout<<"It was okay to create the socket
";

    //Socket address information
    sockaddr_in addr;
    addr.sin_family=AF_INET;
    addr.sin_addr.s_addr=inet_addr("192.168.45.88");
    addr.sin_port=htons(15000);
    std::cout<<"Successfully provided the address"<<std::endl;    
    /*==========Addressing finished==========*/

    //Now we connect
    int conn=connect(u_sock,(SOCKADDR*)&addr,sizeof(addr));
    std::cout<<"conn value:"<<conn<<std::endl;
    if(conn==SOCKET_ERROR){
    std::cout<<"Error - when connecting "<<WSAGetLastError()<<std::endl;
    closesocket(u_sock);
    WSACleanup();
    }
     std::cout<<"Successfully connected to server"<<std::endl;

     //Send some message to remote host
    char* mymsg="Hello Server...How are you?";
    char vect[512]={0};

    int smsg=send(u_sock,mymsg,sizeof(mymsg),0);
    if(smsg==SOCKET_ERROR){
    std::cout<<"Error: "<<WSAGetLastError()<<std::endl;
    WSACleanup();
    }

    int get=recv(u_sock,vect,512,0);
    if(get==SOCKET_ERROR){
    std::cout<<"Error in Receiving: "<<WSAGetLastError()<<std::endl;
    }
    std::cout<<vect<<std::endl;
    closesocket(u_sock);

    getch();

    return 0;

}

Java Server:

import java.net.*;

import java.io.*;



public class EchoServer {

    public static void main(String[] args) throws IOException {



       /* if (args.length != 1) {

        System.err.println("Usage: java EchoServer <port number>");

        System.exit(1);

    }*/

    int portNumber = 15000;

    try (

        ServerSocket serverSocket =  new ServerSocket(portNumber);

        Socket clientSocket = serverSocket.accept();     

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);                   

        BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    ) {

        String inputLine;

        while ((inputLine = in.readLine()) != null) {
        System.out.println(inputLine);
        out.println(inputLine);

        }

    } catch (IOException e) {

        System.out.println("Exception caught when trying to listen on port "

        + portNumber + " or listening for a connection");

        System.out.println(e.getMessage());

    }

    }

}

Java Client:

import java.io.*;
import java.net.*;

public class EchoClient {
    public static void main(String[] args) throws IOException {

    if (args.length != 0) {
        System.err.println(
        "Usage: java EchoClient <host name> <port number>");
        System.exit(1);
    }

    String hostName = "192.168.45.88";
    int portNumber = 15000;

    try (
        Socket echoSocket = new Socket(hostName, portNumber);
        PrintWriter out =
        new PrintWriter(echoSocket.getOutputStream(), true);
        BufferedReader in =
        new BufferedReader(
            new InputStreamReader(echoSocket.getInputStream()));
        BufferedReader stdIn =
        new BufferedReader(
            new InputStreamReader(System.in))
    ) {
        String userInput;
        while ((userInput = stdIn.readLine()) != null) {
        out.println(userInput);
        System.out.println("echo: " + in.readLine());
        }
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host " + hostName);
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to " +
        hostName);
        System.exit(1);
    }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Few things :

1) when sending data with winsock sockets, sizeof (mymsg) is wrong, it should be strlen (mymsg)+1

2) I have written a Java-Server-C++ -Client before , and the main thing is dealing with ASCII vs. UNICODE. in the Java side receiving , make sure it builds the string from ASCII character as encoding. on the other hand , when sending data to winsock server - send it as binary data , otherwise the winsock server won't be able to finish recv() function .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...