月度归档:2009年11月

常见MIME

常见的MIME类型如:
超文本标记语言文本 .html,.html text/html
普通文本 .txt text/plain
RTF文本 .rtf application/rtf
GIF图形 .gif image/gif
JPEG图形 .ipeg,.jpg image/jpeg
au声音文件 .au audio/basic
MIDI音乐文件 mid,.midi audio/midi,audio/x-midi
RealAudio音乐文件 .ra, .ram audio/x-pn-realaudio
MPEG文件 .mpg,.mpeg video/mpeg
AVI文件 .avi video/x-msvideo
GZIP文件 .gz application/x-gzip
TAR文件 .tar application/x-tar
bzip2文件 .bz2 application/x-bzip2

自己写的一个linux 远程控制工具

学习linux有些时日了,这个就是我的一第一真正的作品,发表一下作品,
希望其中的一些技巧对朋友们有所帮助,也希望达人指正一下其中的不足。

主要应用知识:
linux的网络通信
文件的属性及操作
多进程的应用
进程的回收
信号的应用

下面是客户端
文件的意义:
command.c 命令模块 ,common.h 公共声明, input.c 分割命令, main.c 主程序 ,
Makefile make工具用的,ssjio.c ssjio.h 是我自己的模块

main.c

#include "common.h"

int main(void)
{
char cline[COMMAND_LINE]="123";
struct command_line command;
int sock_fd;
struct sockaddr_in sin;

printf("ssj-soft$: ");
fflush(stdout);

while(fgets(cline,COMMAND_LINE,stdin)!=NULL)
{ //printf("%x,%x,%x,%xn",cline[0],cline[1],cline[2],cline[3]);fflush(stdout);
if(cline[0]==0xa){cline[1]=0xa;cline[2]='';}
if(split(&command,cline)==-1)
exit(1);

if(strcasecmp(command.name,"get")==0){
if(do_get(command.argv[1],command.argv[2],sock_fd)==-1)
exit(1);
}else if(strcasecmp(command.name,"put")==0){
if(do_put(command.argv[1],command.argv[2],sock_fd)==-1)
exit(1);
}else if(strcasecmp(command.name,"cd")==0){
if(do_cd(command.argv[1])==-1)
exit(1);
}else if(strcasecmp(command.name,"!cd")==0){
if(do_serv_cd(command.argv[1],sock_fd)==-1)
exit(1);
}else if(strcasecmp(command.name,"ls")==0){
if(do_ls(command.argv[1])==-1)
exit(1);
}else if(strcasecmp(command.name,"!ls")==0){
if(do_serv_ls(command.argv[1],sock_fd)==-1)
exit(1);
}else if(strcasecmp(command.name,"connect")==0){
if(do_connect(command.argv[1],&sin,&sock_fd)==-1)
exit(1);
}else if(strcasecmp(command.name,"RUN")==0){
if(do_serv_command(command.argv[1],sock_fd)==-1)
exit(1);
}else if(strcasecmp(command.name,"bye")==0){
if(do_bye(sock_fd)==-1)
exit(1);
break;
}else{
printf("wrong commandn");
printf("usage:connect <ip>n");
printf(" run <command>n");
printf(" cd <path>n");
printf(" !cd <path>n");
printf(" get <srcpathfile> <dstpath>n");
printf(" put <srcpathfile> <dstpath>n");
printf(" ls <path>n");
printf(" !ls <path>n");
printf(" byen");
}
printf("ssj-soft$: ");
fflush(stdout);
}
if(close(sock_fd)==-1)
{perror("Fail to close");exit(1);}

return 0;
}

command.c

#include "common.h"

//================connect==================
int do_connect(char *ip,struct sockaddr_in *sin,int *sock_fd)
{
int sfd;

bzero(sin,sizeof(struct sockaddr_in));
sin->sin_family=AF_INET;
if(inet_pton(AF_INET,ip,&sin->sin_addr)==-1)
{perror("wrong format of ip address");return -1;}

sin->sin_port=htons(PORT);
if((sfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{perror("fail to create socket");return -1;}

if(connect(sfd,(struct sockaddr *)sin,sizeof(struct sockaddr_in))==-1)
{perror("Fail to connetc");return -1;}

*sock_fd=sfd;
return 0;
}

//=====================get_file=======================
int do_get(const char *src,const char *dst,int sock_fd)
{
char *dst_file,*p,buf[MAX_LINE];
struct stat statbuf;
int n,fd,len,res=-1;

if(src==NULL || dst==NULL)
{printf("wrong commandn");return -1;}

if (src[strlen(src)-1]=='/')
{printf("source file should be a regular filen");return -1;}

if((dst_file=(char *)malloc(strlen(dst)+strlen(src)))==NULL)
{perror("Fail to malloc");return -1;}

strcpy(dst_file,dst);

if(dst_file[strlen(dst_file)-1]!='/')
strcat(dst_file,"/");
p=rindex(src,'/');
strcat(dst_file,p+1);

if((fd=open(dst_file,O_WRONLY|O_CREAT|O_TRUNC,0664))==-1)
{perror("Fail to open dst_file");goto end2;}

if(fstat(fd,&statbuf)==-1)
{perror("Fail to stat dst_file");goto end1;}

if(!S_ISREG(statbuf.st_mode))
{printf("dst_file should be a regular file");goto end1;}

sprintf(buf,"GET %s",src);

if(my_write(sock_fd,buf,strlen(buf)+1)==-1)
goto end1;

if((n=my_read(sock_fd,buf,MAX_LINE))<=0)
goto end1;

if(buf[0]=='E')
{write(STDOUT_FILENO,buf,n);res=0;goto end1;}

len=atoi(&buf[3]);
printf("len atoi:%dn",len);//no
if(my_write(sock_fd,"RDY",4)==-1)
{perror("Fail to write RDY");goto end1;}
printf("while……n");fflush(stdout);
while(1){
if(len!=0)
n=my_read(sock_fd,buf,MAX_LINE);
printf("read up len:%d,n:%d",len,n);fflush(stdout);
if(n>0)
{write(fd,buf,n);printf("len:%d,n:%d",len,n);fflush(stdout);len-=n;n=0;}
else if(len==0)
{printf("OKn");break;}
else
goto end1;
}
res=0;

end1:
close(fd);
end2:
free(dst_file);
return res;
}

//=====================!cd=======================
int do_serv_cd(char *path,int sock_fd)
{
char buf[MAX_LINE];
int n;
sprintf(buf,"CD %s",path);

if(my_write(sock_fd,buf,strlen(buf)+1)==-1)
{return -1;}

if((n=my_read(sock_fd,buf,MAX_LINE))<=0)
{return -1;}

if(buf[0]=='E')
write(STDOUT_FILENO,buf,n);

return 0;
}

//==================!ls=======================
int do_serv_ls(char *path,int sock_fd)
{
char buf[MAX_LINE];
int len,n;

sprintf(buf,"LS %s",path);

if(my_write(sock_fd,buf,strlen(buf)+1)==-1)
return -1;

if((n=my_read(sock_fd,buf,MAX_LINE))<=0)
return -1;

if(buf[0]=='E')
{write(STDOUT_FILENO,buf,n);return 0;}

len=atoi(&buf[3]);

if(my_write(sock_fd,"RDY",4)==-1)
return -1;

while(1)
{
if(len!=0)
n=my_read(sock_fd,buf,MAX_LINE);
if(n>0)
{write(STDOUT_FILENO,buf,n);len-=n;n=0;}
else if(len==0)
{printf("OKn");break;}
else
return -1;
}
return 0;
}

//================bye======================
int do_bye(int sock_fd)
{
if(my_write(sock_fd,"BYE",4)==-1)
return -1;

return 0;
}

//===============put=====================
int do_put(const char *src,const char *dst,int sock_fd)
{
char *dst_file,buf[MAX_LINE],*p;
struct stat statbuf;
int n,fd,res=-1;

if(src==NULL || dst==NULL)
{printf("wrong commandn");return -1;}

if(src[strlen(src)-1]=='/')
{printf("source file should be a regular filen");return -1;}

if((dst_file=malloc(strlen(src)+strlen(dst)+2))==NULL)
{perror("Fail to malloc");return -1;}

strcpy(dst_file,dst);
if(dst_file[strlen(dst_file)-1]!='/')
strcat(dst_file,"/");
p=rindex(src,'/');

strcat(dst_file,p+1);

if((fd=open(src,O_RDONLY))==-1)
{perror("Fail to open src_file");goto end1;}

if(fstat(fd,&statbuf)==-1)
{perror("Fail to stat src_file");goto end2;}
if(!S_ISREG(statbuf.st_mode))
{fputs("src_file should be a regular filen",stderr);goto end2;}

sprintf(buf,"PUT %d %s",statbuf.st_size,dst_file);
if(my_write(sock_fd,buf,strlen(buf)+1)==-1)
{goto end2;}
if((n=my_read(sock_fd,buf,MAX_LINE))<=0)
goto end2;
if(buf[0]=='E')
{write(STDOUT_FILENO,buf,n);goto end2;}

while(1)
{
n=read(fd,buf,MAX_LINE);

if(n>0)
{if(my_write(sock_fd,buf,n)==-1)
goto end2;}
else if(n==0)
{printf("OKn");break;}
else
{perror("Fail to readn");goto end2;}
}
res=0;
end2:
close(fd);
end1:
free(dst_file);
return res;
}

//===================cd=================
int do_cd(char *path)
{
if(chdir(path)==-1)
{perror("Fail to change directoryn");return -1;}

return 0;
}

//===================ls=================
int do_ls(char *path)
{
char cmd[128],buf[NAME_LEN];
FILE *fp;

sprintf(cmd,"ls %s > temp.txt",path);
system(cmd);

fp=fopen("temp.txt","r");
if(fp==NULL)
{perror("Fail to ls");return -1;}

while(fgets(buf,NAME_LEN,fp)!=NULL)
printf("%s",buf);

fclose(fp);
return 0;
}

//================command======================
int do_serv_command(char *path,int sock_fd)
{
char buf[MAX_LINE];
int len,n;

sprintf(buf,"RUN %s",path);

if(my_write(sock_fd,buf,strlen(buf)+1)==-1)
return -1;

if((n=my_read(sock_fd,buf,MAX_LINE))<=0)
return -1;

if(buf[0]=='E')
{write(STDOUT_FILENO,buf,n);return 0;}

len=atoi(&buf[3]);

if(my_write(sock_fd,"RDY",4)==-1)
return -1;

while(1)
{
if(len!=0)
n=my_read(sock_fd,buf,MAX_LINE);
if(n>0)
{write(STDOUT_FILENO,buf,n);len-=n;n=0;}
else if(len==0)
{printf("OKn");break;}
else
return -1;
}
return 0;
}

input.c

#include "common.h"
//去除命令之前的空格等
void del_blank(int *pos,char cline[])
{
while(*(cline+*pos)!=''&&(*(cline+*pos)==' '||*(cline+*pos)=='t'))
{(*pos)++;}
}

//得到参数部分,写入结构体
void get_arg(char arg[],int *pos,char cline[])
{
int i=0;
while(*(cline+*pos)!='' && *(cline+*pos)!=' ' && *(cline+*pos)!='t')
{*(arg+i)=*(cline+*pos);i++;(*pos)++;}
*(arg+i)='';
}

int split(struct command_line *command,char cline[])
{
int i=0,pos=0;
cline[strlen(cline)-1]=''; //fgets得到的字符串中有一个n,把它变成

del_blank(&pos,cline);
i=0;
while(cline[pos]!='')
{
if((command->argv[i]=(char *)malloc(MAX_LENGTH))==NULL)
{perror("Fail malloc");exit(-1);}
get_arg(command->argv[i],&pos,cline);
i++;
del_blank(&pos,cline);
}

if(strncasecmp(command->argv[0],"RUN",3)==0)
{strcpy(command->argv[1],cline+4);
i=2;}
command->argv[i]=NULL;
command->name=command->argv[0];
return i;
}

服务器端:

main.c

#include "common.h"
static void handler(int signo)
{
pid_t pid;
pid = waitpid(-1,NULL, WNOHANG);
printf("process id:%d closedn",pid);
}

int main(void)
{
signal(SIGCHLD,handler); //子程序退出的信号
//signal(SIGINT,handler); //CTRL-C的信号
//signal(SIGTERM,handler);

struct sockaddr_in sin;
struct sockaddr_in cin;
int lfd, cfd;
socklen_t len = sizeof(struct sockaddr_in);
char buf[MAX_LINE];
char str[ADDR_LEN];
int sock_opt = 1;
int n;
pid_t pid;

if(init(&sin, &lfd, sock_opt) == -1)
exit(1);

printf("waiting connections …n");

while(1){
if((cfd = accept(lfd, (struct sockaddr *)&cin, &len)) == -1){
perror("fail to accept");
exit(1);
}

if( (pid = fork()) < 0){
perror("fail to fork");
exit(1);
}else if(pid == 0){
close(lfd);

while(1){
if(my_read(cfd, buf, MAX_LINE) == -1)
exit(1);

if(strstr(buf, "GET") == buf)
{if(do_put(cfd, &buf[4]) == -1)
printf("error occours while puttingn");
else
printf("GET successful!n");
}
else if(strstr(buf, "PUT") == buf)
{if(do_get(cfd, &buf[4]) == -1)
printf("error occours while gettingn");}
else if(strstr(buf, "CD") == buf)
{if(do_cd(cfd, &buf[3]) == -1)
printf("error occours while changing directoryn");}
else if(strstr(buf, "LS") == buf)
{if(do_ls(cfd, &buf[3]) == -1)
printf("error occours while listingn");else printf("LS OKn");}
else if(strstr(buf, "RUN") == buf)
{if(do_command(cfd, &buf[4]) == -1)
printf("error occours while listingn");else printf("RUN OKn");}
else if(strstr(buf, "BYE") == buf)
break;
else{
printf("wrong commandn");
exit(1);
}
}

close(cfd);
exit(0);
}else
close(cfd);
}
return 0;
}

command.c

#include "common.h"

//================socket,bind,listen======================
int init(struct sockaddr_in *sin,int *lfd,int sock_opt)
{
int tfd;
bzero(sin,sizeof(struct sockaddr_in));
sin->sin_family=AF_INET;
sin->sin_addr.s_addr=INADDR_ANY;
sin->sin_port=htons(PORT);

if((tfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{perror("Fail to creat socket");return -1;}

setsockopt(tfd,SOL_SOCKET,SO_REUSEADDR,&sock_opt,sizeof(int));

if(bind(tfd,(struct sockaddr *)sin,sizeof(struct sockaddr_in))==-1)
{perror("Fail to bind");return -1;}

if(listen(tfd,20)==-1)
{perror("Fail to listen");return -1;}

*lfd=tfd;
return 0;
}

//=====================put==========================
int do_put(int cfd,char *file)
{
struct stat statbuf;
int n,fd,res=-1;
char buf[MAX_LINE];

if((fd=open(file,O_RDONLY))==-1)
{my_write(cfd,"ERR open server filen",22);return res;}

if(fstat(fd,&statbuf)==-1)
{my_write(cfd,"ERR stat server filen",22);goto end;}
if(!S_ISREG(statbuf.st_mode))
{if(my_write(cfd,"ERR server path should be a regular filen",42)==-1)
goto end;
res=0;
goto end;
}

sprintf(buf,"OK %d",statbuf.st_size);
if(my_write(cfd,buf,strlen(buf)+1)==1)
goto end;
if(my_read(cfd,buf,MAX_LINE)<=0)
goto end;

while(1)
{
n=read(fd,buf,MAX_LINE);
printf("n:%d – ",n);
if(n>0)
{if((my_write(cfd,buf,n))==-1)
goto end;}
else if(n==0){
printf("OKn");break;}
else{
perror("Fail to read");goto end;}
}
res=0;
end:
close(fd);
retur

n res;
}

//===================get==============================
int do_get(int cfd,char *file)
{
struct stat statbuf;
int n,fd,len,res=-1;
char buf[MAX_LINE],c[MAX_LINE],*save=file;
char *lenc=c;
printf("%sn",file);
while(*file!=' ')
{*lenc++ = *file++;*lenc='';}
len=atoi(c);
printf("lenc:%s,file:%s,len:%dn",c,file,len);
if((fd=open(file+1,O_WRONLY|O_CREAT|O_TRUNC,0644))==-1)
{
if(errno==EISDIR)
{
if(my_write(cfd,"ERR server has a dir with the same namen",41)==-1)
goto end;
res=0;
goto end;
}else{
my_write(cfd,"ERR open server filen",22);
goto end;
}
}

if(fstat(fd,&statbuf)==-1)
{my_write(cfd,"ERR stat server filen",22);goto end;}

if(!S_ISREG(statbuf.st_mode))
{
if(my_write(cfd,"ERR server path should be a regular filen",42)==-1)
goto end;
res=0;
goto end;
}

if(my_write(cfd,"OK",3)==-1)
goto end;

while(1)
{
if(len!=0)
n=my_read(cfd,buf,MAX_LINE);

if(n>0)
{write(fd,buf,n);len-=n;n=0;}
else if(len==0)
{printf("OKn");break;}
else
goto end;
}
res=0;
end:
close(fd);
return res;
}

//======================cd===========================
int do_cd(int cfd,char *path)
{
if(chdir(path)==-1)
{perror("Fail to change directoryn");
my_write(cfd,"ERR cat't change directoryn",28);return -1;
}
my_write(cfd,"OKn",4);
return 0;
}

//=====================ls===========================
int do_ls(int cfd,char *path)
{
char cmd[128],buf[NAME_LEN];
struct stat statbuf;
int n,fd,res=-1;

sprintf(cmd,"ls %s > temp.txt",path);
system(cmd);

if((fd=open("temp.txt",O_RDONLY))==-1)
{my_write(cfd,"ERR ls server filen",20);return res;}

if(fstat(fd,&statbuf)==-1)
{my_write(cfd,"ERR stat server filen",22);goto end;}

if(!S_ISREG(statbuf.st_mode))
{
if(my_write(cfd,"ERR server path should be a regular filen",42)==-1)
goto end;
res=0;
goto end;
}

sprintf(buf,"OK %d",statbuf.st_size);
if(my_write(cfd,buf,strlen(buf)+1)==-1)
goto end;

if(my_read(cfd,buf,MAX_LINE)<=0)
goto end;

while(1)
{
n=read(fd,buf,MAX_LINE);
printf("%d,%sn",n,buf);
if(n>0)
{ if(my_write(cfd,buf,n)==-1)
goto end;}
else if(n==0)
{printf("OKn");break;}
else
{perror("Fail to read");goto end;}

}

res=0;
end:
close(fd);
return res;
}

//==================command=========================
int do_command(int cfd,char *path)
{
char cmd[128],buf[MAX_LINE];
struct stat statbuf;
int n,fd,res=-1;

sprintf(cmd,"%s > temp.txt",path);
system(cmd);

if((fd=open("temp.txt",O_RDONLY))==-1)
{my_write(cfd,"ERR ls server filen",20);return res;}

if(fstat(fd,&statbuf)==-1)
{my_write(cfd,"ERR stat server filen",22);goto end;}

if(!S_ISREG(statbuf.st_mode))
{
if(my_write(cfd,"ERR server path should be a regular filen",42)==-1)
goto end;
res=0;
goto end;
}

sprintf(buf,"OK %d",statbuf.st_size);
if(my_write(cfd,buf,strlen(buf)+1)==-1)
goto end;

if(my_read(cfd,buf,MAX_LINE)<=0)
goto end;

while(1)
{
n=read(fd,buf,MAX_LINE);
printf("n:%d,buf:%sn",n,buf);fflush(stdout);
if(n>0)
{ if(my_write(cfd,buf,n)==-1)
goto end;n=0;}
else if(n==0)
{printf("OKn");break;}
else
{perror("Fail to read");goto end;}

}

res=0;
end:
close(fd);
return res;
}

以上是主要文件的内容,其它文件请下载压缩包。

点击下载

CentOS 5.4试用 kvm

前一阵子试用了一下centos5.4,没觉出多在区别来,想试用一下kvm,可以测试用的电脑没有vt-x,过几天有空,安在主机上试试吧,启动速度还是一如既往的慢,xen内核下的显卡驱动,就是一个古老的传说!!!!

unix 域 套接字 示例

这个与AF_INET不同地方在于,客户端也要用bind绑定一下,不要靠connect分配,不然服务器是无法知道客户用的是哪个套接字文件

服务器端

[ssj@main test]$ cat AFunixserver.c
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>

#define PATH "/home/ssj/source/test/connect.socket"
#define STALE 30
#define MAX 1024

void my_turn(char *p)
{
if (p == NULL)return;
while( *p != '')
{
if( *p >= (char)0x61 && *p <= (char)0x7a)
*p=(char)((int)*p-(int)0x20);
p++;
}
}

int init(int *lfd,char *path)
{
int fd,len;
struct sockaddr_un un;

if((fd=socket(AF_UNIX,SOCK_STREAM,0))==-1)
{perror("Fail to socket");return -1;}

unlink(path);
memset(&un,0,sizeof(un));
un.sun_family=AF_UNIX;
strcpy(un.sun_path,path);

len=offsetof(struct sockaddr_un,sun_path)+strlen(path);
if(bind(fd,(struct sockaddr *)&un,len)==-1)
{perror("Fail to bind");goto err;}

if(listen(fd,10)==-1){perror("Fail to listen");goto err;}

*lfd=fd;
return 0;

err:
close(fd);return -1;
}

int main(void)
{
int lfd,cfd,len,n;
time_t staletime;
struct sockaddr_un un;
struct stat statbuf;
char buf[MAX];

if(init(&lfd,PATH)==-1)
exit(1);
printf("running……n");

while(1)
{
len=sizeof(struct sockaddr_un);
if((cfd=accept(lfd,(struct sockaddr *)&un,&len))==-1)
{perror("Fail to accept");exit(1);}

len-=offsetof(struct sockaddr_un,sun_path);
un.sun_path[len]='';

if(stat(un.sun_path,&statbuf)==-1){perror("Fail to get status");exit(1);}
if((statbuf.st_mode&(S_IRWXG|S_IRWXO))||(statbuf.st_mode&S_IRWXU)!=S_IRWXU)
{printf("wrong permissionsn");exit(1);}

staletime=time(NULL)-STALE;
if(statbuf.st_atime<staletime||statbuf.st_ctime<staletime||statbuf.st_mtime<staletime){printf("client is too oldn");close(cfd);break;}

if(unlink(un.sun_path)==-1){perror("Fail to unlink");exit(1);}

n=read(cfd,buf,MAX);
if (n==-1)
{
perror("Fail to read");
exit(1);
}
else if(n==0)
{
printf("the connect closedn");
close(cfd);
continue;
}

if(strcmp(buf,"Nothing")==0)
goto close;

my_turn(buf);
if(write(cfd,buf,n)==-1){perror("Fail to write");exit(1);}
close(cfd);

}

if(unlink(PATH)==-1){perror("Fail to unlink");exit(1);}

close(lfd);
return 0;

close:
write(cfd,"Server Close",13);
close(cfd);
close(lfd);
if(unlink(PATH)==-1){perror("Fail to unlink");exit(1);}
return -1;
}

客户端

[ssj@main test]$ cat AFunixclient.c
#include <stdio.h>
#include <stddef.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>

#define PATH "/home/ssj/source/test/connect.socket"
#define C_PATH "/home/ssj/source/test/"
#define MAX 1024

int main(int argc,char *argv[])
{
int cfd,len;
struct sockaddr_un un;
char buf[MAX],*str="Nothing";
if(argc>1)
str=argv[1];

if((cfd=socket(AF_UNIX,SOCK_STREAM,0))==-1)
{perror("Fail to socket");exit(1);}

memset(&un,0,sizeof(struct sockaddr_un));
un.sun_family=AF_UNIX;
sprintf(un.sun_path,"%s%d",C_PATH,getpid());
len=offsetof(struct sockaddr_un,sun_path)+strlen(un.sun_path);

unlink(un.sun_path);

if(bind(cfd,(struct sockaddr *)&un,len)==-1)
{perror("Fail to bind");exit(1);}

if(chmod(un.sun_path,S_IRWXU)<0){perror("Fail to chmod");exit(1);}

memset(&un,0,sizeof(struct sockaddr_un));
un.sun_family=AF_UNIX;
strcpy(un.sun_path,PATH);

len=offsetof(struct sockaddr_un,sun_path)+strlen(un.sun_path);

if(connect(cfd,(struct sockaddr *)&un,len)<0)
{perror("Fail to connect");exit(1);}

if(write(cfd,str,len)==-1){perror("Fail to write");exit(1);}

if(read(cfd,buf,MAX)==-1){perror("Fail to read");exit(1);}

printf("recive from server:%sn",buf);
close(cfd);
return 0;
}

非命名unix 域 套接字 示例

这个东西和匿名管道就没有什么区别呀

[ssj@main test]$ cat socketpair.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <string.h>

#define MAX 100

int main(void)
{
int sockfd[2],n;
pid_t pid;
char buf[MAX];
bzero(buf,sizeof(buf));

if(socketpair(AF_UNIX,SOCK_STREAM,0,sockfd)==-1)
{perror("Fail to socketpair");exit(1);}

pid=fork();
if(pid<0){perror("Fail to fork");exit(1);}
else if(pid==0)
{
close(sockfd[0]);
strcpy(buf,"hello parent");
n=strlen(buf);

if(write(sockfd[1],buf,n)==-1)
{
perror("Fail to write");
exit(1);
}
printf("child closen");

}
else
{
close(sockfd[1]);

if((n=read(sockfd[0],buf,MAX))==-1)
{
perror("Fail to read");exit(1);
}
//printf("%dn",n);
//n=strlen(buf);
//printf("%dn",n);
buf[n]='';
printf("from child:%sn",buf);

if(wait(NULL)==-1){perror("Fail to wait");exit(1);}

printf("the parent donen");
}
return 0;

}

在打印字符串之前的buf[n]='';只是为了测试,如果不给buf赋初值,打印结果会有所不同,注意字符串的‘’

I/O 多路选择示例 (select)

书中自有言如玉呀,select函数的使用方法,我在中间加入了一些printf语言,更方便理解程序的流程
客户端用之前的TCP例子就行了,见http://www.kumouse.com/article.asp?id=141

[ssj@main test]$ cat selectserver.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>

#define MAX 80

int port=65533;

void my_turn(char *p)
{sleep(5);
if (p == NULL)return;
while( *p != '')
{
if( *p >= (char)0x61 && *p <= (char)0x7a)
*p=(char)((int)*p-(int)0x20);
p++;
}
}

int main(void)
{
struct sockaddr_in sin,cin;
int lfd,cfd,sfd,rdy,client[FD_SETSIZE],maxi,maxfd;
fd_set rset,allset;
socklen_t addr_len;
char buf[MAX],str[INET_ADDRSTRLEN];
int i,n,len,opt=1;

bzero(&sin,sizeof(sin));
sin.sin_family=AF_INET;
sin.sin_addr.s_addr=INADDR_ANY;
sin.sin_port=htons(port);

lfd=socket(AF_INET,SOCK_STREAM,0);
if(lfd==-1)
{perror("Fail to socket");exit(1); }

setsockopt(lfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));

if((n=bind(lfd,(struct sockaddr *)&sin,sizeof(sin)))==-1)
{perror("Fail to bind");exit(1);}

if((n=listen(lfd,20))==-1)
{perror("Fail to listen");exit(1);}

printf("running….n");

maxfd=lfd;
maxi=-1;

for(i=0;i<FD_SETSIZE;i++)
{client[i]=-1;}

FD_ZERO(&allset);
FD_SET(lfd,&allset);

while(1){
rset=allset;
rdy=select(maxfd+1,&rset,NULL,NULL,NULL);
printf("%dn",rdy);
if(FD_ISSET(lfd,&rset)){
printf("p1n");
addr_len=sizeof(sin);
if((cfd=accept(lfd,(struct sockaddr *)&cin,&addr_len))==-1)
{perror("Fail to accept");exit(1);}

for(i=0;i<FD_SETSIZE;i++)
if(client[i]<0){client[i]=cfd;break;}

if(i==FD_SETSIZE){printf("too many clientsn");exit(1);}

FD_SET(cfd,&allset);

if(cfd>maxfd)
maxfd=cfd;
if(i>maxi)
maxi=i;
if(–rdy<=0)
continue;
}

for(i=0;i<=maxi;i++){
if((sfd=client[i])<0)continue;

if(FD_ISSET(sfd,&rset)){
if((n=read(sfd,buf,MAX))==0){
printf("the other side has been close.n");
fflush(stdout);
close(sfd);
FD_CLR(sfd,&allset);
client[i]=-1;
}
else{
inet_ntop(AF_INET,&cin.sin_addr,str,sizeof(str));
printf("client IP:%s:%dn",str,ntohs(cin.sin_port));
my_turn(buf);
if((n=write(sfd,buf,n))==-1)exit(1);
}
if(–rdy<=0)break;
}
}
}
close(lfd);
return 0;
}