Ant
Ant

Reputation: 1143

Using sockets to send a string from a C++ client on one computer to a Python server on another. Getting `send: Bad file descriptor`

I'm trying to send a string from a C++ client on one computer to a Python server on another computer.
My error is send: Bad file descriptor

The Python server is killed if it is contacted by the client but it doesn't receive a string. While the Python server is running it does end the program when I attempt to send the string from the C++ client. So I know the server is being reached by the client when I execute it.

I am able to send strings to the server from the C++ client's computer with a Python client script. Since it's not a basic problem with the server I don't think this and other answers apply to my problem.

On the Python script I have tried changing this number. s.listen(11)

Here is the Python server

import os
import sys
import socket

s=socket.socket()

host='192.168.0.101'
port=12003

s.bind((host,port))
s.listen(11)

while True:
    c, addr=s.accept()
    content=c.recv(1024).decode('utf-8')
    print(content)
    if not content:
        break

Here is the C++ client

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <unistd.h>
    
#define ADDR "192.168.0.101"
#define PORT "12003"
    
void sendall(int socket, char *bytes, int length)
{
    int n = 0, total = 0;
    while (total < length) {
        n = send(socket, bytes + total, total-length, 0);
        if (n == -1) {
            perror("send");
            exit(1);
        }
        total += n;
    }
}
    
int main()
{
    struct addrinfo hints = {0}, *addr = NULL;
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    
    int status = getaddrinfo(ADDR, PORT, &hints, &addr);
    if (status != 0) {
        fprintf(stderr, "getaddrinfo()\n");
        exit(1);
    }
    int sock = -1;
    {
        struct addrinfo *p = NULL;
        for (p = addr; p != NULL; p = addr->ai_next) {
            int sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
            if (sock == -1) {
                continue;
            }
            if (connect(sock, p->ai_addr, p->ai_addrlen) != -1) {
                break;
            }
            close(sock);
        }
        if (p == NULL) {
            fprintf(stderr, "connect(), socket()\n");
            exit(1);
        }
        freeaddrinfo(addr);
        /* Do whatever. */
        sendall(sock, "Hello, World", 12);
    
        /* Do whatever. */
    }
    
    close(sock);
    return 0;
}

UPDATE:
In the client there was an unessacary int in front of sock = socket...

I removed it and now I'm getting an error on the server side when I send the string that reads..

$ python server.py

Traceback (most recent call last):
  File "/home/computer/server.py", line 35, in <module>
    content=c.recv(1024).decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 29: invalid start byte

Upvotes: 0

Views: 579

Answers (1)

Barmar
Barmar

Reputation: 780673

You're redeclaring the sock variable in the for loop, so the value of sock when you call sendall() is the original -1. Change

            int sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);

to

            sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);

so it assigns the outer variable.

Upvotes: 1

Related Questions