Home > Software engineering >  I'm converting ints to chars and trying to send them from a C client to a Python server on anot
I'm converting ints to chars and trying to send them from a C client to a Python server on anot

Time:02-02

On one computer with the client (written in C) I get the error send: Bad address when I try to send chars to another computer with a server written in Python. But the address is NOT bad.
If instead of chars I just send a written string, "A string written like this" I can send it just fine to the server and see it print with no problems. So, I don't think there is really a problem with an address.

I have also tried converting the int to a string. I get error when compiling cannot convert string to char. I have tried variations and I can only compile with the client written as it is below.

The client (in C)

#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>

#define ADDR "192.168.0.112"
#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;
    }
}

void thesock(char *ADDRf, char *PORTf, char *RAZZstr)
{
    struct addrinfo hints = {0}, *addr = NULL;
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    int status = getaddrinfo(ADDRf, PORTf, &hints, &addr);
    if (status != 0) {
        std::cerr << "Error message";
        exit(1);
    }
    
    int sock = -1;
    struct addrinfo *p = NULL;
    for (p = addr; p != NULL; p = addr->ai_next) {
        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);
    }
    sendall(sock, RAZZstr, 12);
    close(sock);
}

int main()
{
    int someInt = 321;
    char strss[12];
    sprintf(strss, "%d", someInt);  
    
    thesock(ADDR, PORT, strss);

    return 0;
}

This last part of the code above is where the chars, or string is entered. It's this part of the code where you can replace strss in thesock with a string written in the strss position "just like this" and it will send to the server on the other computer written in Python. Though, when compiling I do get warnings ISO C forbids converting a string constant to ‘char*’.

The server (In Python)

import os
import sys
import socket

s=socket.socket()

host='192.168.0.112'
port=12003

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

while True:
    c, addr=s.accept()
    content=c.recv(29).decode('utf-8')

    print(content)  

This server decodes utf-8. I don't know if I have the option for a different 'decode' here. I don't think Python has 'chars'.

CodePudding user response:

TL;DR: this is unrelated to "address" in terms of IP address but it is about invalid access to a local memory access.

int n = 0, total = 0;
while (total < length) {
    n = send(socket, bytes   total, total-length, 0);

total - length is a negative number, i.e. 0-12 = -12 in your case. The third argument of send is of type size_t, i.e. an unsigned integer. The negative number (-12) thus gets cast into an unsigned integer, resulting in a huge unsigned integer.

This causes send to access memory far outside the allocated memory for bytes, hence EFAULT "Bad address".

  •  Tags:  
  • Related