Results 1 to 5 of 5
  1. #1
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    56
    Rep Power
    0

    Default unsigned chars over udp socket

    I can have up to 100,000 simultaneous connections to a udp socket, which needs to be handled by seperate threads. Each of the connections talk to a c program on an embedded device that sends and responds with an array of unsigned chars. Now Java does not have an unsigned char type. The closest you can use is a short. But DatagramPacket requires a byte argument:

    public class Socket {
    public Socket(int port) throws IOException {
    new SocketThread().start();
    }
    }

    public class SocketThread extends Thread {
    protected DatagramSocket socket = null;

    public SocketThread() throws IOException {
    this("SocketThread");
    }

    public SocketThread(String name) throws IOException {
    super(name);
    socket = new DatagramSocket(1732);
    }
    public void run() {
    try {
    byte[] buf = new byte[256];

    DatagramPacket packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet);

    System.out.println( new String(packet.getData()) ) ;

    InetAddress address = packet.getAddress();
    int port = packet.getPort();
    packet = new DatagramPacket(buf, buf.length, address, port);
    socket.send(packet);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    So how can I go about reading and writing unsigned chars over a udp socket?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: unsigned chars over udp socket

    Assign the char to an int and use bitwise AND and shift to get the individual byte values.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    johnmerlino is offline Member
    Join Date
    May 2014
    Posts
    56
    Rep Power
    0

    Default Re: unsigned chars over udp socket

    Quote Originally Posted by Norm View Post
    Assign the char to an int and use bitwise AND and shift to get the individual byte values.
    thanks for reply. The second problem is as follows:

    new DatagramPacket(buf, buf.length);

    It appears DatagramPacket requires you to specify maximum size of the buffer. But the data coming in is unknown length. Is there a way to pass DatagramPacket a buffer of unspecified size and have it grow as needed?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: unsigned chars over udp socket

    Have you tried some experiments with different sizes to see what happens?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,275
    Rep Power
    6

    Default Re: unsigned chars over udp socket

    Check the answer in this thread (google: java DatagramPacket variable size)

    java - UDP socket Datagram receive packet 2x to get full message - Stack Overflow
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. What does it mean to be signed and unsigned?
    By fatabass in forum New To Java
    Replies: 5
    Last Post: 02-10-2012, 02:16 AM
  2. Replies: 2
    Last Post: 07-21-2011, 04:46 AM
  3. Replies: 1
    Last Post: 02-07-2011, 09:22 PM
  4. again -.- unsigned types
    By prich in forum Networking
    Replies: 1
    Last Post: 10-17-2009, 06:53 PM
  5. writting extended ascii chars on socket........or Endianness Issue......??
    By sachinj13 in forum Threads and Synchronization
    Replies: 8
    Last Post: 09-23-2008, 03:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •