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 8697907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:37:38+00:00 2026-06-13T01:37:38+00:00

I would like to communicate with 2 applications : a client in C which

  • 0

I would like to communicate with 2 applications : a client in C which send a message to the server in TCP and the server in Java which receive it and send an acknowledgement.

Here is the client (the code is a thread) :

static void *tcp_client(void *p_data)
{
  if (p_data != NULL)
  {
    char const *message = p_data;

    int sockfd, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        error("ERROR opening socket");
    }

    server = gethostbyname(ALARM_PC_IP);

    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
    serv_addr.sin_port = htons(TCP_PORT);

    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
        error("ERROR connecting");
    }

    n = write(sockfd,message,strlen(message));

    if (n < 0) {
        error("ERROR writing to socket");
    }

    bzero(buffer,256);
    n = read(sockfd,buffer,255);
    if (n < 0) {
      error("ERROR reading from socket");
    }
    printf("Message from the server : %s\n",buffer);
    close(sockfd);
  }

  return 0;
}

And the java server :

try {
    int port = 9015;
    ServerSocket server=new ServerSocket(port);
    System.out.println("Server binded at "+((server.getInetAddress()).getLocalHost()).getHostAddress()+":"+port);
    System.out.println("Run the Client");

    while (true) {
        Socket socket=server.accept();
        BufferedReader in= new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println(in.readLine());
        PrintStream out=new PrintStream(socket.getOutputStream());
        out.print("Welcome by server\n");
        out.flush();
        out.close();
        in.close();
        System.out.println("finished");
    }
} catch(Exception err) {
    System.err.println("* err"+err);
}

With n = read(sockfd,buffer,255); the client is waiting a response and for the server, the message is never ended so it doesn’t send a response with PrintStream.

If I remove these lines :

bzero(buffer,256);
n = read(sockfd,buffer,255);
if (n < 0) {
  error("ERROR reading from socket");
}
printf("Message from the server : %s\n",buffer);

The server knows that the message is finished but the client can’t receive the response.

How solve that ?

Thank you

  • 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-06-13T01:37:40+00:00Added an answer on June 13, 2026 at 1:37 am

    in.readLine() waits for a NL,CR or CR+NL in the stream. Make sure you are sending that in your client message.

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

Sidebar

Related Questions

I would like to create a server and client application that communicate via sockets
My Flash movie would like communicate with MySQL server to fetch and save data
My target: Create node.js server and client applications that will send antyhing (event, string,
I have a client and a server application which communicate over .NET 2.0 Remoting
Would like to make anapplication in Java that will not automatically parse parameters used
Would like to know the c# code to actually retrieve the IP type: Static
I have written a game in PlayN, which has to communicate with a JavaEE-Server
I need to write an application in java which communicates with a web server.
I would like to make an application for iOS devices that would communicate with
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode

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.