ç聊天程序得到一个错误段错误(核心转储) [英] C chat program get an error Segmentation fault (core dumped)

查看:158
本文介绍了ç聊天程序得到一个错误段错误(核心转储)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个客户端和C语言服务器之间的聊天程序。客户端将连接到服务器,发送到它的消息。然后,服务器将响应该meesage。

I am building a chat program between a client and a server in C language. Client will connect to server, sends to it a message. Then server will response that meesage.

如果有另一个客户端连接到服务器时,新的连接将创建一个新的线程。所以我用pthreads.h中建立一个多线程聊天程序C.请参阅下面的server.c和client.c code,以获得更多的细节。

If there is another client connects to server, the new connection will be created a new thread. So I use pthread.h to build a multi-threaded chat program in C. Please see the server.c and client.c code below to get more details.

server.c

#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define PORT 7778

void error(char *msg){
    perror(msg);
    exit(1);
}

void *clientHandler(void*);

int main(){
    printf("INFO enter the main()\n");
    int sockfd, newsockfd, clilen;
    char buffer[256];
    struct sockaddr_in serv_addr, cli_addr;
    int n, threadID;
    pthread_t interrupt;

    printf("INFO before calling socket()\n");
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
        printf("INFO after calling socket()\n");
    if(sockfd < 0)
        error("ERROR opening socket\n");

    printf("INFO before calling bzero()\n");
    bzero((char*) &serv_addr, sizeof(serv_addr));
    printf("INFO after calling socket()\n");

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(PORT);
    printf("INFO after assigning Internet info\n");

    if(bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0)    
    {
        error("ERROR on binding\n");
        return 0;
    }
    printf("INFO before calling listen()\n");

    listen(sockfd, 5);

    printf("INFO before entering While(1)\n");
    while(1)
    {
        int re;
        clilen = sizeof(cli_addr);
        printf("INFO before calling accept()\n");
        newsockfd = accept(sockfd, (struct sockaddr*)&cli_addr, &clilen);

        if(newsockfd < 0){
            error("ERROR on accepting\n");
            return 0;
        }
        printf("INFO before calling pthread_create()\n");
        re = pthread_create(&interrupt, NULL, clientHandler, NULL);
        if(re){
            printf("ERROR return code from the pthread_create() is %d\n", re);
        }       
    }
    printf("INFO before calling pthread_exit(NULL)\n");
    pthread_exit(NULL);
    return 0;
}

void *clientHandler(void *param){
    int n, newsockfd;
    newsockfd = *((int*)param);
    char buffer[256];
    bzero(buffer, 256);

    while(1){
        n = read(newsockfd, buffer, 255);

        if(n < 0){
            error("ERROR reading from socket\n");
            pthread_exit(NULL);
        }

        printf("Server received the message: %s", buffer);

        n = write(newsockfd, "Server got it\n", 18);

        if(n < 0){
            error("ERROR writing to socket\n");
            pthread_exit(NULL);
        }
    }
}

client.c

client.c

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define PORT 7778
#define HOSTNAME "127.0.0.1"

void error(char*msg){
    perror(msg);
    exit(1);
}

int main(){
    int sockfd, n;
    struct sockaddr_in serv_addr;
    struct hostent* server;
    //char *hostname = "127.0.0.1";

    char buffer[256];

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if(sockfd < 0)
        error("ERROR opening socket\n");

    server = gethostbyname(HOSTNAME);
    if(server == NULL){
        fprintf(stderr,"ERROR no such host\n");
        exit(0);
    }

    bzero((char*) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;

    bcopy((char*)server->h_addr, (char*)&serv_addr.sin_addr.s_addr, server->h_length);

    serv_addr.sin_port = htons(PORT);

    if(connect(sockfd, &serv_addr, sizeof(serv_addr)) < 0){
        error("ERROR connecting\n");
        return 0;
    }

    while(1)
    {    
        printf("Please enter the message: ");
        bzero(buffer, 256);
        fgets(buffer, 255, stdin);
        n=write(sockfd, buffer, strlen(buffer));

        if(n < 0){
            error("ERROR reading from socket\n");
            return 0;    
        }

        printf("[Server got it] %s\n", buffer);
    }

    return 0;
}

行,我成功Linux环境下使用终端建造的* .c文件。

OK, I builded the *.c files successfully in Linux environment by using terminal.

我用这个命令行来构建server.c

I used this command line to build server.c

gcc server.c -o server.out -pthread

和用这个来构建client.c

and use this one to build client.c

gcc client.c -o client.out

然后,我打电话server.out运行服务器:

Then, I call server.out to run the server:

 ./server.out

和运行client.out

and run client.out

./client.out 

但在我server.out运行的时候,我得到了错误:

BUT, at the time I run server.out, I got the error:

Segmentation fault (core dumped)

家伙,你能不能和我分享这个经验。是我的code错在什么地方?

Guys, could you share with me your experiences about this. Is my code wrong in somewhere?

推荐答案

这行传递NULL作为处理函数的参数

This line is passing NULL as the argument of the handler

 re = pthread_create(&interrupt, NULL, clientHandler, NULL);

它应该是:

 re = pthread_create(&interrupt, NULL, clientHandler, &newsockfd);

作为Sourav戈什意见。

as Sourav Ghosh comments.

这篇关于ç聊天程序得到一个错误段错误(核心转储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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