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