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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:50:44+00:00 2026-06-13T02:50:44+00:00

I’m having a bit of a weird problem. I’m trying to write an interface

  • 0

I’m having a bit of a weird problem. I’m trying to write an interface between a C client and a Java server. To this end I’ve written a gateway in Java (which communicates with the server using RMI). Almost everything is working, but I’m trying to return some integers from the gateway to the C client. Here’s the code:

Java gateway:

import java.net.Socket;
import java.net.ServerSocket;
import java.net.MalformedURLException;
import java.io.BufferedReader;
import java.net.InetSocketAddress;
import java.io.InputStreamReader;
import java.io.DataOutputStream;
import java.lang.System;

public class hotelgw{

  public static final int PORT = 4242;
  public static final int BACKLOG = 5;
  public static final int MAX_ARGS = 5;

  public hotelgw(){
    InetSocketAddress address;
    ServerSocket socket = null;
    try{
      address = new InetSocketAddress(PORT);
      socket = new ServerSocket(PORT,BACKLOG);
    }catch (Exception e){
      System.out.println("Error: "+e);
      System.exit(1);
    }

    while(true){

      Socket newsock = null;
      try{
       newsock =  socket.accept();
      }catch (Exception e){
        System.out.println("Error: "+e);
        System.exit(1);
      }

      BufferedReader in = null;
      DataOutputStream out = null;

      try{
        in = new BufferedReader(new InputStreamReader(newsock.getInputStream()));
        out = new DataOutputStream(newsock.getOutputStream());
      }catch (Exception e){
        System.out.println("Error: "+e);
        System.exit(1);
      }


      String[] init_args = new String[MAX_ARGS];
      int i = 0;
      String c;

      try{
        while(!(c = in.readLine()).equals("end")){
          System.out.printf("%s",c);
          init_args[i] = c;
          i++;
        }
      }catch (Exception e){
        System.out.println("Error: "+e);
        System.exit(1);
      }

      for(int j=0;j<init_args.length;j++){
        System.out.printf("init_args[%d] = %s\n", j, init_args[j]);
      }

      int counter = 0;
      for(int j=0;j<init_args.length;j++){
        if(init_args[j] != null){
          counter++;
        }
      }

      String[] final_args = new String[counter];
      for(int j=0;j<counter;j++){
        final_args[j] = init_args[j];
      }

      if(final_args[1].equals("list")){
        int[] list = hotelclient.get_list(final_args);
        System.out.println("list received in hotelgw.java");
        for(int j=0;j<list.length;j++){
          try{
            out.writeInt(list[j]);
          }catch (Exception e){
            System.out.println("Error writing to socket: " + e);
            System.exit(1);
          }
        }
      }


      try{
        in.close();
        newsock.close();
      }catch (Exception e){
        System.out.println("Error: "+e);
        System.exit(1);
      }
    }
  }
}

C client:

#include <stdio.h>
#include <sys/socket.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#define PORT 4242
#define TYPES_OF_ROOMS 3
#define BUFFER_SIZE 64

/*Writes message to socket*/
ssize_t writen(int fd, const void *vptr, size_t n)
{
  size_t nleft;
  ssize_t nwritten;
  const int *ptr;

  ptr = vptr;
  nleft = n;
  while(nleft>0){
    if ( ((nwritten = write(fd,ptr,nleft)) <= 0)){
      if (errno == EINTR)
        nwritten = 0;
      else
        return -1;

    }
    nleft -= nwritten;
    ptr += nwritten;
  }
  return n;
}


int create_socket(char* address){
  struct hostent *server_address;
  struct in_addr *addr;
  struct sockaddr_in server_addr;
  socklen_t addrlen;
  char* ip;

  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if(sockfd<0){
    perror("Error creating socket");
    exit(1);
  }

  server_address = gethostbyname(address);
  if(server_address == NULL){
    fprintf(stderr, "Server not found!\n");
    exit(1);
  }else{
    addr = (struct in_addr*) server_address->h_addr_list[0];
  }

  ip = inet_ntoa(*addr);

  server_addr.sin_family  =  AF_INET;
  server_addr.sin_port    =  htons(PORT);
  server_addr.sin_addr.s_addr  =  inet_addr(ip); 

  addrlen = (socklen_t) sizeof(struct sockaddr_in);

  if(connect(sockfd,(struct sockaddr *) &server_addr, addrlen)){
    perror("Error connecting to server");
    exit(1);
  }

  return sockfd;
}


int main(int argc, char** argv){
  int sockfd, err, i, type1=0, type2=0, type3=0;
  int int_buf[TYPES_OF_ROOMS];
  char string_buf[BUFFER_SIZE];
  char newline = '\n';
  char *end = "end";

  if(argc == 1){
    printf("Usage: hotelgwclient <address> {list,guests,book} [room type] [guest name]\n");
    exit(1);
  }

  sockfd = create_socket(argv[1]);
  printf("Socket created\n");


  if(strcmp(argv[2], "list")==0){
    if(argc != 3){
      printf("Usage: hotelgwclient <address> list\n");
      exit(1);
    }


    printf("list initiated\n");
    writen(sockfd, argv[1], strlen(argv[1]));
    writen(sockfd, &newline , sizeof(newline));
    writen(sockfd, argv[2], strlen(argv[2])); 
    writen(sockfd, &newline, sizeof(newline));
    writen(sockfd, end, strlen(end));
    writen(sockfd, &newline, sizeof(newline));
    printf("Written to socket\n");

    err = read(sockfd, &type1, sizeof(int));
    err = read(sockfd, &type2, sizeof(int));
    err = read(sockfd, &type3, sizeof(int));

    printf("Read from socket\n");
    if(err<0){
      perror("Error reading from socket");
      exit(1);
    }
    /*
    for(i=0;i<TYPES_OF_ROOMS;i++){
      printf("%d\t",int_buf[i]);
    }
    */
    type1 = htonl(type1);
    type2 = htonl(type2);
    type3 = htonl(type3);

    printf("%d\t",type1);
    printf("%d\t",type2);
    printf("%d\t",type3);
    printf("\n");
    return 0;
  }

  printf("Command not recognized.\nUsage: hotelgwclient <address> {list,guests,book} [room type] [guest name]\n");
  exit(1);
}

(N.B. I know it’s messy, I’mma clean it up when it works 🙂 ).

The problem is that whenever I call the list method, and C tries to read the respons (three ints) it will only read one byte, not four. This is fixed if I add a sleep() call before reading from the sockets. Does anybody know what’s happening and what I should do to fix it?!

Thanks!

  • 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-13T02:50:45+00:00Added an answer on June 13, 2026 at 2:50 am

    I suspect the problem is that you are assuming you will read as much data as you want when the minimum is always 1 (even in Java)

    Why you are getting one byte before the others is that you are using DataOutputStream without a BufferedOutputStream so it is sending one byte at a time. It might be tempting to use this workaround but it just hides the underlying problem that you need to be able to read one byte at a time correctly as this is always a possibility.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.