中止陷阱:6并在connect()函数上发出

时间:2016-10-02 20:04:09

标签: c connect abort

我有两个主要问题,即使整个下午都在尝试修复它们,我还没有找到解决方案。

我正在服务器上创建电话簿,客户可以要求服务器搜索某人的电话号码或添加联系人(姓名,姓氏和电话号码)(如果不存在)。 使用该服务的每个人都需要具有添加/搜索的权限,因此有两个文件包含允许的人的昵称和密码。

/*  login  */
printf("Welcome to the telephone book!\n Insert add to add a new contact or search to look for the telephone number.\n");
fgets(tmp, MAX_LINE, stdin);
strtok(tmp, "\n"); 
strcpy(op,tmp);    // store the operation for further purposes
strcpy(buffer, "Y "); // Y = YES. Here the concatenation in the buffer starts: login op name password
strcat(buffer, tmp);
strcat(buffer, " ");

/*  check correctness of operation  */
if ( strcmp(tmp, "add") != 0 && strcmp(tmp, "search") != 0 ) {
    printf("L'operazione inserita non corrisponde a quelle indicate.\n");
    exit(EXIT_FAILURE);
}

/*  username  */
printf("Inserisci il tuo nome: ");
fgets(tmp, MAX_LINE, stdin);
check(tmp); // validating name
strtok(tmp, "\n");
strcat(buffer, tmp);
strcat(buffer, " ");

/*  password  */
printf("Inserisci la tua password: ");
fgets(tmp, MAX_LINE, stdin);
check(tmp); // validating password
strtok(tmp, "\n");
strcat(buffer,tmp);
strcat(buffer, " ");


/*  once everything is ready, connect() to the remote echo server  */
if ( connect(conn_s, (struct sockaddr *) &servaddr, sizeof(servaddr) ) < 0 ) {
    printf("client: errore durante la connect.\n");
    exit(EXIT_FAILURE);
}     

/*  Send buffer to echo server */
Writeline(conn_s, buffer, strlen(buffer));
memset(buffer, 0, sizeof(char)*(strlen(buffer)+1));

我连接了一些参数,我打开与服务器的连接并将它们发送到服务器。

我的问题是:

  • 如果我将搜索作为输入写入中止陷阱:6发生,我不明白为什么
  • 如果我写add作为输入我没有错误,但函数connect()无法正常工作,我无法连接到服务器。

你们有人可以帮我理解如何解决这个问题吗? 提前谢谢。

0 个答案:

没有答案
相关问题