首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

IdUDPServer1与IdUDPClient1的运用

2013-07-04 
IdUDPServer1与IdUDPClient1的使用?怎么使用IdUDPServer1与IdUDPClient1,我想做一个udp的测试,服务器发送

IdUDPServer1与IdUDPClient1的使用?
怎么使用IdUDPServer1与IdUDPClient1,
我想做一个udp的测试,服务器发送字符串,客户端接收字符串,该怎么设置?
[解决办法]
http://blog.csdn.net/hawkzone/article/details/448525

[解决办法]
参考一下吧,使用MSWinsock组件
//---------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Test.h"
//---------------------------------------
#pragma package(smart_init)
#pragma link "MSWinsockLib_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    listenItemClick(NULL);
}
//---------------------------------------
void __fastcall TForm1::listenItemClick(TObject *Sender)
{
    listenItem->Checked=!listenItem->Checked;
    if(listenItem->Checked)
    {
        ClientSocket->Active=false;
        ServerSocket->Active=true;
        StatusBar1->Panels->Items[0]->Text="Listen...";
    }
    else
    {
        if(ServerSocket->Active)
            ServerSocket->Active=false;
        StatusBar1->Panels->Items[0]->Text="";
    }
}
//---------------------------------------
void __fastcall TForm1::connectItemClick(TObject *Sender)
{
    if(ClientSocket->Active)
        ClientSocket->Active=false;
    if(InputQuery("Computer to connect to","Address Name:",Server))


    {
        if(Server.Length()>0)
        {
            ClientSocket->Host=Server;
            ClientSocket->Active=true;
        }
    }
}
//---------------------------------------
void __fastcall TForm1::ClientSocketConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
    StatusBar1->Panels->Items[0]->Text="Connected to "+Socket->RemoteHost;
}
//---------------------------------------
void __fastcall TForm1::ServerSocketClientConnect(TObject *Sender,
      TCustomWinSocket *Socket)
{
    Memo2->Lines->Clear();
}
//---------------------------------------
void __fastcall TForm1::ServerSocketAccept(TObject *Sender,
      TCustomWinSocket *Socket)
{
    IsServer=true;
    StatusBar1->Panels->Items[0]->Text="Connected to "+Socket->RemoteHost;
}
//---------------------------------------

void __fastcall TForm1::Memo1KeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
    if(Key==VK_RETURN)
    {
        if(IsServer)
            ServerSocket->Socket->Connections[0]->SendText(
                Memo1->Lines->Strings[Memo1->Lines->Count-1]);
        else
            ClientSocket->Socket->SendText(
                Memo1->Lines->Strings[Memo1->Lines->Count-1]);
    }
}
//---------------------------------------
void __fastcall TForm1::ClientSocketRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
    Memo2->Lines->Add(Socket->ReceiveText());
}
//---------------------------------------


void __fastcall TForm1::ServerSocketClientRead(TObject *Sender,
      TCustomWinSocket *Socket)
{
    Memo2->Lines->Add(Socket->ReceiveText());
}
//---------------------------------------
void __fastcall TForm1::ClientSocketError(TObject *Sender,
      TCustomWinSocket *Socket, TErrorEvent ErrorEvent, int &ErrorCode)
{
    Memo2->Lines->Add("Error connecting to : "+Server);
    ErrorCode=0;
}
//---------------------------------------

热点排行