//server端
#include <stdio.h>
//#include <linux/in.h>#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <time.h>
#include <unistd.h>
#define UDP_PORT 3000
#define DEFAULT_PORT 3000
int passivesock(const char *service,const char *transport,int qlen)
{
int sock,ret,type,proto;
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
struct servent *sent = getservbyname(service,transport);
if(sent)
{
saddr.sin_port = sent->s_port;
printf("xxx");
}
else
{
saddr.sin_port = htons(DEFAULT_PORT);
}
saddr.sin_addr.s_addr = INADDR_ANY;
if(0 == strcmp(transport,"udp"))
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
struct protoent *pent = getprotobyname(transport);
proto = 0;
if(pent)
{
proto = pent->p_proto;
}
sock = socket(AF_INET,type,proto);
if(-1 == sock)
{
printf("error:%s",strerror(errno));
exit(-1);
}
ret = bind(sock,(sockaddr*)(&saddr),sizeof(sockaddr));
if(-1 == ret)
{
printf("error:%s",strerror(errno));
exit(-1);
}
if(type == SOCK_STREAM)
{
ret = listen(sock,qlen);
if(-1 == ret)
{
printf("error:%s",strerror(errno));
exit(-1);
}
}
printf("%s:%d\n",inet_ntoa(saddr.sin_addr),saddr.sin_port);
return sock;
}
/*
printf("fds limits:%d\n",getdtablesize());
struct sockaddr_in caddr;
socklen_t len = sizeof(caddr);
int sock = passivesock("time","tcp",5);
int usock = passivesock("time","udp",0);
char buf[100];
fd_set fds,rfds;
FD_ZERO(&fds);
FD_ZERO(&rfds);
FD_SET(sock,&fds);
FD_SET(usock,&fds);
int maxfd = sock > usock? sock : usock;
while(1)
{
rfds = fds;
int ret = select(maxfd+1,&rfds,NULL,NULL,NULL);
if(-1 == ret)
{
perror("error");
exit(-1);
}
printf("ret = %d\n",ret);
if(FD_ISSET(sock,&rfds))
{
int cfd = accept(sock,(sockaddr*)&caddr,&len);
if(-1 == cfd)
perror("error");
FD_SET(cfd,&fds);
if(maxfd < cfd)
maxfd = cfd;
}
else if(FD_ISSET(usock,&rfds))
{
printf("usock:%d:\n",usock);
ret = recvfrom(usock,buf,sizeof(buf),0,(sockaddr*)&caddr,&len);
if(ret)
{
if(write(STDOUT_FILENO,buf,ret) < 0)
{
perror("write:");
exit(-1);
}
}
else if(-1 == ret)
{
perror("recvfrom:");
exit(-1);
}
}
else
{
for(int fd = 0;fd <= maxfd;fd++)
{
if(fd == sock || fd == usock)
continue;
if(FD_ISSET(fd,&rfds))
{
printf("%d:\n",fd);
ret = recv(fd,buf,sizeof(buf),0);
if(-1 == ret)
perror("error");
else
write(STDOUT_FILENO,buf,ret);
close(fd);
FD_CLR(fd,&fds);
}
}
}
}
return 0;
*/
struct ServiceEnt
{
const char* service;
int useTcp;
int sock;
int (*func)(ServiceEnt*);
};
int tcpEcho(ServiceEnt *p)
{
struct sockaddr_in caddr;
socklen_t len = sizeof(caddr);
int cfd = accept(p->sock,(sockaddr*)&caddr,&len);
char buf[100];
int s = recv(cfd,buf,sizeof(buf),0);
if(s > 0)
{
if(send(cfd,buf,s,0) < 0)
{
perror("send:");
return -1;
}
}
else if(s < 0)
{
perror("tcpEcho:");
return s;
}
return 0;
}
int tcpTime(ServiceEnt* p)
{
time_t now;
struct sockaddr_in caddr;
socklen_t len = sizeof(caddr);
int cfd = accept(p->sock,(sockaddr*)&caddr,&len);
if(cfd < 0)
{
perror("bad cfd:");
return -1;
}
int pid = fork();
if(pid == -1)
{
perror("pid:");
exit(-1);
}
if(0 == pid)
{
time(&now);
int s = write(cfd,(char*)&now,sizeof(now));
if(s < 0)
{
perror("write:");
exit(-1);
}
printf("write %d of %d\n",s,sizeof(now));
close(cfd);
printf("tcpTime:%ld\n",now);
exit(0);
}
return 0;
}
ServiceEnt sents[]=
{
{"echo",1,-1,tcpEcho},
{"time",1,-1,tcpTime},
{NULL,0,-1,NULL}
};
#define MAX_FD 64
#define MAX(a,b) ((a)>(b)?(a):(b))
int main(int argc,char *argv[])
{
// ServiceEnt *fdent[MAX_FD];
fd_set fds,rfds;
FD_ZERO(&fds);
FD_ZERO(&rfds);
int maxfd = 0;
ServiceEnt *p = sents;
for(;p->service;p++)
{
if(p->useTcp)
p->sock = passivesock(p->service,"tcp",5);
else
p->sock = passivesock(p->service,"udp",0);
if(p->sock < 0)
{
perror("can't get socket");
exit(-1);
}
FD_SET(p->sock,&fds);
maxfd = MAX(p->sock,maxfd);
}
while(1)
{
rfds = fds;
int ret = select(maxfd+1,&rfds,0,0,0);
if(ret < 0)
{
perror("select:");
exit(-1);
}
else
{
printf("ret = %d\n",ret);
}
for(p = sents;p->service;p++)
{
if(FD_ISSET(p->sock,&rfds))
p->func(p);
}
}
return 0;
}
//client 端
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
int connectsock(const char *host,const char *service,const char* transport)
{
int sock;
struct sockaddr_in saddr ={0};
saddr.sin_family = AF_INET;
struct servent *ent = getservbyname(service,transport);
if(ent)
{
saddr.sin_port = ent->s_port;
}
else
{
if((saddr.sin_port = htons(atoi(service))) == 0)
{
printf("error!\n");
exit(-1);
}
printf("#%s=%d\n",service,saddr.sin_port);
}
struct hostent *hent = gethostbyname(host);
if(hent)
{
memcpy(&saddr.sin_addr,hent->h_addr,hent->h_length);
}
else
{
if((saddr.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
{
printf("error,can't get address from %s",host);
exit(-1);
}
printf("####\n");
}
int type;
struct protoent *pent = getprotobyname(transport);
if(pent)
{
if(0 == strcmp(transport,"udp"))
type = SOCK_DGRAM;
else
type = SOCK_STREAM;
sock = socket(AF_INET,type,pent->p_proto);
if(-1 == sock)
{
printf("error,socket() fail:%s",strerror(errno));
exit(-1);
}
printf("##proto:%d\n",pent->p_proto);
}
else
{
printf("can't get proto from:%s",transport);
exit(-1);
}
printf("%s:%d:%d\n",inet_ntoa(saddr.sin_addr),saddr.sin_port,pent->p_proto);
if(connect(sock,(sockaddr*)&saddr,sizeof(saddr)) == -1)
{
printf("can't connect:%s",strerror(errno));
exit(-1);
}
return sock;
}
int connectUDP(const char* host,const char* service)
{
return connectsock(host,service,"udp");
}
int connectTCP(const char* host,const char* service)
{
return connectsock(host,service,"tcp");
}
int main(int argc,char *argv[])
{
int sock = connectTCP("192.168.1.103","time");
time_t now;
int s = read(sock,(char*)&now,sizeof(now));
if(s > 0)
printf("%s\n",ctime(&now));
else if(s < 0)
{
perror("read:");
}
}