現在、echoサーバを作成しています。
要件は、長い文字列が送られていた時も、リストを用いてちゃんと返すというものです。
自分でここまでプログラムを書いたのですが、以下のようにエラーが起きます。
どこに原因があるのでしょうか。
c
1master@LAPTOP-4FABUBT7:~$ ./a.out 3875 2free(): invalid pointer 3Aborted (core dumped) 4 5master@LAPTOP-4FABUBT7:~$ telnet localhost 3875 6Trying 127.0.0.1... 7Connected to localhost. 8Escape character is '^]'. 9hello 10OK: hello 11 12 helooooooooooooooooooooooooo 13Connection closed by foreign host. 14
c
1#include <sys/types.h> 2#include <sys/socket.h> 3#include <netinet/in.h> 4#include <netdb.h> 5#include <stdio.h> 6#include <errno.h> 7#include <err.h> 8#include <unistd.h> 9#include <string.h> 10#include <stdlib.h> 11#include <arpa/inet.h> 12 13#define SIZE 2048 14#define L_SIZE 6//リストサイズ 15 16/* リスト */ 17struct List { 18 char*str; 19 char *save; 20 int len; 21 int head; 22}; 23 24/*リスト初期化*/ 25void init_list(struct List*list) { 26 list->len = L_SIZE; 27 list->save = (char*)malloc(sizeof(char)*L_SIZE); 28 list->head = 0; 29 list->str=(char*)malloc(sizeof(char)*L_SIZE); 30} 31 32/*ソケット準備*/ 33int socket_set(char*argv, struct addrinfo *hints, struct addrinfo **res) { 34 35 struct sockaddr_storage from; 36 socklen_t fromlen; 37 int error; 38 39 hints->ai_socktype = SOCK_STREAM; 40 hints->ai_family = AF_INET; 41 hints->ai_flags = AI_PASSIVE; 42 43 error = getaddrinfo(NULL, argv, hints, res); 44 if (error) { 45 errx(1, "%s", gai_strerror(error)); 46 return 1; 47 } 48 49 return socket((*res)->ai_family, (*res)->ai_socktype, (*res)->ai_protocol); 50 51} 52 53/*拡張*/ 54int expand(struct List**list) { 55 56 char*new_cell; 57 int i; 58 (*list)->len *= 2; 59 if ((new_cell = (char*)malloc(sizeof(char)*((*list)->len))) == NULL) { 60 return 0; 61 } 62 for (i = 0; i < (*list)->head; i++) { 63 new_cell[i] = (*list)->save[i]; 64 } 65 for (i = 0; i < (*list)->len; i++) { 66 new_cell[(*list)->head + i] = (*list)->str[i]; 67 } 68 (*list)->head = (*list)->head * 2 + L_SIZE; 69 free((*list)->str); 70 free((*list)->save); 71 (*list)->save = new_cell; 72 (*list)->str = (char*)malloc(sizeof(char)*((*list)->len)); 73 74 return 1; 75} 76void Read_Write(int *i, int *n, struct List*buf0, struct List*buf1, fd_set rfd0) { 77 78 int k; 79 int l; 80 while ((*n = read(*i, buf0->str, buf0->len)) >= buf0->len) { 81 if ((buf0->str[*n - 2] == 13) || (buf0->str[*n - 2] == 10)) { 82 break; 83 } 84 expand(&buf0); 85 expand(&buf1); 86 } 87 if (*n <= L_SIZE) 88 { 89 if (*n >= 2) { 90 buf0->str[*n - 2] = '\0'; 91 for (k = 0; k < L_SIZE; k++) { 92 buf0->save[k + buf0->head] = buf0->str[k]; 93 } 94 } 95 else if (*n == 1) { 96 buf0->save[buf0->head] = buf0->str[k]; 97 } 98 else { 99 buf0->str[*n - 2] = '\0'; 100 expand(&buf0); 101 expand(&buf1); 102 } 103 } 104 if (strncmp("quit", buf0->str,4) == 0) { 105 close(*i); 106 FD_CLR(*i, &rfd0); 107 } 108 else { 109 sprintf(buf1->str, "OK: %s\n", buf0->save); 110 l = write(*i, buf1->str, strlen(buf1->str) + 1); 111 if (l < 0) { 112 perror("write"); 113 exit(1); 114 } 115 } 116} 117int main(int argc, char *argv[]) { 118 119 struct addrinfo hints, *res; 120 struct sockaddr_storage from; 121 socklen_t fromlen; 122 int i, n, ls, s, test; 123 i = n = ls = s = test = 0; 124 125 fd_set rfd, rfd0; 126 char buf[2048], buf2[2048]; 127 128 struct List list0, list1; 129 130 if (argc != 2) { 131 fprintf(stderr, "usage: %s port\n", argv[0]); 132 exit(1); 133 } 134 memset(&hints, 0, sizeof(hints)); 135 init_list(&list0); 136 init_list(&list1); 137 138 while (res != 0) { 139 s = socket_set(argv[1], &hints, &res); 140 if (s < 0) { 141 fprintf(stderr,"socket_erorr\n"); 142 res = res->ai_next; 143 } 144 else { 145 break; 146 } 147 } 148 149 if (bind(s, res->ai_addr, res->ai_addrlen) < 0) { 150 perror("bind"); 151 close(s); 152 return -1; 153 }; 154 155 if (listen(s, 5) < 0) { 156 perror("listen"); 157 close(s); 158 return -1; 159 } 160 161 FD_ZERO(&rfd0); 162 FD_SET(s, &rfd0); 163 while (1) { 164 rfd = rfd0; 165 if((select(FD_SETSIZE,&rfd,NULL,NULL,NULL))<0){ 166 perror("select"); 167 exit(1); 168 } 169 for (i = 0; i < FD_SETSIZE; i++) { 170 if (FD_ISSET(i, &rfd)) { 171 if ((ls = accept(i, NULL, NULL)) > 0) { 172 FD_SET(ls, &rfd0); 173 } 174 else { 175 Read_Write(&i,&n,&list0,&list1,rfd0); 176 free(list0.str); 177 178 free(list0.save); 179 free(list1.str); 180 free(list1.save); 181 init_list(&list0); 182 init_list(&list1); 183 } 184 } 185 } 186 } 187 close(s); 188} 189
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/07 18:28