【发布时间】:2018-05-18 00:50:14
【问题描述】:
在 C++Builder 中,我必须发送一个 int 的多维数组,例如:
int example[3][3];
使用 TCP 协议。
我使用这个视频创建了一个套接字:
https://www.youtube.com/watch?v=UjrITeDk718
但我不明白如何只发送多维数组而不是字符串...有什么提示吗?
客户代码:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
IdTCPClient1->Connect();
//send byte
IdTCPClient1->WriteInteger(Edit1->Text.Length());
//send text
IdTCPClient1->Write(Edit1->Text);
//send request
TStringList *SL = new TStringList;
SL->Add(Edit1->Text);
IdTCPClient1 ->WriteStrings(SL);
delete SL;
ListBox1->Items->Add(Edit1->Text+">>sent");
int bytes = IdTCPClient1 -> ReadInteger();
AnsiString resp = IdTCPClient1->ReadString(bytes);
ListBox1->Items->Add(resp);
IdTCPClient1->Disconnect();
}
//---------------------------------------------------------------------------
服务器代码:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::IdTCPServer1Execute(TIdPeerThread *AThread)
{
int bytes = AThread->Connection->ReadInteger();
AnsiString request = AThread->Connection->ReadString(bytes);
ListBox1->Items->Add(request);
Edit1->Text=FormatDateTime("hh:mm AM/PM", Now());
AnsiString risp = Edit1->Text;
AThread->Connection->WriteInteger(risp.Length());
TStringList *SL = new TStringList;
SL->Add(risp);
AThread->Connection->WriteStrings(SL);
delete SL;
ListBox1->Items->Add(risp+">> inviato");
AThread->Connection->Disconnect();
}
//---------------------------------------------------------------------------
【问题讨论】:
-
您还没有显示所有代码。
IdTCPClient1是什么?请参阅 minimal reproducible example 并考虑创建一个来改进您的问题。 -
它只是C++ Builder的一个组件,如果您从Indy Clients中选择TCP Client,IdTCPClient1是标准名称... :)
-
了解,但忽略这一点会使没有 C++ Builder 并且没有大量额外时间观看 YouTube 视频的人(例如我自己)难以或不可能帮助诊断您的问题。
-
@BlueFab 仅供参考,您的代码使用的是 Indy 9,它已经过时了十多年。请考虑升级到 Indy 10。
标签: c++ tcp c++builder