完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
/* 这是client的code*/ #include #include #include #include #include #include #include #include #include #define S_PORT 3333 #define BUFFER 100 int main(int argc,char **argv) { struct sockaddr_in server_addr; int c_fd,s_fd; socklen_t s_len; char *buffer = (char *)malloc(BUFFER+1); char addr_t[BUFFER]; int s_bits,r_bits=-1; if(argc != 2){ printf("usage:no valid argument!n");exit (-1); } if(!buffer) {perror("malloc");exit (-1);} bzero(buffer,BUFFER+1); c_fd=socket(AF_INET,SOCK_STREAM,0); if(!c_fd){perror("socket");exit (-1);} server_addr.sin_port = htons(S_PORT); /*inet_pton(AF_INET,argv[1],addr_t);*/ server_addr.sin_addr.s_addr = inet_addr(argv[1]); server_addr.sin_family = AF_INET; s_len = sizeof(struct sockaddr_in); if((s_fd=connect(c_fd,(struct sockaddr*)&server_addr,s_len))==-1){ perror("connect");exit (-1); } gets(buffer); s_bits = send(c_fd,buffer,sizeof(buffer),0); if(s_bits == sizeof(buffer)){ printf("success send to server: %s",buffer); } else{ printf("fail send to server!n"); } r_bits = recv(c_fd,buffer,sizeof(buffer),0); if(r_bits == -1){ printf("receive from server error!n");exit (-1); } else printf("receive from the server : %sn",buffer); if(close(c_fd)==-1){ printf("close the socket error!n");exit (-1); } return 1; } /* 这是server的代码*/ #include #include #include #include #include #include #include #include #include #include #include #define S_PORT 3333 #define BUFFER 100 void up_chars(char *buf){ int i=0; while((buf[i]=toupper(buf[i])) != ' |