关于C socket的问题!!!
#include<stdio.h>#ubclude<unistd.h>#include<stdlib.h>#include<errno.h>#include<string...
#include<stdio.h>
#ubclude<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<time.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
static void
bail(const char*on_what){
fputs(strerror(errno),stderr);
fputs(":",stderr);
fputs(on_what,stderr);
fputs('\n',stderr);
exit(1);
}
int main(int argc,char**argv){
int z;
int x;
char*srvr_addr=NULL;
struct sockaddr_in adr_srvr;
struct sockaddr_in adr;
int len_inet;
char dgram[512];
if(argc>=2){
srvr_addr=argv[1];
}else{
srvr_addr="127.0.0.1";
}
memset(&adr_srvr,0,sizeof adr_srvr);
adr_srvr.sin_family= AF_INET;
adr_srvr.sin_post=htons(4000);
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;
s=socket(AF_INET,SOCK_DGRAM,0);
if(s==-1)
bail("socket()");
for(;;){
fputs("\nEnter format string:",stdout);
if(!fgets(dgram,sizeof dgram,stdin))
break;
z=strlen(dgram);
if(z>0&&dgram[--z]=='\n')
dgram[z]=0;
z=sendto(s,dgram,strlen(dgram),0,(struct sockaddr*)&adr_srvr,len_inet);
if(z<0)
bail("sendto(2)");
if(!srecasecmp(dgram,"QUIT"))
break;
x=sizeof adr;
z=recvfrom(s,dgram,sizeof dgram,0,(struct sockaddr*)&adr,&x);
if(z<0)
bail("recvfrom(2)");
dgram[z]=0;
printf("Result from %s port%u:\n\t'%s'\n",inet_ntoa(adr.sin_addr),(unsigned)ntophs(adr.sin_port),dgram);
close(s);
putchar('\n');
return 0;
}
麻烦帮我解释下这段代码什么意思!!↓》???
static void
bail(const char*on_what){
fputs(strerror(errno),stderr);
fputs(":",stderr);
fputs(on_what,stderr);
fputs('\n',stderr);
exit(1); 展开
#ubclude<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<time.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
static void
bail(const char*on_what){
fputs(strerror(errno),stderr);
fputs(":",stderr);
fputs(on_what,stderr);
fputs('\n',stderr);
exit(1);
}
int main(int argc,char**argv){
int z;
int x;
char*srvr_addr=NULL;
struct sockaddr_in adr_srvr;
struct sockaddr_in adr;
int len_inet;
char dgram[512];
if(argc>=2){
srvr_addr=argv[1];
}else{
srvr_addr="127.0.0.1";
}
memset(&adr_srvr,0,sizeof adr_srvr);
adr_srvr.sin_family= AF_INET;
adr_srvr.sin_post=htons(4000);
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;
s=socket(AF_INET,SOCK_DGRAM,0);
if(s==-1)
bail("socket()");
for(;;){
fputs("\nEnter format string:",stdout);
if(!fgets(dgram,sizeof dgram,stdin))
break;
z=strlen(dgram);
if(z>0&&dgram[--z]=='\n')
dgram[z]=0;
z=sendto(s,dgram,strlen(dgram),0,(struct sockaddr*)&adr_srvr,len_inet);
if(z<0)
bail("sendto(2)");
if(!srecasecmp(dgram,"QUIT"))
break;
x=sizeof adr;
z=recvfrom(s,dgram,sizeof dgram,0,(struct sockaddr*)&adr,&x);
if(z<0)
bail("recvfrom(2)");
dgram[z]=0;
printf("Result from %s port%u:\n\t'%s'\n",inet_ntoa(adr.sin_addr),(unsigned)ntophs(adr.sin_port),dgram);
close(s);
putchar('\n');
return 0;
}
麻烦帮我解释下这段代码什么意思!!↓》???
static void
bail(const char*on_what){
fputs(strerror(errno),stderr);
fputs(":",stderr);
fputs(on_what,stderr);
fputs('\n',stderr);
exit(1); 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询