|
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<time.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main(int argc,char **argv){
int z;
char *srvr_addr=NULL;
struct sockaddr_in adr_inet;
struct sockaddr_in adr_clnt;
int len_inet;
int s;
char dgram[512];
srvr_addr="202.197.242.221";
s=socket(AF_INET,SOCK_DGRAM,0);
if(s==-1) printf("create error.\n");
else printf("create success\n");
memset(&adr_inet,0,sizeof(adr_inet));
adr_inet.sin_family=AF_INET;
adr_inet.sin_port=htons(9014);
adr_inet.sin_addr.s_addr=inet_addr(srvr_addr);
len_inet=sizeof(adr_inet);
z=bind(s,(struct sockaddr *)&adr_inet,len_inet);
if(z==-1) printf("bind error");
printf("bind success!");
if(1){
printf("this is before recvfrom.\n"); //为何去除该行,程序就不能运行??
z=recvfrom(s,dgram,sizeof(dgram),0,(struct sockaddr *)&adr_clnt,&len_inet);
if(z<0) printf("recvfrom is error.\n");
printf("this is after recvfrom.\n");
// printf("the client address is %s.",adr_clnt.sin_addr.s_addr);
dgram[z]='\0';
printf("messgge from client :%s.\n",dgram);
z=sendto(s,dgram,strlen(dgram),0,(struct sockaddr *)&adr_clnt,len_inet);
if(z<0) printf("sendto is error.\n");
}
close(s);
return 0;
} |
|