小威
...

  1.并发服务器与客户端 (UDP)

  1 //client.c
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 #include <unistd.h>
  6 #include <netinet/in.h>
  7 #include <fcntl.h>
  8 
  9 #define MAX 10
 10 #define MAXLINE 18
 11 #define SERV_PORT 8000
 12 
 13 int main(int argc, char *argv[])
 14 {
 15     struct sockaddr_in servaddr;
 16     int sockfd = 0, n = 0, number = 0;
 17     char buf[MAXLINE] = {0};
 18     char str[INET_ADDRSTRLEN] = {0};
 19     char find[MAXLINE] = {0};
 20     socklen_t servaddr_len;
 21     int len = 0, fr = 0, fw = 0, data_len = 0, read_len = 0;
 22 
 23     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
 24 
 25     bzero(&servaddr, sizeof(servaddr));
 26     servaddr.sin_family = AF_INET;
 27     inet_pton(AF_INET, argv[1], &servaddr.sin_addr);
 28     servaddr.sin_port = htons(SERV_PORT);
 29 
 30     while((fgets(buf, MAXLINE, stdin) != NULL))
 31     {
 32         if('\n' == buf[strlen(buf) - 1]) {
 33             buf[strlen(buf) - 1] = '\0';
 34         }
 35 
 36         int fr = open(buf, O_RDONLY);
 37         bzero(find, sizeof(find));
 38         len = strlen(buf);
 39         number = -1;
 40         //*((int *)(find + 0)) = number;
 41         memcpy(find, &number, 4);
 42         memcpy(&find[4], &len, 4);
 43         memcpy(&find[8], buf, len);
 44         n = sendto(sockfd, find, MAXLINE, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
 45         if(-1 == n) {
 46             perror("sendto");
 47             exit(1);
 48         }
 49         int i = 0;
 50         for(i = 0; i != MAXLINE; i ++)
 51         {
 52             printf("%x", find[i]);
 53         }
 54         printf("\n");
 55 
 56         n = recvfrom(sockfd, &fw, 4, 0, NULL, 0);
 57         printf("fw = %d\n", fw);
 58         //---------------------------------------------------------
 59         data_len = lseek(fr, 0, SEEK_END);
 60         lseek(fr, 0, SEEK_SET);
 61         printf("data_len = %d\n", data_len);
 62         //==========================================================
 63         while(data_len > 0)
 64         {
 65             bzero(find, sizeof(find));
 66             //bzero(buf, sizeof(buf));
 67             //memcpy(find, &fr, 4);
 68             *((int *)(find)) = fw;
 69             if(data_len < MAX) {
 70                 read_len = read(fr, buf, data_len);
 71                 //memcpy(&find[4], &read_len, 4);
 72                 *((int *)(find + 4)) = read_len;
 73                 memcpy(&find[8], buf, read_len);
 74             }
 75             else {
 76                 read_len = read(fr, buf, MAX);
 77                 //memcpy(&find[4], (char *)MAX, 4);
 78                 *((int *)(find + 4)) = MAX;
 79                 memcpy(&find[8], buf, MAX);
 80             }
 81             printf("read_len = %d\n", read_len);
 82             printf("%s\n", &find[8]);
 83             n = sendto(sockfd, find, MAXLINE, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
 84             data_len -= read_len;
 85             printf("%d = data_len\n", data_len);
 86         }
 87         //===============================================================
 88         bzero(find, sizeof(find));
 89         *((int *)(find)) = fw;
 90         n = sendto(sockfd, find, MAXLINE, 0, (struct sockaddr *)&servaddr, sizeof(servaddr));
 91         //======================================================================================
 92         bzero(buf, sizeof(buf));
 93         close(fr);
 94 #if 0
 95         n = recvfrom(sockfd, find, MAXLINE, 0, NULL, 0);
 96         if(-1 == n) {
 97             perror("recvform error");
 98             exit(1);
 99         }
100         write(STDOUT_FILENO, find, n);
101 #endif
102     }
103 
104     close(sockfd);
105 
106     return 0;
107 }
client
 1 //server.c
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 #include <fcntl.h>
 6 #include <netinet/in.h>
 7 
 8 #define MAX 10
 9 #define MAXLINE 18
10 #define SERV_PORT 8000
11 
12 int main(int argc, char *argv[])
13 {
14     struct sockaddr_in servaddr, cliaddr;
15     socklen_t cliaddr_len;
16     int sockfd = 0;
17     char buf[MAXLINE] = {0};
18     char itemp[MAXLINE] = {0};
19     char filename[MAX] = {0};
20     char str[INET_ADDRSTRLEN] = {0};
21     int i = 0, n = 0, file_len = 0, data_len = 0, fw = 0, write_len = 0, f_get = 0;
22 
23     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
24 
25     bzero(&servaddr, sizeof(servaddr));
26     servaddr.sin_family = AF_INET;
27     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
28     servaddr.sin_port = htons(SERV_PORT);
29 
30     bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
31 
32     printf("Accepting connections ...\n");
33     cliaddr_len = sizeof(cliaddr);
34 
35     while(1)
36     {
37         //--------------------------------------------------
38         bzero(buf, sizeof(buf));
39         n = recvfrom(sockfd, buf, MAXLINE, 0, (struct sockaddr *)&cliaddr, &cliaddr_len);
40         printf("received from %s at PORT %d\n", (char *)inet_ntop(AF_INET, &cliaddr.sin_addr, str, sizeof(str)), ntohs(cliaddr.sin_port));
41         if(-1 == n) {
42             perror("recvfrom error");
43             exit(1);
44         }
45         f_get = *((int *)(buf)); 
46         printf("fw == %d\n", f_get);
47         if(-1 == f_get) {
48             memcpy(&file_len, &buf[4], 4);
49             printf("filename_len = %d\n", file_len);
50             memcpy(filename, &buf[8], file_len);
51             printf("filename = %s\n", filename);
52             fw = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644);
53             if(fw < 0) {
54                 perror("open fw");
55                 exit(1);
56             } 
57             n = sendto(sockfd, &fw, 4, 0, (struct sockaddr *)&cliaddr, sizeof(cliaddr));
58         }
59         else if(f_get == fw){
60             write_len = *((int *)(buf + 4));
61             printf("%d\n", write_len);
62             if(0 == write_len) {
63                 close(f_get);
64             }
65             else {
66                 write(f_get, &buf[8], write_len);
67             }
68         }
69         bzero(filename, sizeof(filename));
70         bzero(buf, sizeof(buf));
71     }
72 
73     return 0;
74 }
server

   2.函数原型 : #include <sys/socket.h>  #include <sys/types.h>

        ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);  //发送数据报

        ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);    //接受数据报

posted on 2013-09-22 18:24  小威_  阅读(192)  评论(0编辑  收藏  举报