基于Linux的socket编程模板(7)
来源:未知 责任编辑:责任编辑 发表时间:2014-01-20 07:53 点击:次
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int add_new_tcp_process_thr( ServerEnv *envp )
{
pthread_t tcpThr;
if( pthread_create( &tcpThr,NULL,tcpServerThrFxn,envp ) )
{
printf("tcp thread create fail!\n");
return -1;
}
printf("tcp thread has been created!\n");
return 0;
}
int save_user_name( char * pUsername )
{
printf("ok,user name saved,username=%s\n",pUsername);
return 0;
}
int save_user_age( int age )
{
printf("ok,user age saved,userage=%d\n",age);
return 0;
}
void * tcpServerThrFxn( void * arg )
{
ServerEnv * envp = (ServerEnv *)arg;
int socketfd = envp->m_hSocket;
int returnBytes;
ServerPack sPack;
ReturnPack rPack;
memcpy(rPack.cmdHeader,TCP_CMD_HEADER_STR,TCP_CMD_HEADER_LEN);
while(1)
{
// read cmd from client
returnBytes = recv(socketfd,(uint8_t *)&sPack,SERVER_PACK_LEN,0);
if( returnBytes == SERVER_PACK_LEN )
{
//printf("ok,recv %d bytes! \n",SERVER_PACK_LEN);
}
else if( returnBytes <= 0 && errno != EINTR && errno != EWOULDBLOCK && errno != EAGAIN )
{
printf("disconnected or error occur! errno=%d\n ",errno);
break;
}
else
相关新闻>>
- 发表评论
-
- 最新评论 更多>>