|
#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>
#include<signal.h>
#define TRUE 1
#define FALSE 0
int main(int argc,char **argv){
int y,z;
char *srvr_addr=NULL;
static int so_reuseaddr=TRUE;
char *bc_addr="202.197.242.255";
struct sockaddr_in srvradr_inet,adr_inet,bcadr_inet;
struct sockaddr_in clntadr_inet;
int len_inet,len_bc;
int s;
char dgram[512],*dgram1="quit";
srvr_addr="202.197.242.221";
len_bc=sizeof bc_addr;
s=socket(AF_INET,SOCK_DGRAM,0);
if(s==-1) printf("create error.\n");
else printf("create success\n");
srvradr_inet.sin_family=AF_INET;
srvradr_inet.sin_port=htons(9999);
srvradr_inet.sin_addr.s_addr=inet_addr(srvr_addr);
len_inet=sizeof(srvradr_inet);
bcadr_inet.sin_family=AF_INET;
bcadr_inet.sin_port=htons(9097);
bcadr_inet.sin_addr.s_addr=inet_addr(bc_addr);
z=setsockopt(s,SOL_SOCKET,SO_REUSEADDR,&so_reuseaddr,sizeof so_reuseaddr);
if(z==-1) printf("setsockopt error!");
y=bind(s,(struct sockaddr *)&bcadr_inet,len_inet);
if(z==-1) printf("bind error");
printf("bind success!\n");
while(1){
printf("please input your messages:\n");
fgets(dgram,sizeof dgram,stdin);
if(!strcasecmp(dgram,"quit")) break;
sendto(s,dgram,sizeof(dgram),0,(struct sockaddr *)&srvradr_inet,len_inet);
printf("after sendto\n");
z=recvfrom(s,dgram,sizeof(dgram),0,(struct sockaddr *)&bcadr_inet,&len_inet);
printf("the message from bcserver is: %s",dgram);
}
close(s);
return 0;
}
上面这段程序是一个简单网络聊天室的客户端程序,能够运行,问题是输入字符串"quit"时,不能退出,请哪位能人指教!! |
|