• 0

Bytes with TCP socket and bit manipulation


Question

I'm taking a computer communication summer class and this is our 2nd and last program project. The protocol for the assignment can be found here.

I've already done one project that involves a client and server connecting and playing a game of guess the number with strings being sent back and forth. For this project though we have to manipulate individual bits of bytes and send only bytes across the socket connection. How do you do this in Java? I haven't been able to find anything about forming bytes with particular bits and sending just bytes across the connection.

11 answers to this question

Recommended Posts

  • 0

I'm not sure what your project assignment is doing (I dont' have time to read it). But from my experience in Java, to read binary data you use InputStream or OutputStream to read and write binary data.

Here's an example:

Let's assume "sock" is your socket connection.

InputStream in = sock.getInputStream();
while (in.read() != -1)
{
     //do whatever you need to do with it
}

You can also look at sun's java api for more info (search for inputstream).

Hope this helps!

  • 0

I've read some of the things on the input and output streams and i'll be definitely be using them. My problem comes after I've received the bytes. I have to use certain control bytes as notifiers for things:

00000001- REQuest connection.

00000010 - CONnection Granted.

ssss0011 - FILe name packet (sequence number sss = 000).

ssss0100 - DATa Packet, where sss is the sequence number of the packet.

ssss0101 - Short Data Packet(SDP), where sss is the sequence number of the packet.

ssss0110 - ACKnowledgment packet, where sss is the sequence number of the packet expected next.

ssss 0111 - list request packet (sequence number sss = 000).

0000 1111 - TERmination (a simple two-way handshake).

After I've received the bytes I don't understand how to interpret the different bits of the bytes to know what I'm dealing with.

  • 0

You can AND them (note this is C-style code)

if(byte == 0x01) // REQuest connection
{
}
else if(byte == 0x02) // CONnection granted
{
}
else if(byte & 0x0F == 0x03) // FILe name packet
{
    sequence = (byte & 0xF0) >> 4;
}

  • 0

Thx for the info Andareed!

I've taken computer structures so I understand the idea of And'ing and Or'ing things but I don't quite understand the hex strings your using. Any extra tips on those? I tried google'ing some stuff but it didn't really help. Also, what exactly does the >> do.

Edit:

Since hex is 16 bits I'm guessing the first digit is the first 4 bits and when you check for 0x0F you're testing to see if the first 3 bits are all 1's to know it's a file packet and similarly for the others. The >> still confuses me though.

Edited by Little Moe
  • 0

I've run into another problem. When sending a packet with file information one of the bytes has to be a one-byte unsigned binary value. I've found a whole lot of stuff about doing unsigned bytes in Java by using ints and AND'ing stuff with 0x0FF but my I think my problem comes with actually sending the byte.

My teacher expects our clients and servers to run against his C clients and servers. He gave us an example exe of each and whenever I run my client against his it says "Number of char in file name should be between 1 and 124 - LEN field = 0" like it's not getting my byte at all. I'm thinking it's either the byte I'm using or something special in the OutputStream that might be messing up my byte? I know there is no difference between the bits of a signed or unsigned byte but something is going wrong. Does anyone have an example of sending a byte like he is asking or maybe if I'm thinking of this wrong.

  • 0

I ended up finishing the project. My biggest problem starting off was I was trying to send bytes individually by just writing the bytes one at a time. My teacher was expecting the bytes one right after another. I had to send a byte array for him to get everything in a packet that I wanted. Couldn't just test one byte at a time. Had to test things just by hardcoding byte array values to see what happened.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Go ###### yourself Apple. What an embarassing piece of ###### company.
    • From code to combat: Meta CTO calls for Silicon Valley involvement in military contracts by Hamid Ganji This week, Meta announced a partnership with the defense technology startup Anduril to build next-gen VR/AR extended reality headsets for the US military. Microsoft was given the contract in 2018, and the headsets were supposed to be built based on Microsoft HoloLens. Earlier this year, Microsoft gave up the entire project to Anduril but kept its role as the cloud services provider. Meta's Chief Technology Officer (CTO) is now calling for more Silicon Valley involvement in military contracts. Speaking at the Bloomberg Tech summit in San Francisco (via: Business Insider), Andrew Bosworth said the recent partnership between Meta, Anduril, and the US military could be a "return to grace" for Silicon Valley. "The Valley was founded on a three-way investment between the military, academics, and private industry. That was the founding of it," Bosworth said. Meta's CTO added that building VR/AR headsets for the US military doesn't turn the company into a defense contractor. He also said it was "way too early" to determine whether military contracts would become a business segment for Meta. "So far, it's like a zero. Let's start with one and go from there. I think there's no reason it couldn't be meaningful in the impact that it has," he added. The US military's desire for AI-powered tools and weapons has turned Big Tech into military contractors, whether willingly or unwillingly. Companies like Microsoft, Google, and Meta have a long history of providing services to the military and law enforcement agencies. The relationship between tech firms and the defense segment has always been controversial. In the most recent case, a group of Microsoft employees protested against the company's partnership with the Israeli military, which led to the layoff of the protesting staff.
    • Or run msinfo32! With Windows 11, Microsoft believes it has reinvented the wheel?
    • The long-awaited Nothing Phone (3) is finally coming next month, launch date confirmed by Aditya Tiwari London-based consumer electronics brand Nothing is due to launch its latest flagship in 2025. The company dropped a new teaser for the Phone (3), revealing when the flagship device will be out on the market. Nothing Phone (3) will be unveiled during a live event on July 1 at 1:00 PM ET / 10:00 AM PT / 6:00 PM BST / 11:30 PM IST. It has already created a live event titled "Come to Play" on its official YouTube channel, for which you can add a reminder by clicking on the "Notify Me" button. Nothing CEO Carl Pei has previously dropped several details about the unreleased smartphone. He took part in a social media AMA earlier this year and said Nothing Phone (3) will arrive in the third quarter of 2025. Pei confirmed that Nothing Phone (3) will make its way to the US this time after a dry spell since 2023. However, he didn't specify whether the device will be sold directly or through the beta channel, which currently includes Phone (3a), Phone (3a) Pro, and CMF Phone 2 Pro. The US has been a rocky terrain for Nothing. Its first smartphone was made available through the beta channel, and the Nothing Phone (2) is the only smartphone from the company that has been widely available in the US. The Glyph interface featured on the back of Nothing smartphones has remained a differentiating factor from the start. However, the smartphone maker recently posted a 9-second video in which the Glyph lights on the back of a Nothing smartphone abruptly turn off. "We killed the Glyph Interface," the company said. It makes sense when you check out the Phone (3) teaser and see dot matrix-style lights being flashed in a pattern. Nothing released another teaser about a week ago, featuring the number 3 lit up as dot matrix LEDs. If you're looking for some trivia, Nothing product manager Raymond Zhu estimated in a Q&A video that the company would need to sell about 250,000 Phone (3) units to turn a profit. Answering another question, he added that their biggest weakness is "no one knows us", and the company is struggling to reach the masses without high marketing budgets. Let's wait to see what Nothing has in store for Phone (3) next month other than the new physical button. Speaking of the future of smartphones, the Nothing CEO believes that our entire software experience will eventually be condensed down to just one app.
    • As far as I can remember, no one has done a 4v4 before. 2v2? yes. 3v3? yes.
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      403
    2. 2
      +FloatingFatMan
      179
    3. 3
      snowy owl
      174
    4. 4
      ATLien_0
      170
    5. 5
      Xenon
      135
  • Tell a friend

    Love Neowin? Tell a friend!