Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 529175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:03:51+00:00 2026-05-13T09:03:51+00:00

I know this must be a pretty common problem, but I haven’t been able

  • 0

I know this must be a pretty common problem, but I haven’t been able to find a definitive answer on how to do it.

First, assume we have a java server that accepts queries such as (I’ve just put the relevant lines, and I’ve taken out the exception handling for clarity):

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

and then in the 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();

Right 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 a C++ client to do the same thing as the java client? I’ve tried many things but nothing seems to work. Ideally I wouldn’t want to touch the java server, is that possible? If someone could point me to a good example or some sample code, that would be much appreciated. I searched through a lot of websites but to no avail.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:03:51+00:00Added an answer on May 13, 2026 at 9:03 am

    Here I put a simple code to connect to a server. It may help you if this is your problem.

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

    I’m using Posix standard and the code is very simplified but I think its a start point.

    Regards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there must be a really simple answer to this question, but I
I know this must be a n00b question, but I have to implement a
I know that this question must have been asked and answered a million times,
I know this has been asked a dozen times. I must be brain dead
I know SQL well but I must be missing something really dumb here. This
I tried searching for information on this, but must not know the right terms
This is a pretty simple question, but my Google skills haven't gotten me the
I find this code in Ruby to be pretty intriguing (1..4).inject(&:+) Ok, I know
I know this isn't strictly a programming question but y'all must have experienced this.
I have this scenario, which I think must be pretty common: class Parameter {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.