Network communication between a Java socket (server) and a C ++ socket (client)

I know this should be a fairly common problem, but I could not find a definitive answer on how to do this.

First, suppose we have a Java server that accepts requests such as (I just put the appropriate lines and I made sure to handle the exceptions):

    ServerSocket socket = new ServerSocket(port);
    while (true) {
        ClientWorker w;
        w = new ClientWorker(socket.accept());
        Thread t = new Thread(w);
        t.start();
    }

and then in ClientWorker

    BufferedReader inFromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
    DataOutputStream outToClient = new DataOutputStream(client.getOutputStream());

    String query = inFromClient.readLine();
    // process query here
    String response = "testresponse";

    outToClient.writeBytes(response + "\n");

    outToClient.close();
    inFromClient.close();
    client.close();

Now I can get a java client that works with this server:

String query = "testquery";
Socket queryProcessorSocket = new Socket(queryIp,queryPort);
DataOutputStream queryProcessorDos = new DataOutputStream(queryProcessorSocket.getOutputStream());
BufferedReader queryProcessorReader = new BufferedReader(new InputStreamReader(queryProcessorSocket.getInputStream()));
queryProcessorDos.writeBytes(query + "\n");
String response = queryProcessorReader.readLine();

But how can I get the C ++ client to do the same thing as the java client? I tried a lot of things but nothing works. Ideally, I would not want to touch the java server, is this possible? If someone can point me to a good example or some sample code, that would be very appreciated. I have looked at many websites, but to no avail.

+3
5

. , .

void client(const char* server_address, short server_port)
{
     int     sockfd;
     struct sockaddr_in servaddr;

     sockfd = socket(AF_INET, SOCK_STREAM, 0);

     memset(&servaddr, 0x00, sizeof(servaddr));
     servaddr.sin_family = AF_INET;
     servaddr.sin_port = htons(server_port);
     inet_pton(AF_INET, server_address, &servaddr.sin_addr);

     connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

     //from this point you can start write to the server and wait for its respose

     std::string buffer = "testquery";
     writen(sockfd, buffer.c_str(), buffer.length());

     char *ReadBuffer[512];
     while(1)
     {
         memset(ReadBuffer, 0x00, sizeof(ReadBuffer));
         int n = readn(sockfd, ReadBuffer, sizeof(ReadBuffer));
         if(n <= 0)
         {
             //or you dont have anything to read, or you have a problem
             break;
         }
         //this function does the hard job of knowing what to do with all these data
         processBuffer(ReadBuffer, n);
     }

 close(sockfd);

 }

Posix, , .

.

+5

, " "?

, , ( ) , . ++? ?

+2

(, Java RMI) ( - , -), . , (, TCP/IP + ). , - ​​ , / . - , . Project Dark Star - , Java, Java, C/++, Flash .. .

+1

, ++ ( C COBOL, perl ruby ​​ ...), java-. , , java- ++ ( C, COBOL, perl-, ruby ​​ ...). , .

Internet Sockets, POSIX API.

0

. ++ Java, Windows. , Java .

Later I realized this because of problems with endia. Java VM = big endian, C ++ (on my hardware) was a bit essential. A little problem, but it took some time to understand this lover.

0
source

Source: https://habr.com/ru/post/1726298/


All Articles