linux socket耗尽,linux下的socket是一直保持着链接的么-

linux下的socket是一直保持着链接的么-

(2012-04-10 02:19:23)

标签:

linux

杂谈

linux下的socket是一直保持着链接的么?我的代码如下,写的一个简单的访问telnet服务器的代码

使用socket在windows下的运行良好

我使用该框架,修改了一下,可在recv的时候

总是返回0或者-1

不知道怎么回事,是不是我对linux下的socket编程模式使用的不对

extern boost::thread g_tNionClient;

extern int g_socketNionClient;

extern bool g_bNionClientConnected;

extern string g_strNionServerIP;

extern long g_lNionPort;

extern string g_strNionClientUserName;

extern string g_strNionClientPassWord;

extern string g_strNionAirportName ;

extern long g_lNionReconnectTime;

extern long g_lNionSockTimeOut;

bool StartHYENionClient()

{

WriteFileLogInfo_Nion("create

RunHYENionClient_THREAD ....\t");

g_tNionClient = boost::thread(

&Thread_RunHYENionClient );

WriteFileLogInfo_Nion("OK");

return true;

}

//这里是boost的一个线程,该线程持续从服务器recv数据

int Thread_RunHYENionClient()

{

bool bFirst = true;

while(1)

{

if (http://www.erobc.com/!bFirst)

{

WriteFileLogInfo_Nion("NION Client: NION Server

closed ,just reconnect after 2 sec..");

boost::this_thread::sleep(boost::posix_time::seconds(2));

bFirst = false;

}

if (!g_bNionClientConnected)

{

shutdown(g_socketNionClient,SHUT_RDWR);

close(g_socketNionClient);

CreatesocketConnect();

bFirst = false;

}

string strBuffer;

//string strDaqing = "statusIs running

\"DaQingAirport\"

";

string strDaqing = "statusIs running

\"";

strDaqing += g_strNionAirportName;

strDaqing += "\"

";

while(g_bNionClientConnected )

{

char temp;

int ret;

ret = recv(g_socketNionClient,&temp,1,0);

if (ret == 0) //服务器将socket关闭

{

close(g_socketNionClient);

g_bNionClientConnected = false;

string strlog;

strlog = "NION Client:***** socket client closed

gracefully ,remove this socket";

string str;

strlog += Inter2String(str,g_socketNionClient);

WriteFileLogInfo_Nion(strlog.c_str());

break;

}

else if (ret == -1)//出现错误,需要重连

{

g_bNionClientConnected = false;

close(g_socketNionClient);

string strlog = "NION Client: recv error! remove

this socket";

WriteFileLogInfo_Nion(strlog.c_str());

break;;

}

else//接收的正确数据

{

cout<

if (temp != '\n')

{

strBuffer += temp;

}

else

{

if (strBuffer.find(strDaqing) >= 0)

{

ClearNionServerChannel();//;connect to nion and clear nion

}

strBuffer = "";

}

}

continue;

}

}

return 1;

}

bool ClearNionServerChannel()

{

return false;

}

bool CreatesocketConnect()

{

connectagain:

if ((g_socketNionClient = socket(AF_INET,SOCK_STREAM,0)) ==

-1)

{

return false;

}

struct sockaddr_in Remote;

bzero(&Remote, sizeof(Remote));

Remote.sin_family = AF_INET;

Remote.sin_port = htons((u_short)g_lNionPort);

Remote.sin_addr.s_addr =

inet_addr(g_strNionServerIP.c_str());

//inet_pton(AF_INET, g_strNionServerIP.c_str(),

&Remote.sin_addr);

int ret = connect(g_socketNionClient,(struct

sockaddr*)&Remote,sizeof(Remote));

if(ret == -1)

{

WriteFileLogInfo_Nion("NION client: TRYING TO

RECONNECT TO NION Server...");

boost::this_thread::sleep(boost::posix_time::seconds(g_lNionReconnectTime));

goto connectagain;

}

if(!LoginServer(g_strNionClientUserName,

g_strNionClientPassWord))

{

WriteFileLogInfo_Nion("NION client: login on the

NION server failured!");

close(g_socketNionClient);

g_bNionClientConnected = false;

goto connectagain;

}

struct timeval timeout = {g_lNionSockTimeOut+10,0};

cout<

SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout,

sizeof(timeout));

cout<

SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout,

sizeof(timeout));

WriteFileLogInfo_Nion("NION Client: Connect to the

NION server successfully!");

g_bNionClientConnected = true;

return true;

}

bool LoginServer(const string& user,const

string& password)

{

char connStr;

strcpy(connStr, user.c_str());

strcat(connStr, "\r");

strcat(connStr, password.c_str());

strcat(connStr, "\r\n");

if (g_socket

分享:

a4c26d1e5885305701be709a3d33442f.png喜欢

0

a4c26d1e5885305701be709a3d33442f.png赠金笔

加载中,请稍候......

评论加载中,请稍候...

发评论

登录名: 密码: 找回密码 注册记住登录状态

昵   称:

评论并转载此博文

a4c26d1e5885305701be709a3d33442f.png

发评论

以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值