服务器连接示例首先是c(Segmentation fault(`core'generado))

时间:2015-08-24 01:55:28

标签: c

我真的很感激是否可以帮助我解决这个问题:

我是从第473页第一页开始运行一个例子。贝娄是我的代码。我正在使用telnet来运行它,但是我收到了这个错误:分段错误(`core'generado)。有什么建议?感谢。

#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
int main (int argc, char *argv[])
{
    char * advice[] = {
    "Take smaller bites\r\n,"
    "One word: inappropiate\r\n"
    };

    int listener_d = socket (PF_INET, SOCK_STREAM, 0);
    if (listener_d == -1)
    {
        printf("Can't open socket");
    }
    else
    {   
            struct sockaddr_in name;
            name.sin_family = PF_INET;
            name.sin_port = (in_port_t)htons(30000);
            name.sin_addr.s_addr = htonl(INADDR_ANY);
            int c = bind(listener_d, (struct sockaddr *) &name, sizeof(name));

            if (c == -1)
            {
                printf("Can't bind to socket");
            }
            else
            {
                if((listen(listener_d,10)) == -1)
                {
                    printf("Can't Listen"); 
                }
                else
                {
                    puts("Waiting for connection");

                    while(1)
                    {
                        struct sockaddr_storage client_addr;
                        unsigned int address_size = sizeof(client_addr);
                        int connect_d = accept(listener_d, (struct sockaddr *)&client_addr, &address_size);
                        if (connect_d == -1)
                        {
                            printf("Can't open secondary socket");
                        }
                        else
                        {
                            char *msg = advice[rand() % 2];
                            if ((send(connect_d, msg, strlen(msg), 0))== -1);
                                printf("Can't send the message");
                            close(connect_d);
                        }                   
                    }       
                }                   
            }
        }           
    return 0;
}

错误的屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:3)

如有疑问,请通过以下方式在调试模式下编译程序:

gcc infile.c -O0 -ggdb

现在,使用GDB启动您的程序:

gdb ./a.out
RUN

从调试日志中,我们发现它看起来存在访问advice字符串的问题:

GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./a.out...done.
(gdb) run
Starting program: /home/owner/tmp/a.out 
Waiting for connection

Program received signal SIGSEGV, Segmentation fault.
__strlen_sse2_bsf () at ../sysdeps/i386/i686/multiarch/strlen-sse2-bsf.S:50
50  ../sysdeps/i386/i686/multiarch/strlen-sse2-bsf.S: No such file or directory.
(gdb) bt full
#0  __strlen_sse2_bsf () at ../sysdeps/i386/i686/multiarch/strlen-sse2-bsf.S:50
No locals.
#1  0x080487d6 in main (argc=1, argv=0xbffff4d4) at test.c:51
        msg = 0x10 <error: Cannot access memory at address 0x10>
        client_addr = {ss_family = 2, __ss_align = 16777343, 
          __ss_padding = '\000' <repeats 12 times>, "t\364\377\277\350\363\377\277\340\363\377\277P\203\004\b8\371\377\267\000\000\000\000\277\000\000\000F\302\351\267\377\377\377\377\016\364\377\277\370;\341\267s\242\343\267\000\000\000\000N0\264,\001\000\000\000a\204\004\b\253\366\377\277/\000\000\000\000\240\004\b\202\210\004\b\001\000\000\000\324\364\377\277\334\364\377\277-\244\343\267\304\023\373\267\000\360\377\267;\210\004\b"}
        address_size = 16
        connect_d = 4
        name = {sin_family = 2, sin_port = 12405, sin_addr = {s_addr = 0}, sin_zero = "\000\000\000\000\000\000\000"}
        c = 0
        advice = {0x80488c0 "Take smaller bites\r\n,One word: inappropiate\r\n"}
        listener_d = 3
(gdb) quit
A debugging session is active.

    Inferior 1 [process 14423] will be killed.

Quit anyway? (y or n) y

经过仔细检查,你有错别字和错误的逗号:

您对advice的声明和定义应如此:

char **advice = (char*[]) {
    "Take smaller bites\r\n",
    "One word: inappropiate\r\n"
};

您不小心将逗号放在"Take smaller bites"字符串中,因此您的字符串数组只有一个字符串元素,但您的模数算术%2意味着有时您会尝试访问非字符串-existent(即:out of bounds)数组元素。

原因是因为C将空格分隔的字符串文字视为单个字符串,因此:

"This is a string"  "And this too"

连接到单个字符串This is a stringAnd this too