Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2165377
  • 博文数量: 374
  • 博客积分: 7276
  • 博客等级: 少将
  • 技术积分: 5669
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-06 16:35
文章分类

全部博文(374)

文章存档

2013年(23)

2012年(153)

2011年(198)

分类: LINUX

2011-10-12 11:23:08

server:





#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define MAX_CLIENT 64

#define TRACE printf("\n\n%s @ %u\n\n",__FILE__,__LINE__);

const char buf[]="Welcome!";

int
main(int argc, char *argv[])
{
struct sockaddr_in myAddr; /*the server addr*/
struct sockaddr_in clientAddr; /*client addr*/
int iLen;
struct timeval tv;
int sockfd;
int acceptfd;
int z;
int loop=1;
int reuse_addr=1;
int wkfd; /*the working fd;*/
fd_set read_sock;
fd_set wk_sock; /*the current working socket set*/
int maxfds; /*the select max number of file describe*/
char buffer[256];

memset( &myAddr, 0, sizeof( struct sockaddr_in) );

sockfd = socket(PF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
perror("socket");
close(sockfd);
exit(-1);
}

setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&reuse_addr,sizeof(reuse_addr) );

if( argv[1] != NULL )
{
if( inet_aton(argv[1],&myAddr.sin_addr)< 0 )
{
perror("inet_aton");
exit(-1);
}
}else
{
myAddr.sin_addr.s_addr = INADDR_ANY;
}

if( argv[2] != NULL )
{
myAddr.sin_port = htons( atoi(argv[2]));
}else
{
myAddr.sin_port = htons(0);
}

if( bind(sockfd, (struct sockaddr *)&myAddr,sizeof( myAddr)) == -1 )
{
perror("bind");
close(sockfd);
exit(-1);
}

if( listen(sockfd, 10 ) < 0)
{
perror("listen");
close(sockfd);
exit(-1);
}

FD_ZERO(&read_sock);
FD_SET( sockfd, &read_sock); /*+s*/
maxfds=sockfd+1;
TRACE;
for(;;)
{
/*copy read_sock to wk_sock*/
FD_ZERO( &wk_sock );
for(z=0; z< maxfds; z++)
{
if( FD_ISSET(z, &read_sock ) )
FD_SET( z, &wk_sock);
}

/*set the over time 2.03 second*/
tv.tv_sec=2;
tv.tv_usec=30000;

TRACE;
wkfd = select(maxfds, &wk_sock,NULL, NULL, NULL);
TRACE;
if( wkfd == -1)
{
fprintf(stderr,"%s:select(2)\n",strerror(errno) );
exit(-1);
}else if( wkfd == 0)
{
printf("The time is come out");
continue;
}

/*detect if there a connecting request*/
if( FD_ISSET(sockfd, &wk_sock) )
{
/*wait for connect*/
TRACE;
iLen=sizeof( struct sockaddr);
acceptfd = accept(sockfd, (struct sockaddr *)&clientAddr, &iLen );
if(acceptfd < 0)
{
fprintf( stderr,"%s:accept",strerror(errno) );
exit(-1);
}

printf("accept:%d socket:%d\n",acceptfd,sockfd);

if(acceptfd > MAX_CLIENT)
{
printf("Max Client Number come out");
close(acceptfd);
continue;
}

if ( acceptfd +1 > maxfds )
maxfds = acceptfd+1;

FD_SET(acceptfd, &read_sock);//同一个client execute 两次
}

/*check the client */
for (acceptfd=0;acceptfd < maxfds; acceptfd++ )
{
printf("_________________loop %d\n",loop++);
if( acceptfd == sockfd )
{
TRACE;
continue;
}
TRACE;
if(FD_ISSET(acceptfd, &wk_sock) )
{
TRACE;
/*if(write(acceptfd, "Welcome",sizeof("Welcome"))< 0)
perror("write");
TRACE;*/
if( recv(acceptfd,buffer,sizeof(buffer),0) < 0 )
{
perror("recv");
FD_CLR(acceptfd, &read_sock);
}
buffer[strlen(buffer)]=0;
printf("%s",buffer);
bzero(buffer,sizeof(buffer));
FD_CLR(acceptfd, &read_sock);
}
}

/*if possible, reduce the maxfds*/
for( acceptfd = maxfds -1 ;acceptfd>=0 &&!FD_ISSET(acceptfd,&read_sock);
acceptfd = maxfds-1)
{
maxfds = acceptfd;
}
}

/*the control will never come here*/
printf("some thing error\n");
return 0;
}




client:




#include
#include
#include
#include
#include
#include
#include
#include
#include

int
main(int argc, char **argv)
{
int socketfd;
//char buf[256];
struct sockaddr_in *clientaddr=malloc(sizeof(struct sockaddr_in));
char *addr=NULL;

socketfd = socket(PF_INET, SOCK_STREAM, 0 );
if(socketfd < 0 )
{
perror("socketfd error");
exit(-1);
}

memset(clientaddr, 0,sizeof (struct sockaddr_in) );

clientaddr->sin_family=AF_INET;
if(argc == 2 )
{
inet_aton(argv[1], &(clientaddr->sin_addr) );
} else
{
addr="127.0.0.1";
clientaddr->sin_addr.s_addr=inet_addr("127.0.0.1");
clientaddr->sin_port=htons(9090);
}
clientaddr->sin_port=htons(9090);

if( connect( socketfd, ( struct sockaddr *)clientaddr,
sizeof( struct sockaddr) ) != -1 )
{
//int n;

/*if ( ( n=read(socketfd, buf, sizeof( buf) ) ) > 0 )
{
buf[n]=0;
printf(buf);
printf("\n");
bzero(buf,strlen(buf));
}

if ( fgets(buf,sizeof(buf),stdin) )
{
buf[ strlen(buf)-1 ]=0;
write(socketfd,buf,strlen(buf) );
}*/
if(write(socketfd,"Hello",sizeof("Hello")) )
{
while(1)
{}
write(stdout,"successful\r",12);
}

} else
{
perror("connect error");
}

while(1)
{
}
printf("connect closed!\n");
close(socketfd);
free(clientaddr);

exit(0);
}
阅读(589) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~