分类: C/C++
2008-04-23 10:40:29
#include <netinet/in.h>
#include <sys/socket.h>
#include <time.h>
#include <stdio.h>
#include <string.h> %20-%20Focus%20on%20ACE%20-%20C++博客.files/None.gif)
#define MAXLINE 100 %20-%20Focus%20on%20ACE%20-%20C++博客.files/None.gif)
int main(int argc, char** argv) %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedBlockStart.gif)
%20-%20Focus%20on%20ACE%20-%20C++博客.files/ContractedBlock.gif)
{
int listenfd,connfd;
struct sockaddr_in servaddr;
char buff[MAXLINE+1];
time_t ticks;
unsigned short port;
int flag=1,len=sizeof(int); %20-%20Focus%20on%20ACE%20-%20C++博客.files/InBlock.gif)
port=10013;
if( (listenfd=socket(AF_INET,SOCK_STREAM,0)) == -1) %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
{
perror("socket");
exit(1);
}
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(port);
if( setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &flag, len) == -
1) %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
{
perror("setsockopt");
exit(1);
}
if( bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr)) ==
-1) %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
{
perror("bind");
exit(1);
}
else
printf("bind call OK!\n");
if( listen(listenfd,5) == -1) %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
{
perror("listen");
exit(1);
}
for(;;) %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
{
if( (connfd=accept(listenfd,(struct sockaddr*)NULL,NULL)) == -1)%20-%20Focus%20on%20ACE%20-%20C++博客.files/InBlock.gif)
%20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
{
perror("accept");
exit(1);
} %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
if( fork() == 0)/**//*child process*/ %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
{ %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
close(listenfd);/**//*这句不能少,原因请大家想想就知道了。*/
ticks=time(NULL);
snprintf(buff,100,"%.24s\r\n",ctime(&ticks));
write(connfd,buff,strlen(buff));
close(connfd);
sleep(1);
execlp("f1-9d",NULL);
perror("execlp");
exit(1);
}
close(connfd); %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
exit(0);/**//* end parent*/
}
} %20-%20Focus%20on%20ACE%20-%20C++博客.files/None.gif)
#include <netinet/in.h>
#include <sys/socket.h>
#include <time.h>
#include <stdio.h>
#include <string.h> %20-%20Focus%20on%20ACE%20-%20C++博客.files/None.gif)
#define MAXLINE 100 %20-%20Focus%20on%20ACE%20-%20C++博客.files/None.gif)
int main(int argc, char** argv) %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedBlockStart.gif)
%20-%20Focus%20on%20ACE%20-%20C++博客.files/ContractedBlock.gif)
{
int fd1,fd2;
struct sockaddr_in servaddr1,servaddr2;
char buff[MAXLINE+1];
time_t ticks;
unsigned short port;
int flag=1,len=sizeof(int); %20-%20Focus%20on%20ACE%20-%20C++博客.files/InBlock.gif)
port=10013;
if( (fd1=socket(AF_INET,SOCK_STREAM,0)) == -1) %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
{
perror("socket");
exit(1);
}
if( (fd2=socket(AF_INET,SOCK_STREAM,0)) == -1) %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
{
perror("socket");
exit(1);
}
bzero(&servaddr1,sizeof(servaddr1));
bzero(&servaddr2,sizeof(servaddr2));
servaddr1.sin_family=AF_INET;
servaddr2.sin_family=AF_INET; %20-%20Focus%20on%20ACE%20-%20C++博客.files/InBlock.gif)
if( inet_pton(AF_INET, "127.0.0.1", &servaddr1.sin_addr) <= 0) %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
%20-%20Focus%20on%20ACE%20-%20C++博客.files/ContractedSubBlock.gif)
{
printf("inet_pton() call error:127.0.0.1\n");
exit(1);
}
if( inet_pton(AF_INET, "128.160.1.230", &servaddr2.sin_addr) <= 0) %20-%20Focus%20on%20ACE%20-%20C++博客.files/InBlock.gif)
%20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
%20-%20Focus%20on%20ACE%20-%20C++博客.files/ContractedSubBlock.gif)
{
printf("inet_pton() call error:128.160.1.230\n");
exit(1);
}
servaddr1.sin_port=htons(port);
servaddr2.sin_port=htons(port);
if( setsockopt(fd1, SOL_SOCKET, SO_REUSEADDR, &flag, len) == -1) %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
%20-%20Focus%20on%20ACE%20-%20C++博客.files/ContractedSubBlock.gif)
{
perror("setsockopt");
exit(1);
}
if( setsockopt(fd2, SOL_SOCKET, SO_REUSEADDR, &flag, len) == -1) %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
%20-%20Focus%20on%20ACE%20-%20C++博客.files/ContractedSubBlock.gif)
{
perror("setsockopt");
exit(1);
}
if( bind(fd1,(struct sockaddr*)&servaddr1,sizeof(servaddr1)) == -1)%20-%20Focus%20on%20ACE%20-%20C++博客.files/InBlock.gif)
%20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
%20-%20Focus%20on%20ACE%20-%20C++博客.files/ContractedSubBlock.gif)
{
perror("bind fd1");
exit(1);
}
if( bind(fd2,(struct sockaddr*)&servaddr2,sizeof(servaddr2)) == -1)%20-%20Focus%20on%20ACE%20-%20C++博客.files/InBlock.gif)
%20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
%20-%20Focus%20on%20ACE%20-%20C++博客.files/ContractedSubBlock.gif)
{
perror("bind fd2");
exit(1);
}
printf("bind fd1 and fd2 OK!\n"); %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
/**//*put other process here*/
getchar(); %20-%20Focus%20on%20ACE%20-%20C++博客.files/ExpandedSubBlockStart.gif)
exit(0);/**//* end */
} %20-%20Focus%20on%20ACE%20-%20C++博客.files/None.gif)