/************************************************************************/
// Copyright 2015, han_gangbiao. All rights reserved.
//
// [Time]: 2015-7-1 20:23:54
// [Author]: han_gangbiao
// [Info]: TCP-Client Demo
/************************************************************************/
#include "stdafx.h"
#include "xsmart.h"
#include "xnet/SockConnector.h"
#include "xbase/xlogger.h"
#include "xbase/xthread.h"
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
smart::XSmartLib::init();
XLOG_SET_STDERR(true);
std::cout<<smart::XSmartLib::getLibInfo()<<std::endl;
const char* ip = "127.0.0.1";
uint16 port = 8000;
XNetAddr remote(ip, port);
XSockConnector connector;
XSockStream sock_cli;
int ret = connector.connect(sock_cli, remote);
if(ret < 0)
{
XLOG_ERROR("connect server[%s:%d] failed.", ip, port);
return -1;
}
XLOG_INFO("connect success. remote[%s:%d]", remote.get_ipstr(), remote.get_port());
while(1) {
std::string str = "hello world";
sock_cli.send(str.c_str(), str.length());
SLEEP_MS(100);
char buffer[4096] = {0};
int bytes_receieved = sock_cli.recv(buffer, sizeof(buffer));
buffer[bytes_received] = '\0';
XLOG_INFO("reply data: %s", buffer);
}
sock_cli.close();
smart::XSmartLib.unInit();
return 0;
}
转载于:https://my.oschina.net/jackhen/blog/473082