Sunday, December 1, 2013

Udp on how to build a data portion of the packet issue

Currently, the following requirements:
udp packet format data structure portion are:

Note
field length data typesidentifier 8 Char fixed: ABCDEFGH
Length 4 Int deficiencies in the back fill 0
Phonenum 16 Char padded 0
behindPassword 16 Char padded 0
behindCommand 4 Int 1001
When Sn 4 Int command sequence number generated by the sender, the server returns a response, which is used to determine a command for the return.
Param limitation Char according to command, the value is different
CRC 4 Int CRC32 parity bit

I use the following structure to build, and do not know right

typedef struct {
char identifier [8];
int length;
char phonenum [16];
char password [16];
int command;
int sn;
char param [1];
int crc;
} Udpdata;

Param will be assigned to one letter, so I created for a byte; If param is variable, how can I create?
I then created a udpdata * senddata;
Individual members senddata used as follows for assignment
senddata = malloc (sizeof (udpdata));
memset (senddata, 0, sizeof (udpdata));
memcpy (senddata-> identifier, "ABCDEFGH", 8);
memcpy (senddata-> phonenum, "12345678900", 11);
memcpy (senddata-> password, "123456", 6);
memcpy (senddata-> param, param, strlen (param));
senddata-> command = command;
senddata-> sn = 1;
senddata-> length = sizeof (* senddata);

char buf [sizeof (* senddata)];
memset (buf, 0, sizeof (* senddata));
memcpy (buf, senddata, sizeof (* senddata));
CRC * crc = [[CRC alloc] init];
senddata-> crc = [crc udp_crc32: buf Length: sizeof (* senddata) -4];
CRC, I wrote another class, whether it's temporary may be right or wrong.

There are several fields above the requirements of the "lack of back up 0", do not know how to make?
If the data portion udp interface to create and assign Well, then how to convert it to data can be sent out of it, such as convert NSData? Which mainly contains the int type of field, how to convert, convert general about what kind of format. . . . .

Rookie ask you ~ ~ ~ Thank you! Urgent
Reply:
Do not bother you with the use AsyncSocket. Nsstring directly convert dsdata then sent on it
Reply:
I do not want too much trouble, but the other side of the interface is so, each time a packet is received that is in accordance with the corresponding byte field unpack analysis, and then return the data, if nsstring, ask how to deal with the size in bytes , there are zeros that how to deal with it?
Reply:
cited a floor reply:
do not bother you with the use AsyncSocket. Nsstring directly convert dsdata then sent on it


I do not want too much trouble, but the other side of the interface is so, each time a packet is received that is in accordance with the corresponding byte field unpack analysis, and then return the data, if nsstring, ask how to deal with the size in bytes , there are zeros that how to deal with it?
Reply:
cited a floor reply:
do not bother you with the use AsyncSocket. Nsstring directly convert dsdata then sent on it


I do not want too much trouble, but the other side of the interface is so, each time a packet is received that is in accordance with the corresponding byte field unpack analysis, and then return the data, if nsstring, ask how to deal with the size in bytes , there are zeros that how to deal with it?
Reply:
Such an agreement would have one by one byte right, another string of zeros is for, shaping impossible zeros
Variable length arrays need to specify the length of the agreement, the general format of this protocol is the protocol type, data length, data, where the data can also be nested
Reply:
The interface can be changed without the other? This is too rigid it, that there is no compatibility
Reply:
references, 5th Floor reply:
such an agreement would have one by one byte right, another string of zeros is for, shaping impossible zeros
Variable length arrays need to specify the length of the agreement, the general format of this agreement is the protocol type, data length, data, where the data can also be nested


Can you give an example of it?
Reply:
reference to the 7th floor of replies:
references, 5th Floor reply:

Such an agreement would have one by one byte right, another string of zeros is for, shaping impossible zeros
Variable length arrays need to specify the length of the agreement, the general format of this protocol is the protocol type, data length, data, where the data can also be nested


Can you give an example of it?

General variable length data should be placed at the end, it is easy to first read all the data except for variable-length data, and given the length of the front of the structure to a variable-length data in order to read the second structure. So you probably can this structure:
typedef struct {
char identifier [8];
char phonenum [16];
char password [16];
int command;
int sn;
int crc;
int length ;/ / param this length refers to the length of
char param [1];
} Udpdata;
Finally, note what byte alignment problems.
Reply:
referenced 8th Floor reply:
reference to the 7th floor of replies:

References, 5th Floor reply:

Such an agreement would have one by one byte right, another string of zeros is for, shaping impossible zeros
Variable length arrays need to specify the length of the agreement, the general format of this protocol is the protocol type, data length, data, where the data can also be nested


Can you give an example of it?

General variable length data should be placed at the end, it is easy to first read all the data except for variable-length data, and given the length of the front of the structure to a variable-length data in order to read the second structure. So you ......

Field order of the interface is a good definition of, andriod has developed a corresponding app, can not be changed;
I want to know is how to structure the data in the conversion look through
[Self.sendSocket sendData: databuf
toHost: self.hostIP
port: ServerPort
withTimeout: -1
tag: senddata-> sn];
Send out;
Is sendData: how to build behind that argument with that structure
Reply:
That is simple:
struct buf ;/ / assumptions which have been filled in data
NSData * data = [NSData dataWithBytes: & buf length: buf.length];
. . .
Reply:
reference to the 10th floor of the reply:
that simple:
struct buf ;/ / assumptions which have been filled in data
NSData * data = [NSData dataWithBytes: & buf length: buf.length];
. . .

Please help us to see why I can not put udp packets sent out?
Part of the code is as follows:
Data to be transmitted databuf, server address: self.hostIP, both sides use the same port ServerPort
I loom ip10.10.27.101, including sending udp
networkself.sendSocket = [[AsyncUdpSocket alloc] initWithDelegate: self];
[Self.sendSocket setRunLoopModes: [NSArray arrayWithObject: NSRunLoopCommonModes]];
NSError * error = nil;
[Self.sendSocket bindToAddress: @ "10.10.27.101" port: ServerPort error: & error];
[Self.sendSocket receiveWithTimeout: -1 tag: 0];
[Self.sendSocket sendData: databuf toHost: self.hostIP port: ServerPort withTimeout: -1 tag: senddata-> sn];

No comments:

Post a Comment