gpt4 book ai didi

c++ - Linux 套接字 : Segmentation error

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:24:01 27 4
gpt4 key购买 nike

这里是 Linux n00b。所以大约一个月前,我安装了 emacs 和 gcc/g++ 编译器,并开始了编程。我在网上找到了一些回声服务器程序的代码,复制并编译它来测试网络功能。它编译了,但是当我尝试运行它时,我收到了错误消息:Segmentation fault(core dumped)。当我仔细查看调试器详细信息时,发现是“fwrite()”函数中的一个错误。我在编译和创建输出文件时将代码链接到库 libstdc++.a 所以它确实让我想知道实际库函数中是否存在一些严重错误,我需要返回,找到函数 .c 源代码,然后将它们添加到 header 中以使其工作。代码贴在下面。还有其他人遇到过这个问题吗?

#include <sys-socket.h>       /*  socket definitions        */
#include <sys-types.h> /* socket types */
#include <netinet-in.h> /* inet (3) functions */
#include <unistd.h> /* misc. UNIX functions */
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <ether.h>
#include <string.h>


/* Global constants */

#define ECHO_PORT 2002
#define MAX_LINE 1000
#define LISTENQ 5


ssize_t Readline(int sockd, char *vptr,size_t maxlen) {
ssize_t n, rc;
char* c;
msghdr* buffer;

buffer->msg_iov->iov_base = vptr;
buffer->msg_iov->iov_len = maxlen;

for ( n = 1; n < maxlen; n++ ) {

if ( (rc = recvmsg(sockd,buffer, 1)) == 1 ) {
c = buffer->msg_iov->iov_base++;
if (*c == '\n' )
break;
}
else if ( rc == 0 ) {
if ( n == 1 )
return 0;
else
break;
}
else {
if (rc < 0 )
continue;
return -1;
}
}

buffer->msg_iov->iov_base = 0;
return n;
}


/* Write a line to a socket */

ssize_t Writeline(int sockd, char *vptr) {
msghdr *buffer;


buffer->msg_iov->iov_base = vptr;
size_t nleft = buffer->msg_iov->iov_len;
ssize_t nwritten;

while ( nleft > 0 ) {
if ( (nwritten = sendmsg(sockd, buffer, nleft)) < 0 ) {
return -1;
}
nleft -= nwritten;
buffer += nwritten;
}

return nwritten;
}

int main(int argc, char *argv[]) {
int list_s; /* listening socket */
int conn_s; /* connection socket */
short int port; /* port number */
struct sockaddr_in servaddr; /* socket address structure */
char *endptr; /* for strtol() */
char buffer[MAX_LINE];




port = 5000;


/* Create the listening socket */

if ( (list_s = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) {
fprintf(stderr, "ECHOSERV: Error creating listening socket.\n");
exit(EXIT_FAILURE);
}


/* Set all bytes in socket address structure to
zero, and fill in the relevant data members */

memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(port);


/* Bind our socket addresss to the
listening socket, and call listen() */

if ( bind(list_s, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0 ) {
fprintf(stderr, "ECHOSERV: Error calling bind()\n");
exit(EXIT_FAILURE);
}

if ( listen(list_s, LISTENQ) < 0 ) {
fprintf(stderr, "ECHOSERV: Error calling listen()\n");
exit(EXIT_FAILURE);
}


/* Enter an infinite loop to respond
to client requests and echo input */

while ( 1 ) {

/* Wait for a connection, then accept() it */

if ( (conn_s = accept(list_s, NULL, NULL) ) < 0 ) {
fprintf(stderr, "ECHOSERV: Error calling accept()\n");
exit(EXIT_FAILURE);
}


/* Retrieve an input line from the connected socket
then simply write it back to the same socket. */

Readline(conn_s, buffer, MAX_LINE-1);
Writeline(conn_s, buffer);


/* Close the connected socket */

if ( shutdown(conn_s,0) < 0 ) {
fprintf(stderr, "ECHOSERV: Error calling close()\n");
exit(EXIT_FAILURE);
}
}
}

最佳答案

ssize_t Writeline(int sockd, char *vptr) {
msghdr *buffer;


buffer->msg_iov->iov_base = vptr;

您的指针缓冲区未初始化。您可能会查看此代码片段以正确执行此操作:

      /* This structure contains parameter information for sendmsg.   */
struct msghdr mh;

/* The message header contains parameters for sendmsg. */
mh.msg_name = (caddr_t) &dest;
mh.msg_namelen = sizeof(dest);
mh.msg_iov = iov;
mh.msg_iovlen = 3;
mh.msg_accrights = NULL; /* irrelevant to AF_INET */
mh.msg_accrightslen = 0; /* irrelevant to AF_INET */

rc = sendmsg(s, &mh, 0); /* no flags used */
if (rc == -1) {
perror("sendmsg failed");
return -1;
}
return 0;
}

sendmsg

关于c++ - Linux 套接字 : Segmentation error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18430722/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com