Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Socket connections and Unsigned values

  1. #1
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Socket connections and Unsigned values

    Out of curiosity, I was wondering how you guys handled unsigned values when using sockets.

    The way I imagine handling unsigned values is using bitwise/shift operators (since these "disregard" sign), but that can be quite cumbersome when it comes to things like multiplication/division/modulo operators (add/subtract are very easy to do, not sure on their performance compared to regular add/subtract).

    The other alternative I considered was using the "next size up" method, where you use a data type that's larger than the one you actually want. This means, though, that I'd have to check the bounds every time when using these.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Socket connections and Unsigned values

    unsigned values when using sockets
    Not sure what the relationship is between signs and sockets.
    Can you explain?

    The problem mostly happens when compiler expands bytes to ints, spreading the sign bit. If you know the compiler is doing that, AND the int with 0xFF to remove the spread sign bits.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Socket connections and Unsigned values

    In most cases if I feel I need a bigger number than offered I would just use the next step method and use a long rather than an int. I don't believe the overhead is that massive for this and especially if you use primitives the JVM handles those brilliantly for you.

    It's an interesting question though.

    // Json

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Socket connections and Unsigned values

    Not sure how understanding your data is related to using sockets.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Socket connections and Unsigned values

    hmm, I guess I could have been more specific.

    What I wanted to do was create at UDP socket connection to another application and sending/receiving packets of data to the other application. The other application isn't written in Java, so there is data that needs to be unsigned (unfortunately, this other application can't be changed). I know that there's no problem just sending a signed int "masquerading" as an unsigned int, but the issue comes when doing arithmetic in the Java program.

    I was just wondering whats the best solution to remedy this (either writing some methods to perform the unsigned arithmetic, or using the next size up).

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Socket connections and Unsigned values

    the issue comes when doing arithmetic in the Java program
    Can you explain this bit?
    If the data your java program is reading from an int field that has the H.O. bit = 1 (ie an unsigned 4 billion value) then java would treat it as negative. If your data values are that large, I guess you need to expand the field.
    If you wanted to be able to set values that large in an unsigned int field, you could assign the value to a long and then AND the long with 0xFFFFFFFF to strip the sign bits.

Similar Threads

  1. How to Create a server socket to listen for incoming connections?
    By JavaPF in forum Java Networking Tutorials
    Replies: 3
    Last Post: October 28th, 2011, 09:02 AM
  2. servlet and socket plz HELP
    By Mokhtar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 8th, 2010, 09:21 AM
  3. Access the Same Socket
    By ahmmnhwa in forum Java Networking
    Replies: 5
    Last Post: October 29th, 2009, 11:50 AM
  4. How can i create fake IP addresses to extract information for the DB?
    By neomancer in forum Java Theory & Questions
    Replies: 4
    Last Post: May 8th, 2009, 04:54 AM
  5. Replies: 2
    Last Post: October 7th, 2008, 11:03 PM