全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1273
推到 Plurk!
推到 Facebook!

在非windows平台中使用socket透過thread的方式傳值的問題.

缺席
mingking0947
一般會員


發表:30
回覆:32
積分:12
註冊:2005-07-08

發送簡訊給我
#1 引用回覆 回覆 發表時間:2014-03-28 17:44:39 IP:175.98.xxx.xxx 訂閱
<!--[if gte mso 9]><xml><br />

</xml><![endif]--> 14.0pt">:
windowssocketthread,code font-family:"新細明體","serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:
minor-latin;mso-fareast-font-family:新細明體;mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">到
font-family:"新細明體","serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:
minor-latin;mso-fareast-font-family:新細明體;mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">上就有問題mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
新細明體;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin">請看紅色部份Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:新細明體;
mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:
minor-latin">傳到mso-ascii-theme-font:minor-latin;mso-fareast-font-family:新細明體;mso-fareast-theme-font:
minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">的font-family:"新細明體","serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:
minor-latin;mso-fareast-font-family:新細明體;mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">事件內mso-ascii-theme-font:minor-latin;mso-fareast-font-family:新細明體;mso-fareast-theme-font:
minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">取出font-family:"新細明體","serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:
minor-latin;mso-fareast-font-family:新細明體;mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">所帶入的值就有問題font-family:"新細明體","serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:
minor-latin;mso-fareast-font-family:新細明體;mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">連帶使得14.0pt;font-family:"新細明體","serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:
minor-latin;mso-fareast-font-family:新細明體;mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">事件中取到mso-ascii-theme-font:minor-latin;mso-fareast-font-family:新細明體;mso-fareast-theme-font:
minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">要判斷進入那個條件也跟著有問題mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
新細明體;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin">我在猜問題是否出在14.0pt;font-family:"新細明體","serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:
minor-latin;mso-fareast-font-family:新細明體;mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">前一行的14.0pt;font-family:"新細明體","serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:
minor-latin;mso-fareast-font-family:新細明體;mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">沒有被指定在14.0pt;font-family:"新細明體","serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:
minor-latin;mso-fareast-font-family:新細明體;mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">上導致指定到不對的mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;mso-fareast-font-family:
新細明體;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin">如果問題是此"新細明體","serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:新細明體;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:
Calibri;mso-hansi-theme-font:minor-latin">那要如何將font-family:"新細明體","serif";mso-ascii-font-family:Calibri;mso-ascii-theme-font:
minor-latin;mso-fareast-font-family:新細明體;mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">指到 thread?,?source code. 2pas.
mso-ascii-theme-font:minor-latin;mso-fareast-font-family:新細明體;mso-fareast-theme-font:
minor-fareast;mso-hansi-font-family:Calibri;mso-hansi-theme-font:minor-latin">以下環境在 xe5 update2.

Uses MQTT, MQTTReadThread;

procedure TForm2.btnConnectClick(Sender: TObject);
MQTTClient.OnPublish := OnPublish;
MQTTClient.Connect ;

MQTTClient.Subscribe('123');
MQTTClient.Publish('123', 'abc');


type
//1"> //01"> 1CONNACK
, // Connect Acknowledgment
.........
);
TMQTTClient = class(TObject)
private
var
Data: TBytes;
RL: TRemainingLength;
VH: TBytes;
FH: Byte;
Payload: TUTF8Text;

if FSocket = nil then

begin

// Now to Connect the Socket and send the Data.

FSocket := TTCPBlockSocket.Create;
FSocket.Connect(Self.FHostname, IntToStr(Self.FPort));
FisConnected := True;
if SocketWrite(Data) then
begin
Result := True;
FReadThread := TMQTTReadThread.Create(@FSocket);
FReadThread.OnConnAck := Self.OnRTConnAck;
FReadThread.Resume;
end else Result := False;
end;



type
TMQTTMessage = Record
FixedHeader: Byte;
RL: TBytes;
Data: TBytes;
End;

TMQTTReadThread = class(TThread)

private
FPSocket: PTCPBlockSocket;
FCurrentData: TMQTTMessage;
// Events
FConnAckEvent: TConnAckEvent;
// This is our data processing and event firing command. To be called via Synchronize.
procedure HandleData;
protected
procedure Execute; override;
public
constructor Create(Socket: PTCPBlockSocket);
property OnConnAck : TConnAckEvent read FConnAckEvent write FConnAckEvent;
end;



begin
inherited Create(true);

FPSocket := Socket;
FreeOnTerminate := true;

var
CurrentMessage: TMQTTMessage;
RLInt: Integer;
Buffer: TBytes;
i: integer;
end;
then
begin

SetLength(CurrentMessage.Data, RLInt);

FPSocket^.RecvBufferEx(Pointer(CurrentMessage.Data), RLInt, 1000);

end;

Synchronize(HandleData);
end;

end;

end;


var
MessageType: Byte;

DataLen: integer;

QoS: integer;

Topic,Payload: string;

Topic,Payload :UTF8String;

ResponseVH: TBytes;

ConnectReturn: Integer;

i:integer;

//do something
end

else

if (MessageType = Ord(MQTT.PUBLISH)) then

begin

end;
<!--[if gte mso 9]><xml><br /> Normal 0 0 2 false false false EN-US ZH-TW X-NONE
</xml><![endif]--><!--[if gte mso 9]><xml><br />

</xml><![endif]--><!--[if gte mso 10]>
<style><br /> /* Style Definitions */
table.MsoNormalTable
{mso-style-name:表格內文;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
</style><br />
附加檔案:53354487c38d7_MQTT.zip
編輯記錄
mingking0947 重新編輯於 2014-03-28 17:45:20, 註解 無‧
mingking0947
一般會員


發表:30
回覆:32
積分:12
註冊:2005-07-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2014-04-02 14:45:15 IP:175.98.xxx.xxx 訂閱
{$IFDEF MSWINDOWS}
Result := Ord(FBuffer[1]);
{$ELSE}
Result := Ord(FBuffer[0]);
{$ENDIF}
delete(FBuffer, 1, 1);
end;
ExceptCheck;
end;
以上就能在windows,andorid及iOS上帶回正確的值.

系統時間:2024-04-26 18:54:28
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!