add one sample for testing connect with same port in one machine.

This commit is contained in:
zhengshuxin 2019-08-14 10:56:27 +08:00
parent 46b4bcd63c
commit 3be918e8c1
5 changed files with 146 additions and 8 deletions

View File

@ -6,13 +6,11 @@ static void test(const char* addr)
{
std::list<ACL_VSTREAM*> conns;
for (int i = 0; i < 1; i++)
{
for (int i = 0; i < 1; i++) {
ACL_VSTREAM* client = acl_vstream_connect(addr, ACL_BLOCKING, 10, 10, 4096);
if (client == NULL)
if (client == NULL) {
printf("connect addr: %s error\r\n", addr);
else
{
} else {
printf("connect addr: %s ok, i: %d\r\n", addr, i);
conns.push_back(client);
}
@ -22,8 +20,9 @@ static void test(const char* addr)
getchar();
std::list<ACL_VSTREAM*>::iterator it = conns.begin();
for (; it != conns.end(); ++it)
for (; it != conns.end(); ++it) {
acl_vstream_close(*it);
}
printf("Exit now ok\r\n");
}
@ -75,8 +74,7 @@ int main(int argc, char *argv[])
printf("connecting %s ...\n", argv[1]);
//acl_poll_prefered(1);
for (int i = 0; i < 1; i++)
{
for (int i = 0; i < 1; i++) {
client = acl_vstream_connect(addr, ACL_BLOCKING, 10, 10, 4096);
if (client == NULL) {
printf("connect %s error(%s)\n", addr, acl_last_serror());

View File

@ -0,0 +1,2 @@
include ../Makefile.in
PROG = connect

View File

@ -0,0 +1,112 @@
#include "stdafx.h"
#include <unistd.h>
#include <getopt.h>
class client : public acl::thread
{
public:
client(const char* addr) : addr_(addr) {}
~client(void) {}
protected:
void* run(void)
{
acl::socket_stream conn;
if (!conn.open(addr_, 10, 10)) {
printf("%ld: connect %s error %s\r\n",
acl::thread::self(), addr_.c_str(),
acl::last_serror());
return NULL;
}
(void) conn.set_tcp_solinger(true, 0);
printf("connect %s ok, my addr=%s\r\n",
addr_.c_str(), conn.get_local(true));
for (int i = 0; i < 10; i++) {
if (conn.format("thread-%ld: hello world\r\n",
acl::thread::self()) == -1) {
printf("write to %s error %s\r\n",
addr_.c_str(), acl::last_serror());
break;
}
acl::string buf;
if (!conn.gets(buf)) {
printf("gets from %s error %s\r\n",
addr_.c_str(), acl::last_serror());
break;
} else {
printf("%ld: %s\r\n", acl::thread::self(),
buf.c_str());
}
}
printf("sleep 2 seconds\r\n");
sleep(2);
printf("disconnect from %s now\r\n", addr_.c_str());
return NULL;
}
private:
acl::string addr_;
};
static void add_servers(std::vector<acl::string>& servers, const char* s)
{
acl::string buf(s);
const std::vector<acl::string>& tokens = buf.split2(",; \t");
for (std::vector<acl::string>::const_iterator cit = tokens.begin();
cit != tokens.end(); ++cit) {
servers.push_back(*cit);
}
}
static void usage(const char* procname)
{
printf("usage: %s -h [help] -s server_list\r\n", procname);
}
int main(int argc, char* argv[])
{
std::vector<acl::string> addrs;
int ch;
while ((ch = getopt(argc, argv, "hs:")) > 0) {
switch (ch) {
case 'h':
usage(argv[0]);
return 0;
case 's':
add_servers(addrs, optarg);
break;
default:
break;
}
}
if (addrs.empty()) {
usage(argv[0]);
return 0;
}
std::vector<acl::thread*> threads;
for (std::vector<acl::string>::const_iterator cit = addrs.begin();
cit != addrs.end(); ++cit) {
acl::thread* thr = new client(*cit);
threads.push_back(thr);
thr->start();
}
for (std::vector<acl::thread*>::iterator it = threads.begin();
it != threads.end(); ++it) {
(*it)->wait();
delete *it;
}
return 0;
}

View File

@ -0,0 +1,8 @@
// stdafx.cpp : 只包括标准包含文件的源文件
// master_threads.pch 将成为预编译头
// stdafx.obj 将包含预编译类型信息
#include "stdafx.h"
// TODO: 在 STDAFX.H 中
//引用任何所需的附加头文件,而不是在此文件中引用

View File

@ -0,0 +1,18 @@
// stdafx.h : 标准系统包含文件的包含文件,
// 或是常用但不常更改的项目特定的包含文件
//
#pragma once
//#include <iostream>
//#include <tchar.h>
// TODO: 在此处引用程序要求的附加头文件
#include "acl_cpp/lib_acl.hpp"
#ifdef WIN32
#define snprintf _snprintf
#endif