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

  • Home
  • SEARCH
  • 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 6089983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:09:43+00:00 2026-05-23T12:09:43+00:00

I am fairly new to programming in both Java and C and need some

  • 0

I am fairly new to programming in both Java and C and need some help.
So I have a C application that sends out structures over UDP :

#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>


int main(int argc, char**argv)
{
   int sockfd,n;
   struct sockaddr_in servaddr,cliaddr;

struct dataType {
    char name[4];
    unsigned short did;
    unsigned short sid;
    unsigned short type:4,pri:2,cb:2,flags:8;
    unsigned char pblock;
    unsigned char tblock;
    unsigned short mess;
    unsigned int window:24;
};

struct dataType sample_header;
strcpy(sample_header.name,"TEST");
sample_header.did=23;
sample_header.sid_id=1;
sample_header.type=1;
sample_header.pri=01;
sample_header.cb=1;
sample_header.flags=18;
sample_header.pblock=10;
sample_header.tblock=20;
sample_header.mess3;
sample_header.window=123890;

sockfd=socket(AF_INET,SOCK_DGRAM,0);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr=inet_addr(argv[1]);
servaddr.sin_port=htons(6120);
sendto(sockfd, (char *)&sample_header, (sizeof(struct dataType)),0,(struct sockaddr *)&servaddr,sizeof(servaddr));

}

Now I need to be able to receive this data over UDP in Java and populate an object with these values. I can get the first string (name) but not sure how to go about getting the remaining items.

 import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.nio.*;

public class UDPReceive {
  public static void main(String args[]) {
    try {
      int port = 6120;

      DatagramSocket dsocket = new DatagramSocket(port);
      byte[] buffer = new byte[1024];
      DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
      while (true) {
        dsocket.receive(packet);
        byte[] data = packet.getData();
        String msg1 = new String(data, 0, 5);
        System.out.println("Here is SOME :" + msg1);
        packet.setLength(buffer.length);
      }
    } catch (Exception e) {
      System.err.println(e);
    }
  }
}

I had a look at Google’s protocol buffers but it obviously requires change on both sides.
I also need to be able to eventually send out this data from Java object to UDP and back into C structure.

Many 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-05-23T12:09:43+00:00Added an answer on May 23, 2026 at 12:09 pm

    You should not in general write out C structures to the network due to endian and bit packing issues. Instead you want to go through a marshalling and encoding process where you write out each structure memory in a portable network encoding format. The other side reads in the portable network format and stores it however it needs to.

    This is how all Internet standard RFCish protocols work. They define order, encodings, endianness, and other semantics of all of the fields. Yes, they sometimes arrange it so that a carefully constructed structure can be overlaid on the network buffer, but even then they often need to perform htons() or friends on the buffers to get them into the correct endian-ness.

    I recommend taking this approach.

    Reading your structure, it is not laid out in a manner which promotes being sent directly on the network. It would seem very likely that there are holes in the structure, for example, which would be very difficult for other systems to decode properly.

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

Sidebar

Related Questions

I'm fairly new to programming C# and I have written a program that uses
I am fairly new to web programming, I have mainly used java to create
I'm new to Android programming however I do have some Java experience. What I
I'm fairly new to C# programming. I am making a program for fun that
I am fairly new to Emacs and I have been trying to figure out
I'm fairly new to programming and new to java, but I'd like to jump
I'm fairly new to programming but I've been reading some interesting discussions on StackOverflow
I am fairly new to Perl programming, but I have a fair amount of
I am fairly new to iPhone/ObjC programming, though I have several years of experience
I'm fairly new at programming, but I've wondered how shell text editors such as

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.