0

I have a Java socket channel and I'm sending a object data and receiving it in C socket ..

Java Code::

//structure
class data
{
       public String jobtype;
       public String budget;
       public String time ;
}
//creating a Socket Channel and sending data through it in java

Selector incomingMessageSelector = Selector.open();
SocketChannel  sChannel = SocketChannel.open();           
sChannel.configureBlocking(false);
sChannel.connect(new InetSocketAddress("localhost", 5000));
sChannel.register(incomingMessageSelector, SelectionKey.OP_CONNECT);

if(sChannel.finishConnect()==true)
{
     sChannel.register(incomingMessageSelector, SelectionKey.OP_WRITE);
}
int len = 256;
ByteBuffer buf = ByteBuffer.allocate(len);
buf.putInt(len); 
// Writing object of data in socket
buf.put(obj.jobtype.getBytes("US-ASCII"));
buf.put(obj.budget.getBytes("US-ASCII"));
buf.put(obj.time.getBytes("US-ASCII"));
buf.put((byte) 0);
buf.flip();
sChannel.write(buf);

C Code ::

struct data
{
    char time[50];
    char jobtype[50];
    char budget[50];
};

n = read(newsockfd, &size, sizeof(size));
struct data *result = malloc(size);
n = read(newsockfd, result, size);

printf("\njobtype :: %s\nbudget :: %s\ntime :: %s\n",result->jobtype,result->budget,result->time);

After giving input in Java as:

jobtype = h1
budget = 20
time = 12

I'm getting these output in C:

jobtype :: 
budget :: 
time :: h1
1
  • Don't do this. Don't use structs as network protocols. Use network protocols as network protocols. Send the data one field at a time and receive it the same way. Apart from the actual bugs mentioned by @Makyen, you're completely overlooking alignment and padding and endian issues.
    – user207421
    Dec 21, 2014 at 0:46

1 Answer 1

2

The buffer which you are sending from Java to C needs to have exactly the same definition (from a byte point of view) in both languages. In your code that is not the case. The buffer you construct in Java does not have the same format as the struct you are using in C to interpret that buffer. Both the length of the strings and order of the strings do not match between sender (Java) and receiver (C). In addition, the size of the buffer sent does not match the size of the buffer expected based on the length information sent (i.e. you are not sending the correct length of your buffer).

In C you have defined a structure that is 150 bytes long containing 3 char arrays (strings), each 50 bytes long. With the order: time, jobtype, budget

In Java you have created a buffer of variable length with strings of variable length in the order: jobtype, budget, time. Fundamentally, the Java code is creating a variable length buffer where the C code is expecting to map this to a fixed length structure.

While it is not what you desire, your C program is obtaining the jobtype string which you placed first in the buffer and assigns it to time. This is how it is currently written.

Assuming that you leave the C program the same, the portion of your Java code which creates and fills the buffer could look something like:

public ByteBuffer createFixedLengthCString(String src, int len) {
    //If the string is longer than len-1 it is truncated.
    ByteBuffer cString = ByteBuffer.allocate(len);
    if(src.length() > len - 1) {
        //Using len-1 prevents the last 0 in the ByteBuffer from being
        //  overwritten. A final 0 is needed:C uses null (0) terminated strings.
        cString.put(src.getBytes("US-ASCII"), 0, len-1);
    } else {
        //The string is not longer than the maximum length.
        cString.put(src.getBytes("US-ASCII"));
    }
    //Already have null termination. Do not want to flip (would change length).
    //Reset the position to 0.
    cString.position(0);
    return cString;
}

int maxBufLen = 256;
int payloadLen = 150
int cStringLen = 50;

ByteBuffer buf = ByteBuffer.allocate(maxBufLen);
//Tell C that the payload is 150 bytes long.
buf.putInt(payloadLen); 

// Writing object data in the buffer
buf.put(createFixedLengthCString(obj.time,    cStringLen));
buf.put(createFixedLengthCString(obj.jobtype, cStringLen));
buf.put(createFixedLengthCString(obj.budget,  cStringLen));

//Use flip() here as it changes the length of bytes sent to the correct
//  number (an int plus 150) and sets the position to 0, ready for reading.
buf.flip();
while(buf.hasRemaining()) {
    //There is the possibility that a single call to write() will not
    //  write the entire buffer. Thus, loop until all data is written.
    //There should be other conditions which cause us to break out of
    //  this loop (e.g. a maximum number of write attempts). Without such,
    //  if the channel is hung this is code will hang in this loop; effectively
    //  a blocking (for this code) write loop.
    sChannel.write(buf);
}

This answer is only intended to address the specific malfunction you have identified in the question. However, the code as presented is really only appropriate as an example/test of transmitting limited data from one process to another on the same machine. Even for that there should be exception and error handling which is not included here.

As EJP implied in his comment, it is often better/easier to use already existing protocols when communicating over a bit pipe. These protocols are designed to address many different issues which can become relevant, even in simple inter-process communications.

2
  • Very good answer, but the reason for using flip() isn't because 'it changes the number bid beets sent to the correct number' but because it is a design requirement of the Buffer API. Without it you would not only send the wrongs number of bytes but also the wrong data.
    – user207421
    Dec 20, 2014 at 22:47
  • @EJP, Thanks. Yeah, the length comment was left over from an implementation making more of a point of the difference between flip() & position(0). I left it in as it combined with the comment about not using flip() in createFixedLengthCString due to the buffer length (limit) change. I would not say flip() is a "design requirement of the Buffer API". At that point the limit needs to be set to the current position and position(0). I don't believe using flip() itself there is an inherent requirement of the Buffer API (e.g. buf.limit(buf.position()); buf.position(0); would work).
    – Makyen
    Dec 21, 2014 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.