| |

VerySource

 Forgot password?
 Register
Search
View: 1001|Reply: 8

How does socket send record type variables?

[Copy link]

2

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-3-23 20:00:02
| Show all posts |Read mode
// 1. Structure definition
    TMsgHead = packed record
      iMsgLen: Longint;
      iMsgID: Longint;
      sMsgSerial: Array [0..11] of char;
    end;

    TMsgBody_Bind = packed record
      iLoginType: Byte;
      sLoginUser: Array [0..15] of char;
      sLoginPwd: Array [0..15] of char;
      sReserve: Array [0..7] of char;
    end;

    TMsg_Bind = packed record
      MsgHead: TMsgHead;
      MsgBody: TMsgBody_Bind;
    end;

// 2. Assignment --------------------------------------------- -
function TLT.BindData_Create (): TMsg_Bind;
var
  BindData: TMsg_Bind;
begin
  ZeroMemory (@BindData, Sizeof (BindData));
  BindData.MsgBody.iLoginType: = $ 1;
  ..... assignment operation


// Call, send --------------------------------------------- -
procedure TForm1.SckLtConnect (Sender: TObject; Socket: TCustomWinSocket);
 var
  BindData: TMsg_Bind;
begin
  BindData: = Lt.BindData_Create;
  sckLt.Socket.SendBuf (BindData, sizeof (BindData));
.........
  result: = BindData;

// ------------------------------------------------ -
The current problem is that after assigning a value to the iloginType and other types in the structure, the socket is used to send the structure. It is found that the number 1 is represented by 01 00 00 00 instead of 00 00 00 01. How does this happen, the server The requirement must be 00 00 00 01. What should I do? ?

Look forward to expert advice!
Reply

Use magic Report

0

Threads

40

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-7-4 13:45:02
| Show all posts
Don't know how to help

The number 1 is represented by 01 00 00 00
, Instead of 00 00 00 01, it means that it is not 1.

Seriously concerned
Reply

Use magic Report

0

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-7-4 16:45:02
| Show all posts
Try to remove the packed keyword
Reply

Use magic Report

2

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

 Author| Post time: 2020-7-7 14:15:01
| Show all posts
Can't remove the packed, just tried

If it doesn't work, you have to abandon the structure and apply for a 61-byte buffer. Then convert the order yourself.

However, I still look forward to the expert's opinion and close the post in the afternoon.
Reply

Use magic Report

0

Threads

40

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-7-7 17:00:01
| Show all posts
It's very difficult. In DELPHI, integers are represented like this, with the high bit behind. If you manually shift it, the value it represents changes, if you need few integers, such as between 0-9, you can use characters to represent, so that it is easy to deal with.
Reply

Use magic Report

1

Threads

5

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-7-8 19:00:01
| Show all posts
You can also define the same RECORD type on the receiving end. When receiving, directly RECV into RECORD, it should be no problem.
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-7-9 08:30:01
| Show all posts
Multi-byte integers in Intel's CPU are low-order first and high-order second.
In Motorola's CPU, the multi-byte integer is high-order first and low-order second.
From the description of the landlord, the server-side program has been written by others. If this is the case, you can only write the conversion function to convert the order.
Reply

Use magic Report

0

Threads

40

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 China

Post time: 2020-7-9 13:15:02
| Show all posts
The landlord is a server written in C++
Reply

Use magic Report

2

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

 Author| Post time: 2020-7-15 20:15:01
| Show all posts
Put away the original record, and reuse Array[..] of Byte, pain.....
Then write your own processing function, convert bytes...

But it's still done.. Thank you all!!!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list