如何解决使用strcmp从服务器缓冲区读取字符串
我正在尝试构造一个tcp服务器以从套接字缓冲区读取字符串。客户端的缓冲区,使用以下代码进行操作:
n = 0;
do {
sign = getchar();
if(n >= 253) break;
if (sign =='\n') break;
buffer[n ++] = sign;
} while (1);
buffer [n] = 0;
bzero(buffer,255);
n = read(sock,buffer,255);
printf("Here is the message: %s\n",buffer);
到目前为止,一切正常。
for(int k = 0; k < sizeof(buffer); k++) {
string[i ++] = buffer[k];
if (buffer[k] == 0 ) break;
if (buffer[k] == '\n' ) break;
}
printf("Here is the string: %s\n",string);
char **dict = dictionary();
cmp(string,dict);
free_dictionary(dict);
exit(0);
}
else close(newsockfd);
char **dictionary() // dictionary code
{
char **dict = (char**)malloc(sizeof(char*) * 5);
int i = 0;
for(i = 0; i < 5; i++)
dict[i] = (char*)malloc(sizeof(char) * 255);
strcpy(dict[0],"mark");
strcpy(dict[1],"ala");
strcpy(dict[2],"wojtek");
strcpy(dict[3],"tom");
strcpy(dict[4],"john");
return (dict);
}
void free_dictionary(char **dict)
{
for (int i = 0; i < 5; i++)
free(dict[i]);
free(dict);
}
int cmp(char *s1,char *s2[5]) //comparison function
{
int k = 0;
int n = 0;
do {
if (strcmp(s1,s2[k]) == 0)
n++;
k++;
} while(k < 5);
if (n > 0)
printf("Found %d",n);
else
printf("nothing found %d\n",n);
return 0;
}
一切似乎都可以,但事实是,strcmp似乎不起作用。包括
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。