我在获取用户字符串作为解析的变量传递时遇到问题,以便将每个单独的单词存储在它自己的变量中。以下是我对此的尝试。client.c
中的程序的另一部分正在请求用户输入。一旦在客户端“foo 123 anwhere blah”中输入字符串,它就会作为变量buf
传递到下面的服务器程序中。一旦有了它,我就将其复制到temp
中,这样我就不会损坏原始字符串(至少在测试时如此)。然后,我针对该变量运行strctok
,以尝试解析出每个单词。
如果目前我处在正确的轨道上,它将可以毫无错误地进行编译并运行。但是,当我从客户端向其发送数据时,会出现“分段错误(核心已转储)”。
谁能阐明为什么?
/*
* server.c
*/
#include <stdio.h>
#include <iostream>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <cstring>
#include <cstdlib>
using namespace std;
#define SERVER_PORT 1617
#define MAX_PENDING 5
#define MAX_LINE 256
int main(int argc, char **argv) {
struct sockaddr_in sin;
socklen_t addrlen;
char buf[MAX_LINE];
int len;
int s;
int new_s;
char *temp;
char fname[32], lname[32], city[32], zip[32], country[32];
/* build address data structure */
bzero((char *)&sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons (SERVER_PORT);
/* setup passive open */
if (( s = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
if ((bind(s, (struct sockaddr *) &sin, sizeof(sin))) < 0) {
perror("bind");
exit(1);
}
listen (s, MAX_PENDING);
addrlen = sizeof(sin);
cout << "The server is up, waiting for connection" << endl;
/* wait for connection, then receive and print text */
while (1) {
if ((new_s = accept(s, (struct sockaddr *)&sin, &addrlen)) < 0) {
perror("accept");
exit(1);
}
cout << "new connection from " << inet_ntoa(sin.sin_addr) << endl;
while (len = recv(new_s, buf, sizeof(buf), 0)) {
temp = buf;
strcpy(fname, strtok(buf , " "));
strcpy(lname, strtok(NULL, " "));
strcpy(city , strtok(NULL, " "));
strcpy(zip , strtok(NULL, " "));
strcpy(country, strtok(NULL, " "));
printf("%s\n", fname);
printf("%s\n", lname);
printf("%s\n", city);
printf("%s\n", zip);
printf("%s\n", country);
/* send (new_s, temp, strlen(temp) + 1, 0); */
}
close(new_s);
}
}
根据要求,在gcc上使用-g选项:
server.c:6:20: error: iostream: No such file or directory
server.c:13:19: error: cstring: No such file or directory
server.c:14:19: error: cstdlib: No such file or directory
server.c:16: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before ânamespaceâ
server.c: In function âmainâ:
server.c:42: warning: incompatible implicit declaration of built-in function âexitâ
server.c:47: warning: incompatible implicit declaration of built-in function âexitâ
server.c:53: error: âcoutâ undeclared (first use in this function)
server.c:53: error: (Each undeclared identifier is reported only once
server.c:53: error: for each function it appears in.)
server.c:53: error: âendlâ undeclared (first use in this function)
server.c:59: warning: incompatible implicit declaration of built-in function âexitâ
server.c:65: warning: incompatible implicit declaration of built-in function âmemcpyâ
server.c:66: warning: incompatible implicit declaration of built-in function âstrcpyâ
server.c:66: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast
server.c:66: note: expected âconst char *â but argument is of type âintâ
server.c:67: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast
server.c:67: note: expected âconst char *â but argument is of type âintâ
server.c:68: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast
server.c:68: note: expected âconst char *â but argument is of type âintâ
server.c:69: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast
server.c:69: note: expected âconst char *â but argument is of type âintâ
server.c:70: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast
server.c:70: note: expected âconst char *â but argument is of type âintâ
最佳答案
将strcpy替换为strncpy。并根据复制的缓冲区长度在strncpy中使用正确的长度。
另外,您不检查strtok的返回值。由于strtok可以返回NULL,因此您正在尝试执行类似strcpy(str,null)的操作。