1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include <unistd.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h>
#define BACKLOG 10
int main(int argc,char *argv[]) { int sfd,cfd,ret; struct sockaddr_in svr_addr,cli_addr; char buffer[1024] = {0}; ssize_t sbytes = 0,rbytes = 0; int length; int total_received; socklen_t len = sizeof(struct sockaddr_in); if (argc != 3){ fprintf(stderr,"Usage : %s < ip > < port >.\n",argv[0]) ; exit(EXIT_FAILURE); }
sfd = socket(AF_INET,SOCK_STREAM,0); if (sfd == -1){ perror("[ERROR] Failed to socket."); exit(EXIT_FAILURE); }
bzero(&svr_addr,sizeof(struct sockaddr_in)); svr_addr.sin_family = AF_INET; svr_addr.sin_port = htons(atoi(argv[2])); svr_addr.sin_addr.s_addr = inet_addr(argv[1]); ret = bind(sfd,(const struct sockaddr *)&svr_addr,sizeof(struct sockaddr)); if (ret == -1){ perror("[ERROR] Failed to bind."); exit(EXIT_FAILURE); } ret = listen(sfd,BACKLOG); if (ret == -1){ perror("[ERROR] Failed to listen."); exit(EXIT_FAILURE); }
cfd = accept(sfd,(struct sockaddr *)&cli_addr,&len); if (ret == -1){ perror("[ERROR] Failed to accpet."); exit(EXIT_FAILURE); }
printf("ip : %s port :%d\n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port)); for(;;){ length = 0; total_received = 0; rbytes = recv(cfd,&length,4,0); if (rbytes == -1){ perror("[ERROR] Failed to recv."); exit(EXIT_FAILURE); } for(;;){
rbytes = recv(cfd,buffer + total_received,length - total_received,0); if (rbytes == -1){ perror("[ERROR] Failed to recv."); exit(EXIT_FAILURE); }else if (rbytes == 0){ printf("The client has been shutdown.\n"); }else if (rbytes > 0){ total_received += rbytes; if (total_received == length) break; } } printf("buffer : %s\n",buffer); sleep(1); }
close(sfd); <span class="bd-box"><h-char class="bd bd-beg"><h-inner>。</h-inner></h-char></span> return 0; }
|