| Navigation:First_Community->Software->Linux development | Goto:New Topic•Setting•Search | |
MegaEntry - Social networking and discussion site!
-------------------------------------------------------------------------------- Client int sock_connect(char *domain,int port) {CopyRight owned by the original author.--(www.MegaEntry.com)
int white_sock; struct hostent * site; struct sockaddr_in me; site = gethostbyname(domain); if (site==NULL) return -2;MegaEntry - Social networking and discussion site!
white_sock = socket(AF_INET,SOCK_STREAM,0); if (white_sock<0) return -1; memset(&me,0,sizeof(struct sockaddr_in)); memcpy(&me.sin_addr,site->h_addr_list[0],site->h_length); me.sin_family = AF_INET;MegaEntry - Social networking and discussion site!
me.sin_port = htons(port); return (connect(white_sock,(struct sockaddr *)&me,sizeof(struct sockaddr))<0) ? -1 : white_sock; } 要由Client向伺服器端要求连线的步骤,首先您必须要找出对方的位址,可利用:MegaEntry - Social networking and discussion site!
gethostbyname() 接下来要建立起一个socket,然後用这个socket来建立连线。 接下来我们利用这个简单的socket程式来写一个读取WWW网页的简单浏览器(看html source)。MegaEntry - Social networking and discussion site!
#includeCopyRight owned by the original author.--(www.MegaEntry.com)
#includeMegaEntry - Social networking and discussion site!
struct sockaddr_in me; site = gethostbyname(domain); if (site==NULL) return -2;MegaEntry - Social networking and discussion site!
white_sock = socket(AF_INET,SOCK_STREAM,0); if (white_sock<0) return -1; memset(&me,0,sizeof(struct sockaddr_in)); memcpy(&me.sin_addr,site->h_addr_list[0],site->h_length); me.sin_family = AF_INET;CopyRight owned by the original author.--(www.MegaEntry.com)
me.sin_port = htons(port); return (connect(white_sock,(struct sockaddr *)&me,sizeof(struct sockaddr))<0) ? -1 : white_sock; } int htsend(int sock,char *fmt,...)MegaEntry - Social networking and discussion site!
{ char BUF[1024]; va_list argptr; va_start(argptr,fmt); vsprintf(BUF,fmt,argptr); va_end(argptr);MegaEntry - Social networking and discussion site!
return send(sock,BUF,strlen(BUF),0); } void main(int argc,char **argv) { int black_sock;CopyRight owned by the original author.--(www.MegaEntry.com)
char bugs_bunny[3]; if (argc<2) return; black_sock = htconnect(argv[1],80); if (black_sock<0) return;MegaEntry - Social networking and discussion site!
htsend(black_sock, "GET / HTTP/1.0%c ",10); htsend(black_sock, "Host: %s%c ",argv[1],10); htsend(black_sock, "%c ",10); while (read(black_sock,bugs_bunny,1)>0) { printf( "%c ",bugs_bunny[0]); } close(black_sock);MegaEntry - Social networking and discussion site!
} 编译: gcc -o ex1 client.c 执行 ./ex1 www.linux.org.twCopyRight owned by the original author.--(www.MegaEntry.com)
-------------------------------------------------------------------------------- Server Listen to a portMegaEntry - Social networking and discussion site!
要建立起一个网路伺服器,第一步就是要 "倾远方 ",也就是要Listen。 以下是一般建立服务的方法: int DaemonSocket; struct sockaddr_in DaemonAddr; int BindSocket(void) {CopyRight owned by the original author.--(www.MegaEntry.com)
DaemonSocket = socket(AF_INET,SOCK_STREAM,0); if (DaemonSocket==-1) return 0; DaemonAddr.sin_family = AF_INET; DaemonAddr.sin_port = htons(DAEMON_PORT); if (bind(DaemonSocket,&DaemonAddr,sizeof(DaemonAddr))<0) {MegaEntry - Social networking and discussion site!
printf( "Can not bind! "); return 0; } if (listen(DaemonSocket,1024)!=0) { printf( "Can not listen! "); return 0;CopyRight owned by the original author.--(www.MegaEntry.com)
} return 1; } Incoming callMegaEntry - Social networking and discussion site!
要查看是否有连线进来,可用以下方式: int incoming_call(void) { fd_set sock; struct timeval tv; int t;MegaEntry - Social networking and discussion site!
FD_ZERO(&sock); FD_SET(DaemonSocket,&sock); tv.tv_sec = 60; tv.tv_usec = 0; t = select(DaemonSocket + 1,&sock,NULL,NULL,&tv); if (t<=0||!FD_ISSET(DaemonSocket,&sock)) return 0;CopyRight owned by the original author.--(www.MegaEntry.com)
printf( "incoming... "); return 1; }CopyRight owned by the original author.--(www.MegaEntry.com)
Connect Client 当我们确认有人进来要求服务时,会需要accept connection,可用以下方式 int ConnectClient(void) { int socksize=sizeof(HostAddr); unsigned char * addr;MegaEntry - Social networking and discussion site!
ClientSocket = accept(DaemonSocket,(struct sockaddr*)&HostAddr,&socksize); if (ClientSocket<0) return 0; addr = (unsigned char *)&HostAddr.sin_addr.s_addr;CopyRight owned by the original author.--(www.MegaEntry.com)
printf( "incoming address:%d.%d.%d.%d ",addr[0],addr[1],addr[2],addr[3]); return 1; } 注意到当您accept connection之後,连线已建立起,此时要用的socket是ClientSocket,而非DaemonSocket,ClientSocket才是真正用来连线用的socket。MegaEntry - Social networking and discussion site!
这是个我才刚开始动手写的象棋伺服器。 #includeMegaEntry - Social networking and discussion site!
#includeMegaEntry - Social networking and discussion site!
#includeCopyRight owned by the original author.--(www.MegaEntry.com)
#define DAEMON_PORT 9901 int DaemonSocket; struct sockaddr_in DaemonAddr; int ClientSocket=0;CopyRight owned by the original author.--(www.MegaEntry.com)
struct sockaddr_in HostAddr; void dlog(char *fmt,...) { va_list argptr; FILE *fp;CopyRight owned by the original author.--(www.MegaEntry.com)
fp = fopen(DAEMON_LOG, "a+t "); va_start(argptr,fmt); vfprintf(fp,fmt,argptr); va_end(argptr); fclose(fp);CopyRight owned by the original author.--(www.MegaEntry.com)
} pid_t CheckLock(void) { pid_t me; FILE * fp;MegaEntry - Social networking and discussion site!
fp = fopen(DAEMON_LOCK, "rt "); if (fp==NULL) return 0; fscanf(fp, "%d ",&me); fclose(fp);MegaEntry - Social networking and discussion site!
return me; } pid_t WriteLock(void) { pid_t me;MegaEntry - Social networking and discussion site!
FILE *fp; me = getpid(); fp = fopen(DAEMON_LOCK, "w "); fprintf(fp, "%d ",me);MegaEntry - Social networking and discussion site!
fclose(fp); return me; } int CleanLock(void)MegaEntry - Social networking and discussion site!
{ return (unlink(DAEMON_LOCK)==0); } void report_time(void) {CopyRight owned by the original author.--(www.MegaEntry.com)
time_t now; now = time(NULL); dlog( "%s ",asctime((const struct tm*)localtime(&now))); } static void signal_catch(int signo)CopyRight owned by the original author.--(www.MegaEntry.com)
{ time_t now; close(DaemonSocket); if (ClientSocket>0) close(ClientSocket); CleanLock();CopyRight owned by the original author.--(www.MegaEntry.com)
now = time(NULL); dlog( "Catch signal %d, leave at %s ",signo,asctime((const struct tm*)localti exit(-1); } void SetupSignal(void)MegaEntry - Social networking and discussion site!
{ struct sigaction act; act.sa_handler = signal_catch; act.sa_flags = 0; sigemptyset(&act.sa_mask);CopyRight owned by the original author.--(www.MegaEntry.com)
sigaction(SIGHUP,&act,NULL); sigaction(SIGINT,&act,NULL); sigaction(SIGQUIT,&act,NULL); sigaction(SIGILL,&act,NULL); sigaction(SIGABRT,&act,NULL); sigaction(SIGIOT,&act,NULL);MegaEntry - Social networking and discussion site!
sigaction(SIGBUS,&act,NULL); sigaction(SIGFPE,&act,NULL); sigaction(SIGTERM,&act,NULL); } int BindSocket(void)CopyRight owned by the original author.--(www.MegaEntry.com)
{ DaemonSocket = socket(AF_INET,SOCK_STREAM,0); if (DaemonSocket==-1) return 0; DaemonAddr.sin_family = AF_INET; DaemonAddr.sin_port = htons(DAEMON_PORT);MegaEntry - Social networking and discussion site!
if (bind(DaemonSocket,&DaemonAddr,sizeof(DaemonAddr))<0) { printf( "Can not bind! "); return 0; } if (listen(DaemonSocket,1024)!=0) { printf( "Can not listen! ");MegaEntry - Social networking and discussion site!
return 0; } return 1; }MegaEntry - Social networking and discussion site!
int incoming_call(void) { fd_set sock; struct timeval tv; int t;CopyRight owned by the original author.--(www.MegaEntry.com)
FD_ZERO(&sock); FD_SET(DaemonSocket,&sock); tv.tv_sec = 60; tv.tv_usec = 0; t = select(DaemonSocket + 1,&sock,NULL,NULL,&tv); if (t<=0||!FD_ISSET(DaemonSocket,&sock)) return 0;CopyRight owned by the original author.--(www.MegaEntry.com)
dlog( "incoming... "); return 1; } int ConnectClient(void)CopyRight owned by the original author.--(www.MegaEntry.com)
{ int socksize=sizeof(HostAddr); unsigned char * addr; ClientSocket = accept(DaemonSocket,(struct sockaddr*)&HostAddr,&socksize); if (ClientSocket<0) return 0;MegaEntry - Social networking and discussion site!
addr = (unsigned char *)&HostAddr.sin_addr.s_addr; dlog( "incoming address:%d.%d.%d.%d ",addr[0],addr[1],addr[2],addr[3]); return 1;CopyRight owned by the original author.--(www.MegaEntry.com)
} int daemon_printf(char *fmt,...) { char BUF[4096]; va_list argptr;CopyRight owned by the original author.--(www.MegaEntry.com)
va_start(argptr,fmt); vsprintf(BUF,fmt,argptr); va_end(argptr); return write(ClientSocket,BUF,strlen(BUF)); }CopyRight owned by the original author.--(www.MegaEntry.com)
void Log(void) { char BUF[4096]; read(DaemonSocket,BUF,16); daemon_printf( "%s ",BUF[0]);MegaEntry - Social networking and discussion site!
} int main(int argc,char **argv) { pid_t myself; time_t now;CopyRight owned by the original author.--(www.MegaEntry.com)
/* find myself */ myself = CheckLock(); if (myself!=0) { printf( "Existing a copy of chess daemon[pid=%d], leave now. ",myself); exit(1);MegaEntry - Social networking and discussion site!
} /* fork */ myself = fork(); if (myself>0) { exit(1);MegaEntry - Social networking and discussion site!
} else if (myself<0) { printf( "Strange world! I don 't like it. Quit because of pid=%d ",myself); exit(1); } else { SetupSignal();CopyRight owned by the original author.--(www.MegaEntry.com)
if (!BindSocket()) { printf( "Can not bind socket! "); exit(1); } WriteLock(); }MegaEntry - Social networking and discussion site!
printf( "Chess Daemon is up, have fun! "); now = time(NULL); dlog( "---------------------------------------------- ");MegaEntry - Social networking and discussion site!
dlog( "I am back! %s " "Chess Daemon comes to alive again. ", asctime((const struct tm*)localtime(&now)) );CopyRight owned by the original author.--(www.MegaEntry.com)
do { if (incoming_call()) { if (ConnectClient()) { fd_set sock;CopyRight owned by the original author.--(www.MegaEntry.com)
struct timeval tv; int t; char BUF[128]; char CC[2]; int n;MegaEntry - Social networking and discussion site!
daemon_printf( "Welcome to Chinese Chess Game Center! "); FD_ZERO(&sock); FD_SET(ClientSocket,&sock); n = 0; do {MegaEntry - Social networking and discussion site!
tv.tv_sec = 60; tv.tv_usec = 0; t = select(ClientSocket+1,&sock,NULL,NULL,&tv); if (t<=0||!FD_ISSET(ClientSocket,&sock)) ; read(ClientSocket,CC,1); if (CC[0]==13||CC[0]==10||CC[0]==0) { BUF[n] = 0;CopyRight owned by the original author.--(www.MegaEntry.com)
dlog( "%s ",BUF); if (strncasecmp(BUF, "exit ",4)==0) { close(ClientSocket); break; } n = 0;CopyRight owned by the original author.--(www.MegaEntry.com)
} else { BUF[n]=CC[0]; n++; } } while (1); } }CopyRight owned by the original author.--(www.MegaEntry.com)
} while (1); return 1; } 检验MegaEntry - Social networking and discussion site!
telnet localhost 9901 在处理Connect Client时,事实上可以运用fork或thread来处理多个连线。