//unixserver.c
#include <stdarg.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/un.h>
void error(int status, int err,char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if(err)
fprintf(stderr,":%s(%d)/n",strerror(err),err);
if(status)
exit(status);
}
int server(char *pathname, char *protocol) {
struct sockaddr_un server;
int s,type;
const int on = 1;
if(strcmp(protocol,"tcp")==0)
type = SOCK_STREAM;
else
type = SOCK_DGRAM;
if((s=socket(AF_LOCAL, type, 0)) <= 0)
error(1, 0, "create socket failed!");
unlink(pathname);
bzero(&server,sizeof(server));
server.sun_family=AF_LOCAL;
strcpy(server.sun_path,pathname);
if(bind(s,(struct sockaddr *)&server,sizeof(server)))
error(1, errno, "bind failed!");
if((type==SOCK_STREAM)&&listen(s,5))
error(1, errno, "listen error!");
return s;
}
//tcpunixserver.c
#include <stdarg.h>
#include <stdio.h>
#include <signal.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/select.h>
#include <sys/un.h>
void sig_child() {
pid_t pid;
int status;
while((pid=waitpid(-1,&status,WNOHANG))>0)
printf("%d terminated!/n",pid);
// while(wait(&status)>0);
}
int main(int argc, char **argv) {
struct sockaddr_un remote;
char *pathname;
int s, c, len,fd;
pid_t pid;
fd_set rset,allset;
int client[1024];
int i,maxi,maxfd,nready;
char buff[1024];
if(argc < 2)
error(1,0,"parameters less than 2/n");
else if(argc == 2) {
pathname = argv[1];
}
s = server(pathname,"tcp");
signal(SIGCHLD,sig_child);
len = sizeof(struct sockaddr);
FD_ZERO(&allset);
FD_ZERO(&rset);
FD_SET(s,&allset);
maxi=0;
maxfd=s;
for(i=0;i<1024;i++)
client[i]=-1;
while(1) {
rset=allset;
if((nready=select(maxfd+1,&rset,NULL,NULL,NULL))<0)
error(1,0,"select error!!");
if(FD_ISSET(s,&rset)) {
if((c=accept(s,(struct sockaddr *)&remote,&len))<=0){
error(1,errno,"accept error!");
}
for(i=0; i<1024; i++) {
if(client[i] != -1)
continue;
else {
client[i]= c;
break;
}
}
if(i == 1024)
error(1,0,"too many clients!!");
FD_SET(c,&allset);
if(c>maxfd)
maxfd = c;
if(i > maxi)
maxi = i;
if(--nready==0)
continue;
}
for(i=0;i<= maxi;i++){
if((fd=client[i])==-1)
continue;
if(FD_ISSET(fd,&rset)){
if((len=recv(fd,buff,sizeof(buff),0))>0) {
buff[len]='/0';
send(fd, buff,strlen(buff), 0);
bzero(buff,sizeof(buff));
}
else {
close(fd);
FD_CLR(fd,&allset);
client[i]=-1;
}
if(--nready==0)
break;
}
}
}
close(s);
exit(0);
}
//unixudpserver.c
#include <stdarg.h>
#include <stdio.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/un.h>
void process_server(int s) {
struct sockaddr_un remote;
char buff[128];
int len,n;
while(1){
n=recvfrom(s,buff,sizeof(buff),0,(struct sockaddr *)&remote,&len);
//printf("%s,%s/n",buff,remote.sun_path);
buff[n]='/0';
sendto(s,buff,strlen(buff), 0, (struct sockaddr *)&remote,sizeof(remote));
}
}
int main(int argc, char **argv) {
struct sockaddr_un remote;
char *pathname;
int s, c, len;
if(argc < 2)
error(1,0,"parameters less than 2/n");
else if(argc == 2) {
pathname = argv[1];
}
s = server(pathname,"udp");
process_server(s);
close(s);
exit(0);
}