当前位置: 技术问答>linux和unix
上午的笔试题目(帮帮忙)
来源: 互联网 发布时间:2015-12-21
本文导语: 这几天笔试老考不好,情高手帮帮忙! 设计一个Linux或window环境下C语言编写基于TCP多线程服务器和客户端发送程序,程序各个部分要完整。 (1)对于服务程序,它收到客户端发来字串后,使用printf()语句进行显...
这几天笔试老考不好,情高手帮帮忙!
设计一个Linux或window环境下C语言编写基于TCP多线程服务器和客户端发送程序,程序各个部分要完整。
(1)对于服务程序,它收到客户端发来字串后,使用printf()语句进行显示。要求对客户端报文的处理采用多线程并发处理的方式。
(2)对于客户端,它接收来自键盘的消息,然后发送给指定的IP服务器。
设计一个Linux或window环境下C语言编写基于TCP多线程服务器和客户端发送程序,程序各个部分要完整。
(1)对于服务程序,它收到客户端发来字串后,使用printf()语句进行显示。要求对客户端报文的处理采用多线程并发处理的方式。
(2)对于客户端,它接收来自键盘的消息,然后发送给指定的IP服务器。
|
贴一段吧:
-----server源码---------
/* server.c */
/* Get Time From Server */
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
void init_daemon(void); /* Daemon init */
/* Func: Report Error message to Log */
static void bail(const char *on_what){
FILE *fp;
time_t t;
if((fp=fopen("/tmp/socket.log","a"))>=0) {
t=time(0);
fprintf(fp,"server> %s error %sn",asctime(localtime(&t)),on_what);
}
fclose(fp);
exit(1);
}
/* Func: Report Server process action */
static void printlog(const char *on_what){
FILE *fp;
time_t t;
if((fp=fopen("/tmp/socket.log","a"))>=0) {
t=time(0);
fprintf(fp,"server> %s do:%sn",asctime(localtime(&t)),on_what);
}
fclose(fp);
}
int main(int argc,char **argv){
int z;
char *srvr_addr=NULL;
char *srvr_port="9909";
struct sockaddr_in adr_srvr; /* AF_INET */
struct sockaddr_in adr_clnt; /* AF_INET */
int len_inet; /* length */
int s; /* server socket */
int c; /* client socket */
int n; /* byte */
time_t td; /* current datetime */
char dtbuf[128];
/* Init Daemon Process */
init_daemon();
printlog("Set Server Process is Daemon Process!");
/* server socket IP address or local address 127.0.0.1 */
if (argc>=2){
/* cmd IP */
srvr_addr=argv[1];
} else {
/* local IP */
srvr_addr="127.0.0.1";
}
/* second param is port */
if (argc>=3){
srvr_port=argv[2];
}
/* create socket */
s=socket(AF_INET,SOCK_STREAM,0);
if (s==-1)
bail("socket()");
printlog("Create socket success");
/* create server socket address */
memset(&adr_srvr,0,sizeof(adr_srvr));
adr_srvr.sin_family=AF_INET;
adr_srvr.sin_port=htons(atoi(srvr_port));
if (strcmp(srvr_addr,"*")!=0){
/* normal address */
adr_srvr.sin_addr.s_addr=inet_addr(srvr_addr);
if (adr_srvr.sin_addr.s_addr==INADDR_NONE){
bail("bad address");
}
} else{
/* General */
adr_srvr.sin_addr.s_addr=INADDR_ANY;
}
/* bind server address */
len_inet=sizeof(adr_srvr);
z=bind(s,(struct sockaddr *)&adr_srvr,len_inet);
if (z==-1)
bail("bind(2)");
printlog("Bind socket success!");
/* listen socket */
z=listen(s,10);
if(z==-1)
bail("listen(2)");
printlog("start listen ...");
/* server loop */
for(;;){
len_inet=sizeof(adr_clnt);
c=accept(s,(struct sockaddr *)&adr_clnt,&len_inet);
if (c==-1)
bail("accept(2)");
printlog("get connect from");
printlog(inet_ntoa(adr_clnt.sin_addr));
/* create time stamp */
time(&td);
n=(int)strftime(dtbuf,sizeof(dtbuf),"%A %b %d %H:%M:%S %Yn",localtime(&td));
/* send result to client */
z=write(c,dtbuf,n);
if(z==-1)
bail("write(2)");
printlog("send time stamp success");
/* close socket */
close(c);
}
return 0;
}
----------client源码------------
/* daytime client */
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
/* Func: Report Error message to Log */
static void bail(const char *on_what){
FILE *fp;
time_t t;
if((fp=fopen("/tmp/socket.log","a"))>=0) {
t=time(0);
fprintf(fp,"client: %s error %sn",asctime(localtime(&t)),on_what);
}
fclose(fp);
exit(1);
}
/* Func: Report Client process action */
static void printlog(const char *on_what){
FILE *fp;
time_t t;
if((fp=fopen("/tmp/socket.log","a"))>=0) {
t=time(0);
fprintf(fp,"client> %s do:%sn",asctime(localtime(&t)),on_what);
}
fclose(fp);
}
int main(int argc,char **argv){
FILE *fp;
time_t t;
int z;
char *srvr_addr=NULL;
char *srvr_port="9909";
struct sockaddr_in adr_srvr;
int len_inet;
int s;
char dtbuf[128];
printlog("Start client process to get Server Time");
if (argc>=2){
srvr_addr=argv[1];
} else {
srvr_addr="127.0.0.1";
}
if (argc>=3){
srvr_port=argv[2];
}
s=socket(AF_INET,SOCK_STREAM,0);
if(s==-1)
bail("socket()");
printlog("Create client socket");
memset(&adr_srvr,0,sizeof(adr_srvr));
adr_srvr.sin_family=AF_INET;
adr_srvr.sin_port=htons(atoi(srvr_port));
adr_srvr.sin_addr.s_addr=inet_addr(srvr_addr);
if(adr_srvr.sin_addr.s_addr==INADDR_NONE)
bail("bad address");
len_inet=sizeof(adr_srvr);
z=connect(s,(struct sockaddr *)&adr_srvr,len_inet);
if(z==-1)
bail("connect(2)");
printlog("client connect server at address");
printlog(inet_ntoa(adr_srvr.sin_addr));
z=read(s,&dtbuf,sizeof(dtbuf)-1);
if(z==-1)
bail("read(2)");
printlog("Get Time from server");
dtbuf[z]=0;
if((fp=fopen("/tmp/socket.log","a"))>=0)
{
t=time(0);
fprintf(fp,"Client:%s Get time is :%sn",asctime(localtime(&t)),dtbuf);
fclose(fp);
}
/* close client socket */
close(s);
printlog("close client socket");
putchar('n');
return 0;
}
----------------------------
编译命令
gcc -g -o clienttime.o clienttime.c
gcc -g -o servertime.o daemon.c servertime.c
执行过程
./servertime.o
./clienttime.o
-----server源码---------
/* server.c */
/* Get Time From Server */
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
void init_daemon(void); /* Daemon init */
/* Func: Report Error message to Log */
static void bail(const char *on_what){
FILE *fp;
time_t t;
if((fp=fopen("/tmp/socket.log","a"))>=0) {
t=time(0);
fprintf(fp,"server> %s error %sn",asctime(localtime(&t)),on_what);
}
fclose(fp);
exit(1);
}
/* Func: Report Server process action */
static void printlog(const char *on_what){
FILE *fp;
time_t t;
if((fp=fopen("/tmp/socket.log","a"))>=0) {
t=time(0);
fprintf(fp,"server> %s do:%sn",asctime(localtime(&t)),on_what);
}
fclose(fp);
}
int main(int argc,char **argv){
int z;
char *srvr_addr=NULL;
char *srvr_port="9909";
struct sockaddr_in adr_srvr; /* AF_INET */
struct sockaddr_in adr_clnt; /* AF_INET */
int len_inet; /* length */
int s; /* server socket */
int c; /* client socket */
int n; /* byte */
time_t td; /* current datetime */
char dtbuf[128];
/* Init Daemon Process */
init_daemon();
printlog("Set Server Process is Daemon Process!");
/* server socket IP address or local address 127.0.0.1 */
if (argc>=2){
/* cmd IP */
srvr_addr=argv[1];
} else {
/* local IP */
srvr_addr="127.0.0.1";
}
/* second param is port */
if (argc>=3){
srvr_port=argv[2];
}
/* create socket */
s=socket(AF_INET,SOCK_STREAM,0);
if (s==-1)
bail("socket()");
printlog("Create socket success");
/* create server socket address */
memset(&adr_srvr,0,sizeof(adr_srvr));
adr_srvr.sin_family=AF_INET;
adr_srvr.sin_port=htons(atoi(srvr_port));
if (strcmp(srvr_addr,"*")!=0){
/* normal address */
adr_srvr.sin_addr.s_addr=inet_addr(srvr_addr);
if (adr_srvr.sin_addr.s_addr==INADDR_NONE){
bail("bad address");
}
} else{
/* General */
adr_srvr.sin_addr.s_addr=INADDR_ANY;
}
/* bind server address */
len_inet=sizeof(adr_srvr);
z=bind(s,(struct sockaddr *)&adr_srvr,len_inet);
if (z==-1)
bail("bind(2)");
printlog("Bind socket success!");
/* listen socket */
z=listen(s,10);
if(z==-1)
bail("listen(2)");
printlog("start listen ...");
/* server loop */
for(;;){
len_inet=sizeof(adr_clnt);
c=accept(s,(struct sockaddr *)&adr_clnt,&len_inet);
if (c==-1)
bail("accept(2)");
printlog("get connect from");
printlog(inet_ntoa(adr_clnt.sin_addr));
/* create time stamp */
time(&td);
n=(int)strftime(dtbuf,sizeof(dtbuf),"%A %b %d %H:%M:%S %Yn",localtime(&td));
/* send result to client */
z=write(c,dtbuf,n);
if(z==-1)
bail("write(2)");
printlog("send time stamp success");
/* close socket */
close(c);
}
return 0;
}
----------client源码------------
/* daytime client */
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
#i nclude
/* Func: Report Error message to Log */
static void bail(const char *on_what){
FILE *fp;
time_t t;
if((fp=fopen("/tmp/socket.log","a"))>=0) {
t=time(0);
fprintf(fp,"client: %s error %sn",asctime(localtime(&t)),on_what);
}
fclose(fp);
exit(1);
}
/* Func: Report Client process action */
static void printlog(const char *on_what){
FILE *fp;
time_t t;
if((fp=fopen("/tmp/socket.log","a"))>=0) {
t=time(0);
fprintf(fp,"client> %s do:%sn",asctime(localtime(&t)),on_what);
}
fclose(fp);
}
int main(int argc,char **argv){
FILE *fp;
time_t t;
int z;
char *srvr_addr=NULL;
char *srvr_port="9909";
struct sockaddr_in adr_srvr;
int len_inet;
int s;
char dtbuf[128];
printlog("Start client process to get Server Time");
if (argc>=2){
srvr_addr=argv[1];
} else {
srvr_addr="127.0.0.1";
}
if (argc>=3){
srvr_port=argv[2];
}
s=socket(AF_INET,SOCK_STREAM,0);
if(s==-1)
bail("socket()");
printlog("Create client socket");
memset(&adr_srvr,0,sizeof(adr_srvr));
adr_srvr.sin_family=AF_INET;
adr_srvr.sin_port=htons(atoi(srvr_port));
adr_srvr.sin_addr.s_addr=inet_addr(srvr_addr);
if(adr_srvr.sin_addr.s_addr==INADDR_NONE)
bail("bad address");
len_inet=sizeof(adr_srvr);
z=connect(s,(struct sockaddr *)&adr_srvr,len_inet);
if(z==-1)
bail("connect(2)");
printlog("client connect server at address");
printlog(inet_ntoa(adr_srvr.sin_addr));
z=read(s,&dtbuf,sizeof(dtbuf)-1);
if(z==-1)
bail("read(2)");
printlog("Get Time from server");
dtbuf[z]=0;
if((fp=fopen("/tmp/socket.log","a"))>=0)
{
t=time(0);
fprintf(fp,"Client:%s Get time is :%sn",asctime(localtime(&t)),dtbuf);
fclose(fp);
}
/* close client socket */
close(s);
printlog("close client socket");
putchar('n');
return 0;
}
----------------------------
编译命令
gcc -g -o clienttime.o clienttime.c
gcc -g -o servertime.o daemon.c servertime.c
执行过程
./servertime.o
./clienttime.o
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。