为什么我在我的C服务器程序得到一个分段错误(但只是有时)? [英] Why am I getting a segmentation fault in my C server program (but only sometimes)?

查看:174
本文介绍了为什么我在我的C服务器程序得到一个分段错误(但只是有时)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我想为了衡量不同大小的TCP报文在LAN上往返时间(我做的时间客户端)写一个简单的客户端/服务器应用程序。该方案对于小数据包大小(> 1000个字节),但我结束了一个段故障工作正常。对于较大的幅度(10KB或更高)的输入错误11

Right now, I'm trying to write a simple client/server application in order to measure the round trip time on a LAN for TCP messages of various sizes (I'm doing the timing client side). The program works fine for small packet sizes (> 1000 bytes) but I end up with a segmentation fault: 11 error for inputs of larger magnitude (10KB or greater).

int main() 
{ 
    struct sockaddr_in sin; 
    char buf[MAX_LINE]; 
    int len; 
    int s, new_s; 
    /* 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( PF_INET, SOCK_STREAM, 0)) < 0) { 
        perror("tcp program: socket"); 
        exit(1); 
    } 
    if (( bind(s, (struct sockaddr *)& sin, sizeof(sin))) < 0) { 
         perror("tcp program: bind"); 
         exit( 1); 
 } 
    listen(s, MAX_PENDING); 
    /* wait for connection, then receive and print text */
     while(1) { 
        socklen_t lent = (unsigned int)&len;
        if ((new_s = accept(s, (struct sockaddr *)& sin, &lent)) < 0) { 
            perror("tcp program: accept"); 
            exit( 1); 
        }


        while ((len = recv(new_s, buf, sizeof(buf), 0))){ 
            char msg[len];
            send( new_s, msg, len, 0); //echo message of same length as received message
        }
        close(new_s); 
      }      
}

此外,我们的目标是测量RTT,所以我想在客户端发送消息,上述服务器接收,然后发回等同大小的消息。我还希望在服务器继续纺纱,以便客户端可以迭代地运行,发送1KB,10KB,... 1000KB,的消息等。然而,这样的迭代通常导致一个分段错误。

Again, the goal was to measure RTT, so I wanted the client to send a message, the above server to receive it, then send back a message of equivalent size. I also wanted the server to continue spinning so that the client could run iteratively, sending messages of 1KB, 10KB,...1000KB, etc. However, such iterations usually result in a segmentation fault.

奇怪的是,如果配置我的客户端运行,例如,一个12KB的消息发送,服务器执行罚款,并继续运行。如果我等待几秒钟,我甚至可以重复调用我的客户端和服务器最多保留。但是,如果我跑快速连续地发送单条消息,我最终再次段错误。

Oddly enough, if I configure my client to run, for example, a single 12KB message send, the server does fine, and continues to run. And if I wait a couple of seconds, I can even repeatedly call my client and the server keeps up. But if I run the single message send in rapid succession, I end up with the segfault again.

任何想法?我提前道歉的风格或格式的任何基本的错误。这是我第一次真正涉足超越世界你好C语言。

Any ideas? I apologize in advance for any elementary errors in style or format. This is my first real foray into the C language beyond "hello world".

谢谢!

推荐答案

我不知道这是否是code,这是错误的只是一部分,但这是错误的:

I don't know if this is the only part of the code that is wrong, but this is wrong:

while ((len = recv(new_s, buf, sizeof(buf), 0)))

请阅读 recv来手册页(),特别是(强调)...

Please read the man page for recv(), in particular (emphasis added)...

这些调用返回接收的字节数,或-1,如果发生错误。的返回值为0时,对已经执行有序关机。

These calls return the number of bytes received, or -1 if an error occurred. The return value will be 0 when the peer has performed an orderly shutdown.

我们知道,网络是的不可靠的,的,这是很常见的的recv()和朋友返回错误。

We know that networks are unreliable, and it is fairly common for recv() and friends to return errors.

此外,在C变长数组是一个相当危险的结构,因为他们在栈上进行动态分配。他们基本上的alloca()伪装,我们知道有多危险的alloca()是。因此,此位:

Additionally, variable-length arrays in C are a fairly dangerous construct, because they perform dynamic allocation on the stack. They're basically alloca() in disguise, and we know how dangerous alloca() is. So this bit:

char msg[len]; // serious problems unless we have good bounds for len

这篇关于为什么我在我的C服务器程序得到一个分段错误(但只是有时)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆