| |

VerySource

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

How does socket send record type variables?

[Copy link]

2

Threads

6

Posts

7

Credits

Newbie

Rank: 1

Credits
7

 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

33

Posts

23

Credits

Newbie

Rank: 1

Credits
23

 China

Post time: 6 day(s) ago
| 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

5

Posts

6

Credits

Newbie

Rank: 1

Credits
6

 China

Post time: 6 day(s) ago
| Show all posts
Try to remove the packed keyword
Reply

Use magic Report

2

Threads

6

Posts

7

Credits

Newbie

Rank: 1

Credits
7

 China

 Author| Post time: 3 day(s) ago
| 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

33

Posts

23

Credits

Newbie

Rank: 1

Credits
23

 China

Post time: 3 day(s) ago
| 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

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: The day before yesterday 19:00
| 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

4

Posts

5

Credits

Newbie

Rank: 1

Credits
5

 China

Post time: Yesterday 08:30
| 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

33

Posts

23

Credits

Newbie

Rank: 1

Credits
23

 China

Post time: Yesterday 13:15
| Show all posts
The landlord is a server written in C++
Reply

Use magic Report

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

Points Rules

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

Quick Reply To Top Return to the list