C server - Python client. connection refused


Anton Zakharov:

I'm new to Socket, please forgive me for my complete ignorance.

useless_server.c:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>


int main(void)
{
    int sd;
    int newsd;
    struct sockaddr_in client_addr;
    socklen_t cli_size;
    struct sockaddr_in server_addr;

    sd = socket(AF_INET, SOCK_STREAM, 0);
    if (sd < 0)
    {
        printf("unable to create socket\n");
        exit(1);
    }

    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = 50000;

    if (bind(sd, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0)
    {
        printf("unable to bind socket\n");
        exit(1);
    }

    listen(sd, 2);

    while (1)
    {
        cli_size = sizeof(client_addr);

        newsd = accept(sd, (struct sockaddr *) &client_addr, &cli_size);
        printf("Got connection from %s\n", inet_ntoa(client_addr.sin_addr));
        if (newsd < 0)
        {
            printf("Unable to accept connection\n");
            exit(1);
        }
        sleep(5);
        close(newsd);
    }

    return 0;
}

useless_client.py

#!/usr/bin/env python3

import socket


sock = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
sock.connect((socket.gethostname(), 50000))

useless_server.c is compiled with gcc. Then, I run the server in a terminal. The client is called by another.

When running useless_client.py I get:

Traceback (most recent call last):
  File "./useless_client.py", line 7, in <module>
    sock.connect((socket.gethostname(), 50000))
ConnectionRefusedError: [Errno 111] Connection refused

When I try to connect to the C server from the C client, everything is fine. I use AF_INET, SOCK_STREAM in both cases.

When I try to connect to the analogy Python server from a Python client, everything is fine.

What am I doing wrong?

renew.

replace

server_addr.sin_port = 50000;

and

server_addr.sin_port = htons(50000);

solved the problem. Thanks!

animal:

What am I doing wrong?

In your C program, you forgot to convert the port number to network byte order (which is done implicitly in Python):

    server_addr.sin_port = htons(50000);

Related


C server - Python client. connection refused

Anton Zakharov: I'm new to Socket, please forgive me for my complete ignorance. useless_server.c: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/socket.h> #include <arpa/inet.h> #include <string.h> int main(void) { int sd;

C server - Python client. connection refused

Anton Zakharov: I'm new to Socket, please forgive me for my complete ignorance. useless_server.c: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/socket.h> #include <arpa/inet.h> #include <string.h> int main(void) { int sd;

Fixed: C++ server/client program: "Connection refused"

username I'm writing a simple program to transfer files from a server to a client (both on the same computer at the moment). Using "telnet 127.0.0.1 [port]", I can successfully get the file from the server, but when I run the client, the server refuses the con

Fixed: C++ server/client program: "Connection refused"

username I'm writing a simple program to transfer files from a server to a client (both on the same computer at the moment). Using "telnet 127.0.0.1 [port]", I can successfully get the file from the server, but when I run the client, the server refuses the con

Client socket connection refused

Net Fox I'm trying to understand how sockets work in Java, so I want to look at a client-server implementation. I found this: Creating a Simple Java TCP/IP Server and Client Socket . If I pass, the server seems to work, localhostor 127.0.0.1the client will ref

Netcat server connection refused

John Murray Using netcat, I need to create a simple server that responds with simple JSON data. First, I try to use the HTML page: while true ; do nc -l 8888 < index.html ; done or more simply: netcat -l 8888 < index.html Then do the following in your brows

Netcat server connection refused

John Murray Using netcat, I need to create a simple server that responds with simple JSON data. First, I try to use the HTML page: while true ; do nc -l 8888 < index.html ; done or more simply: netcat -l 8888 < index.html Then do the following in your brows

Domino server refused connection

Mascu We use Pentaho to generate reports and dashboards with Domino server data using the JDBC connector and they all work fine , but we noticed a few weeks ago that our Pentaho reports were not working. Here is the message we get from the console: Caused by:

Netcat server connection refused

John Murray Using netcat, I need to create a simple server that responds with simple JSON data. First, I try to use the HTML page: while true ; do nc -l 8888 < index.html ; done 或更简单地说: netcat -l 8888 < index.html 然后在浏览器中执行以下操作: http://localhost:8888/index.

Netcat server connection refused

John Murray Using netcat, I need to create a simple server that responds with simple JSON data. First, I try to use the HTML page: while true ; do nc -l 8888 < index.html ; done or more simply: netcat -l 8888 < index.html Then do the following in your brows

kubectl: connection to server refused

like wood When i run kubectl run... or any command i get the error message The connection to the server localhost:8080 was refused - did you specify the right host or port? What exactly is this error? How to solve? renew I really don't know much about kubectl

Websocket server connection refused

Gilgamesh I want to make a small Android game where one person is the host (websocket server) and the other is the client (websocket client). I'm using TooTallNate's Java-WebSocket library for both client and server . I have two phones, one using a websocket s

Connection refused to PostgreSQL server

Orel I am currently setting up a postreSQL server on a Windows computer. I want to create my first database, but the createdb function cannot be created due to failed password authentication. So I found the following questions on this site: How to configure po

Netcat server connection refused

John Murray Using netcat, I need to create a simple server that responds with simple JSON data. First, I try to use the HTML page: while true ; do nc -l 8888 < index.html ; done or more simply: netcat -l 8888 < index.html Then do the following in your brows

Websocket server connection refused

Gilgamesh I want to make a small Android game where one person is the host (websocket server) and the other is the client (websocket client). I'm using TooTallNate's Java-WebSocket library for both client and server . I have two phones, one using a websocket s

C++ socket client to python server not creating connection

fusu I am trying to create a C++ socket client for a python server. Because I'm new to C++ before creating a C++ client, I checked if the python socket server comes with a python client. After creating and running a C++ client, the server doesn't get any conne

C++ socket client to python server not creating connection

fusu I am trying to create a C++ socket client for a python server. Because I'm new to C++ before creating a C++ client, I checked if the python socket server comes with a python client. After creating and running a C++ client, the server doesn't get any conne

C server socket refuses connection from python client

Serumersey I'm trying to make a messenger program (there are a lot of bugs at the moment, so ignore them) and for some reason the server won't let the client connect. I tried changing the port but nothing worked. I get the following error (for my client, it's

C++ socket client to python server not creating connection

fusu I am trying to create a C++ socket client for a python server. Because I'm new to C++ before creating a C++ client, I checked if the python socket server comes with a python client. After creating and running a C++ client, the server doesn't get any conne

C++ socket client to python server not creating connection

fusu I am trying to create a C++ socket client for a python server. Because I'm new to C++ before creating a C++ client, I checked if the python socket server comes with a python client. After creating and running a C++ client, the server doesn't get any conne

Mosquito client gets connection refused

Luca Davanzo: I want to use MQTT protocol through mosquitto library. First, I want to do some testing install mosquitto-clients sudo apt-get install mosquitto-clients The program provides two "methods": mosquitto_pub mosquitto_sub Following this instruction,

Mosquito client gets connection refused

Luca Davanzo I want to use MQTT protocol through mosquitto library. First, I want to do some testing install mosquitto-clients sudo apt-get install mosquitto-clients The program provides two "methods": mosquitto_pub mosquitto_sub Following this instruction,

Mosquito client gets connection refused

Luca Davanzo: I want to use MQTT protocol through mosquitto library. First, I want to do some testing install mosquitto-clients sudo apt-get install mosquitto-clients The program provides two "methods": mosquitto_pub mosquitto_sub Following this instruction,

python socket refused connection

Nicklos So I am trying to make a server program that will call the client program. If I call the server client myself from the command line, the server client works fine, but the connection is refused when the server is called. Why doesn't this work? Here is t

python socket refused connection

Nicklos So I am trying to make a server program that will call the client program. If I call the server client myself from the command line, the server client works fine, but the connection is refused when the server is called. Why doesn't this work? Here is t