You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux下C语言Socket服务器启动报错:connection failed, connection refused

Fixing "connection failed, connection refused" for your C Socket Server

Hey there, let's break down why you're seeing this error and fix it step by step. First off—wait a second: your server program shouldn't be throwing a "connection failed" error, since servers listen for connections instead of initiating them. That error is typically from the client, but the root cause is almost certainly that your server isn't starting up correctly in the first place. Let's go through the issues:

1. Critical Main Function Parameter Mistake

Look at your server.c main function definition:

int main(int argc , char **argv[])

That's wrong! The correct syntax for main is either:

int main(int argc, char *argv[])
// OR
int main(int argc, char **argv)

This mistake will cause compilation failures or runtime crashes, meaning your server never actually starts listening on port 8080. No wonder the client can't connect!

2. Typo in Error Message

In your setsockopt call, you have a typo in the error string: error("setsockertopt"); should be error("setsockopt");. It's a small thing, but it'll make error messages confusing when debugging.

3. Compilation Needs Thread Library Linking

You included <pthread.h> in your server code (even though you're not using threads right now). To avoid warnings or errors during compilation, compile your server with the -pthread flag:

gcc server.c -o server -pthread

For the client, a simple compile works:

gcc client.c -o client

4. Ensure Server Starts First

This is a classic gotcha: always run ./server first, wait for it to block (it'll look like it's doing nothing—that's normal, it's waiting for a client!), then run ./client in another terminal. If you run the client first, it'll immediately throw the "connection refused" error because there's no server listening.

5. Check if Port 8080 is Already in Use

If another program is using port 8080, your server's bind call will fail. Check with these commands:

lsof -i :8080
# Or if lsof isn't installed:
netstat -tulpn | grep 8080

If the port is taken, change the DEFAULT_PORT define in both server.c and client.c to something else (like 8081), then recompile both.

Fixed Server Code

Here's the corrected server.c with all the fixes mentioned:

#include <unistd.h> 
#include <stdio.h> 
#include <sys/socket.h> 
#include <stdlib.h> 
#include <netinet/in.h> 
#include <string.h> 
#include <pthread.h> 

#define DEFAULT_PORT 8080 

void error(char* message){
    perror(message);
    exit(EXIT_FAILURE);
}

void handler_client(void){}

int main(int argc , char **argv){
    int server_fd;
    int new_socket;
    int valread;
    struct sockaddr_in address;
    int opt = 1;
    int addrlen = sizeof(address);
    char buffer[1024] = {0};
    char *hello = "Hello, I'm server";

    if((server_fd = socket(AF_INET, SOCK_STREAM , 0)) == 0){
        error("socket failed");
    }

    if(setsockopt(server_fd , SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT , &opt , sizeof(opt))){
        error("setsockopt");
    }

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(DEFAULT_PORT);

    if(bind(server_fd , (struct sockaddr *)&address, sizeof(address) ) < 0){
        error("bind failure");
    }

    if(listen(server_fd , 3) < 0){
        error("listen");
    }

    if((new_socket = accept(server_fd , (struct sockaddr *)&address , (socklen_t *)&addrlen)) < 0){
        error("accept");
    }

    valread = read( new_socket , buffer , 1024);
    printf(" message from client : %s \n",buffer);
    send(new_socket , hello , strlen(hello) , 0);
    printf("Hello message sent \n");

    return 0;
}

Step-by-Step Verification

  1. Fix the server code as above
  2. Compile server and client with the commands mentioned
  3. Start the server: ./server (it'll sit idle waiting for connections)
  4. Open a new terminal, run ./client, type your message, and you should see the server and client interact successfully.

内容的提问来源于stack exchange,提问作者Mohammad Rehan Abdul Kadir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:30:42