一个简单的客户/服务器应用(socket 学习)
2011-09-21 17:32
549 查看
/* 子进程为客户方,父进程为服务器方。当客户请求通过socket传送给服务器方时,服务器会将当前的系统时间返回给客户
该程序参考自《实战linux socket 编程 》 Warren W.Gay
*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<time.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/wait.h>
int main(int argc,char **argv)
{
int z;
int s[2];
char *msgp;
int mlen;
char buf[80];
pid_t chpid;
z=socketpair(AF_LOCAL,SOCK_STREAM,0,s);
if(z==-1)
{
fprintf(stderr,"%s:socketpair(AF_LOCAL,SOCK_STREAM,0)\n",strerror(errno));
exit(1);
}
if((chpid=fork())==(pid_t)-1)
{
fprintf(stderr,"%s:fork(2)\n",strerror(errno));
exit(1);
}
else if(chpid==0)
{
/*子进程,客户端*/
char rxbuf[80];
printf("Parent PID is %ld",(long)getppid());
close(s[0]);
s[0]=-1;
msgp="%A %d-%b-%Y %1:%M %p";
mlen=strlen(msgp);
printf("Child sending request '%s'\n",msgp);
/*向服务器写入请求*/
z=write(s[1],msgp,mlen);
/*if(shutdown(s[1],SHUT_WR)==-1)
{
fprintf(stderr,"%s:shutdown(2)\n",strerror(errno));
exit(1);
} */
z=read(s[1],rxbuf,sizeof rxbuf);
if(z<0)
{
fprintf(stderr,"%s:read(2)\n",strerror(errno));
exit(1);
}
rxbuf[z]=0;
printf("Server returned '%s'\n",rxbuf);
fflush(stdout);
close(s[1]);
}
else
{
int status;
char txbuf[80];
time_t td;
printf("child PID is %ld",(long)chpid);
fflush(stdout);
close(s[1]);
s[1]=-1;
z=read(s[0],buf,sizeof buf);
if(z<0)
{
fprintf(stderr,"%s:read(2)\n",strerror(errno));
exit(1);
}
buf[z]=0;
time(&td);
strftime(txbuf,sizeof txbuf,buf,localtime(&td));
z=write(s[0],txbuf,strlen(txbuf));
if(z<0)
{
fprintf(stderr,"%s:write(2)\n",strerror(errno));
exit(1);
}
close(s[0]);
waitpid(chpid,&status,0);
}
return 0;
}
该程序参考自《实战linux socket 编程 》 Warren W.Gay
*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<errno.h>
#include<time.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/wait.h>
int main(int argc,char **argv)
{
int z;
int s[2];
char *msgp;
int mlen;
char buf[80];
pid_t chpid;
z=socketpair(AF_LOCAL,SOCK_STREAM,0,s);
if(z==-1)
{
fprintf(stderr,"%s:socketpair(AF_LOCAL,SOCK_STREAM,0)\n",strerror(errno));
exit(1);
}
if((chpid=fork())==(pid_t)-1)
{
fprintf(stderr,"%s:fork(2)\n",strerror(errno));
exit(1);
}
else if(chpid==0)
{
/*子进程,客户端*/
char rxbuf[80];
printf("Parent PID is %ld",(long)getppid());
close(s[0]);
s[0]=-1;
msgp="%A %d-%b-%Y %1:%M %p";
mlen=strlen(msgp);
printf("Child sending request '%s'\n",msgp);
/*向服务器写入请求*/
z=write(s[1],msgp,mlen);
/*if(shutdown(s[1],SHUT_WR)==-1)
{
fprintf(stderr,"%s:shutdown(2)\n",strerror(errno));
exit(1);
} */
z=read(s[1],rxbuf,sizeof rxbuf);
if(z<0)
{
fprintf(stderr,"%s:read(2)\n",strerror(errno));
exit(1);
}
rxbuf[z]=0;
printf("Server returned '%s'\n",rxbuf);
fflush(stdout);
close(s[1]);
}
else
{
int status;
char txbuf[80];
time_t td;
printf("child PID is %ld",(long)chpid);
fflush(stdout);
close(s[1]);
s[1]=-1;
z=read(s[0],buf,sizeof buf);
if(z<0)
{
fprintf(stderr,"%s:read(2)\n",strerror(errno));
exit(1);
}
buf[z]=0;
time(&td);
strftime(txbuf,sizeof txbuf,buf,localtime(&td));
z=write(s[0],txbuf,strlen(txbuf));
if(z<0)
{
fprintf(stderr,"%s:write(2)\n",strerror(errno));
exit(1);
}
close(s[0]);
waitpid(chpid,&status,0);
}
return 0;
}
相关文章推荐
- Java网络学习笔记1:用(Server)Socket编写简单的客户/服务器程序
- Zigbee学习-添加一个简单应用到OSAL中
- ios学习笔记block回调的应用(一个简单的例子)
- 【安卓学习】4.碎片(Fragment)实践---一个简单的新闻应用。
- 使用React、Node.js、MongoDB、Socket.IO开发一个角色投票应用的学习过程(三)
- ServerSocket的一个简单例子,学习backlog参数的设置,即可以接受客户端的数量
- 【狂人C】学习笔记之一个简单的乘法函数的应用
- ios学习笔记block回调的应用(一个简单的例子)
- 学习笔记——一个简单的还原短信的Android应用出现的问题
- Socket 由浅入深,开发一个真正的通信应用(学习)
- 使用React、Node.js、MongoDB、Socket.IO开发一个角色投票应用的学习过程(一)
- socket_一个简单应用_源代码
- 构建一个简单的WCF应用——WCF学习笔记(1)
- socket_一个简单应用_源代码
- struts2.x学习一(搭建一个简单的Struts2应用)
- Hibernate学习笔记1--------一个简单的应用
- 用Python socket实现一个简单的http服务器(post 与get 的区别)、CGIHTTPServer 简单应用
- 使用React、Node.js、MongoDB、Socket.IO开发一个角色投票应用的学习过程(二)
- 网络编程与多线程的应用--基于socket udp编写一个简单聊天程序
- 一个简单的git应用教程