Array of chars in socket client and socket server written in ANSI C

I'm trying to write simple client and server based on sockets in C. The client sends the size of the char array (including last cell for '\0') and then array of chars. Server gets the size and tries to allocate memory for the array of chars from the client. After this, the server looks for a space and copy the characters from index 0 to space, pastes it at the end of the array and sends it back to the client (if there is no space, then the server sends doubled array). I've noticed that sometimes I'm getting -- |00| |02| -- char at the end of the received array from the server. I've run the server and the client using valgrind and it showed that problem is in the server. Code below. Client: #include #include #include #include #include #include #include #include int main () { int sockfd; socklen_t len; struct sockaddr_in address; int result; char ch; char *string; int i; sockfd = socket (AF_INET, SOCK_STREAM, 0); address.sin_family = AF_INET; address.sin_addr.s_addr = inet_addr ("127.0.0.1"); address.sin_port = htons (9734); len = sizeof (address); result = connect (sockfd, (struct sockaddr *) &address, len); if (result == -1) { perror ("oops: netclient"); exit (1); } string = (char*)malloc(sizeof(char)); for (i =0; ch = getchar(); i++) { string = (char*)realloc(string, (i+1)*sizeof(char)); if(ch != '\n') string[i] = ch; else if (ch == '\n') { string[i]='\0'; break; } } printf("%s\n", string); printf("%d\n", i); i=i+1; write (sockfd, &i, 4); write (sockfd, string, i); read (sockfd, &i, 4); string = (char*)realloc(string, i*sizeof(char)); read (sockfd, string, i); printf("String recieved: %s\n", string); close (sockfd); free(string); exit (0); } And server: #include #include #include #include #include #include #include #include int main () { int server_sockfd, client_sockfd; socklen_t server_len, client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; server_sockfd = socket (AF_INET, SOCK_STREAM, 0); server_address.sin_family = AF_INET; /*ipv4*/ server_address.sin_addr.s_addr = htonl (INADDR_ANY); server_address.sin_port = htons (9734); server_len = sizeof (server_address); bind (server_sockfd, (struct sockaddr *) &server_address, server_len); listen (server_sockfd, 5); signal (SIGCHLD, SIG_IGN); while (1) { char *string; int i, j, k, l=0; /*variables to iteration*/ printf ("server waiting\n"); client_len = sizeof (client_address); client_sockfd = accept (server_sockfd, (struct sockaddr *) &client_address, &client_len); if (fork () == 0) { read (client_sockfd, &i, 4); printf("recieved int %d\n", i); string = (char*)malloc(sizeof(char) * (i)); read (client_sockfd, string, i); printf("\nSTRING recieved: %s \n", string); for(j=0; string[j]!='\0'; j++) { if(string[j] == ' ') { i = i+j; /*size to enlarge*/ break; } } /*sending new size*/ write(client_sockfd, &i, 4); /*enlarge the string*/ string = (char*)realloc(string, i*sizeof(char)); for (k = i-j-1; k

以上就是Array of chars in socket client and socket server written in ANSI C的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » CSS3 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏