• 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

    • I've had similar thoughts about the 2TB one. The price came down to a more tempting level a few months ago, but juggling installs with a far cheaper USB SSD hasn't been so bad. Even big games rip around pretty quick when you can push them at 4gbps, and that drive can be repurposed when its time with the Xbox is done. I'd need an iron-clad promise that it'll be 100% feature complete, next-gen compatible to even consider a 4TB unit.
    • It's not. It's a rightwing lie peddled by liars to BillyBob'sFreedumb email chain. Feel free to provide any credible source to support this claim. You won't find a single one. The reason Biden wasn't prosecuted was because it was a handful of documents from the Obama years that were just in storage. And when the FBI asked for them, Biden and his team did everything they could to find them and return them immediately -- even ones the FBI didn't know were "missing". https://en.wikipedia.org/wiki/...assified_documents_incident Whereas the Charlatan in Chief stored mountains of classified material out in the open in his pay to play club infested with foreign spies. https://www.pbs.org/newshour/p...-mar-a-lago-shower-ballroom Trump also revealed this nation's most classified information to foreigners who had no clearance to access them...and then bragged about knowing them to their friends overseas. https://www.nbcnews.com/politi...arines-according-rcna119173 And not only are many still missing and not returned... https://www.cnn.com/interactiv...ssia-intelligence-trump-dg/ Trump denied having them to the FBI repeatedly and moved them around to avoid having to return them to the proper authorities for over a year. https://www.pbs.org/newshour/p...ing-of-classified-documents There is no credible legitimate comparison between the two classified documents cases. You might want to change the source of the information you are getting and falling for. They are quite obviously lying to you. PS On the content issue...I am a content creator. You and everyone else in the world loves the franchises and content I have created and/or contributed meaningfully to. It's the studios that are doing everything they can to remove us, the content creators, from their balance sheets (now with AI)...not the people who consume what we create for free. We've already been paid, thanks. Blame Wall Street for forcing the bottomless greedy enshittification of everything American, not the consumers, pirates, or the actual creators.
    • https://support.microsoft.com/...61ff-00a1-04e2-2d1f3865450d
    • https://support.microsoft.com/...61ff-00a1-04e2-2d1f3865450d
  • Recent Achievements

    • First Post
      sultangris earned a badge
      First Post
    • Reacting Well
      sultangris earned a badge
      Reacting Well
    • First Post
      ClarkB earned a badge
      First Post
    • Week One Done
      Epaminombas earned a badge
      Week One Done
    • Week One Done
      Prestige Podiatry Care earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      144
    2. 2
      Xenon
      127
    3. 3
      ATLien_0
      124
    4. 4
      +Edouard
      102
    5. 5
      snowy owl
      98
  • Tell a friend

    Love Neowin? Tell a friend!