最近開始學(xué)習(xí)ICE,頭有點(diǎn)大,900多頁(yè)的文檔看了五百多頁(yè)還不知CLIENT如何定位SERVER的位置,郁悶的很...昨天毛了直接看文檔的最后幾頁(yè)..我暈原來(lái)在這里給闡述了...差不多想撞墻
以下是中文文檔中關(guān)于端點(diǎn)的描述:
D.2 端點(diǎn)
綱要
endpoint : endpoint
描述
端點(diǎn)列表由一個(gè)或多個(gè)用冒號(hào)(:) 分隔的端點(diǎn)組成。端點(diǎn)的格式如下所
示: protocol option。所支持的協(xié)議有tcp、udp、ssl,以及
default。如果使用了default,它會(huì)被Ice.Default.Protocol 屬性的值替
代。如果端點(diǎn)的格式有問題,或者指定了未知的協(xié)議,應(yīng)用會(huì)收到
Ice::EndpointParseException。
只有安裝了IceSSL 插件,才能使用ssl 協(xié)議。
各個(gè)協(xié)議及其所支持的選項(xiàng)將在下面描述。
TCP 端點(diǎn)
綱要
tcp -h host -p port -t timeout -z
描述
tcp 端點(diǎn)支持以下選項(xiàng):
選項(xiàng)描述客戶語(yǔ)義服務(wù)器語(yǔ)義
-h host 指定端點(diǎn)的主機(jī)名
或IP 地址。如果
沒有指定,將使用
Ice.Default.Hos
t 的值。
確定要連接到的主
機(jī)名或IP 地址。
確定對(duì)象適配器用
于偵聽連接的網(wǎng)絡(luò)
接口,以及在適配
器所創(chuàng)建的代理中
向外公布的主機(jī)
名。
-p port 指定端點(diǎn)的端口
號(hào)。
確定要連接到的端
口( 必須指定)。
如果沒有指定這個(gè)
選項(xiàng),或是port 為
零,端口將由操作
系統(tǒng)選擇。
932 代理與端點(diǎn)
以上是基于TCP協(xié)議的,關(guān)于UDP,SSL協(xié)議的也差不多
我在我的聊天程序中連接LAN中一臺(tái)服務(wù)器的代碼:
#include <Ice/Ice.h>
#include <Printer.h>
using namespace std;
using namespace Demo;
int main(int argc, char * argv[])
{
int status = 0;
char strtemp[100];//聊天內(nèi)容
char clientname[20];//客戶名
char chattmp[130];
Ice::CommunicatorPtr ic;
try {
ic = Ice::initialize(argc, argv);
/*連接服務(wù)器:SimplePrinter16所申請(qǐng)的代理ID,default即為使用默認(rèn)的協(xié)議TCP,-h 192.168.1.16 為服務(wù)端所在LAN中的IP地址,-p 9600為端口號(hào)*/
Ice::ObjectPrx base = ic->stringToProxy("SimplePrinter16:default -h 192.168.1.16 -p 9600");
//創(chuàng)建一個(gè)Printer的代理(Printer即為客戶端與服務(wù)器端的接口),利用checkedCast(base)檢查代理是否存在
PrinterPrx printer = PrinterPrx::checkedCast(base);
if (!printer)
{
throw "Invalid proxy";//代理不存在
}
else
{
printf("請(qǐng)輸入您的ID:");
cin >> clientname;
}
while ( 1 )
{
cin >> strtemp;//輸入聊天內(nèi)容
if ( strcmp( strtemp,"q") == 0 )
{
break;//退出聊天
}
//strcat( chattmp, clientname );
strcpy( chattmp, clientname );
strcat( chattmp, " say: " );
strcat( chattmp, strtemp );
printer->printString(chattmp);
}
//發(fā)給服務(wù)器的退出消息
printer->printString(clientname);
printer->printString("已退出!");
cout << "成功退出!" << endl;
} catch (const Ice::Exception & ex) {
cerr << ex << endl;
status = 1;
} catch (const char * msg) {
cerr << msg << endl;
status = 1;
}
if (ic) {
try {
ic->destroy();
} catch (const Ice::Exception & ex) {
cerr << ex << endl;
status = 1;
}
}
return status;
}