Strcture data communication between java and c socket programming



// 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 as jobtype = h1 budget = 20 and time = 12 in java.


/* i'm getting these output in C jobtype :: budget :: time :: h1


*/


No comments:

Post a Comment