Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualAphton

Posted 11 January 2013 - 01:31 PM



(And it's normally < 100 lines of code)

I highly doubt that since native functions provided by most operating systems dont implement any sort of packet division. Thats basically sth one has to do himself or use a library.
http://www.pcs.cnu.edu/~dgame/sockets/client.c
http://www.pcs.cnu.edu/~dgame/sockets/server.c

Check those out, they are the simplest forms of socket communication examples. Packet division depending on what you mean is handled at the hardware level or at the underlying os level. In short if you send a string you get the string on the other side. You will need to devise a terminating character or sequence so that you can tell what part of your string represents an entire message but this isn't very difficult either. I often use a character that I know will not appear within the actual message (say char 1). When I receive a char 1 break the message, parse the JSON object act and respond. It really is that simple for basic networking.
What I meant with packet division is message-splitting (if thats better to understand).
Im going to try to explain what I really meant further below.
.
Btw the code you provided contains send and recv functions that arent implemented appropriately.
There are 2 problems.
Lets say you wish to send 10 bytes for simplicity's sake.
#1.
x = send(socket, data[0], 10,...)
x <= 0 (either -1 or 0 -> nothing sent; trivial)
x = 10 (everything sent; trivial)
0<x<10 (not everything was sent due to the operating system's internal buffer storing outgoing data being full -->
this implies that one has to track how many bytes were really sent and which need to resend)
#2
lets say send(socket, data[0], 10, ..) returned 10 - so you sent the everything over tcp
x = recv(socket, data[0], 10, ..)
x does not have to be 10!
cases that can happen: x<=0, x=10 or 0<x<10
if 0<x<10 happens, you cant simply assume the data received can be used from now on - because its not a whole "message"/"packet" - its not complete...

"If you send a string you get a string on the other side" This is partially true - you get your string but not in the same shape (damn its really hard to explain =/)
"HelloWorld" 10 bytes: send() may only send lets say 3 bytes -> only "Hel" will be sent
so you need to send the rest by repeating the process
Same happens on the receiving side - you dont have to get 10 bytes or 3.. It can be in different sizes. But its true, you will have your 10 bytes in the end.

Anyway, because of that its essential to implement Streaming-Classes that can store and track how much data has been succeessfully sent & received.

#1Aphton

Posted 09 January 2013 - 06:24 PM

 (And it's normally < 100 lines of code)

I highly doubt that since native functions provided by most operating systems dont implement any sort of packet division. Thats basically sth one has to do himself or use a library.

 

http://www.pcs.cnu.edu/~dgame/sockets/client.c

http://www.pcs.cnu.edu/~dgame/sockets/server.c

 

Check those out, they are the simplest forms of socket communication examples.  Packet division depending on what you mean is handled at the hardware level or at the underlying os level.  In short if you send a string you get the string on the other side.  You will need to devise a terminating character or sequence so that you can tell what part of your string represents an entire message but this isn't very difficult either.  I often use a character that I know will not appear within the actual message (say char 1).  When I receive a char 1 break the message, parse the JSON object act and respond.  It really is that simple for basic networking.

What I meant with packet division is message-splitting (if thats better to understand).

Im going to try to explain what I really meant further below.
.

Btw the code you provided contains send and recv functions that arent implemented appropriately.

There are 2 problems.
Lets say you wish to send 10 bytes for simplicity's sake.

#1.

x = send(socket, data[0], 10,...)

x <= 0 (either -1 or 0 -> nothing sent; trivial)

x = 10 (everything sent; trivial)

0<x<10 (not everything was sent due to the operating system's internal buffer storing outgoing data being full -->
this implies that one has to track how many bytes were really sent and which need to resend)

#2

lets say send(socket, data[0], 10, ..) returned 10 - so you sent the everything over tcp

x = recv(socket, data[0], 10, ..)

x does not have to be 10!

cases that can happen: x<=0, x=10 or 0<x<10

if 0<x<10 happens, you cant simply assume the data received can be used from now on - because its not a whole "message"/"packet" - its not complete...

 

"If you send a string you get a string on the other side" This is partially true - you get your string but not in the same shape (damn its really hard to explain =/)

"HelloWorld" 10 bytes: send() may only send lets say 3 bytes -> only "Hel" will be sent

so you need to send the rest by repeating the process

Same happens on the receiving side - you dont have to get 10 bytes or 3.. It can be in different sizes. But its true, you will get your 10 bytes in the end.

 

Anyway, because of that its essential to implement Streaming-Classes that can store and track how much data has been succeessfully sent & received.


PARTNERS