Reputation: 813
I have written a simple client/server socket program, where the server part is in C language whereas the client is in python. I am very much able to send the data from the client to the server but cannot send a response message/data back to the python script..
server.c
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
int main(int argc , char *argv[])
{
int socket_desc , new_socket , c, valread;
struct sockaddr_in server, client ;
char *message;
char buffer[1024] = {0};
char *hello = "Hello from server";
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 8888 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("bind failed");
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
new_socket = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (new_socket<0)
{
perror("accept failed");
}
puts("Connection accepted\n");
valread = read(new_socket, buffer, 1024);
printf("%s\n", buffer);
send(new_socket, hello, strlen(hello),0);
printf("Socket: Sent data!\n");
write(new_socket, "Some message", 1024);
return 0;
}
Whenever I run the server and the client, I am able to connect them and send data to the server but the response to be sent back cannot be decoded by the client and gives me the following traceback call...
Sending "This is the message. It will be repeated."
Traceback (most recent call last):
File "pythonclient.py", line 35, in <module>
client_program()
File "pythonclient.py", line 25, in client_program
text += data.decode().strip()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfb in position 44: invalid start byte
Upvotes: 0
Views: 1681
Reputation: 123260
write(new_socket, "Some message", 1024);
Since "Some message" is less than 1024 byte the server will send "Some message" and then junk data to the client, i.e. whatever is in memory after this message buffer until the length of 1024 bytes is reached. The chance is high that the junk data contain byte combinations which are not valid utf-8 in which case the Python application will fail to decode the data as UTF-8.
Upvotes: 2
Reputation: 71
I tested your Client.py and it seems works well, but when I send hex 0xfb
, the program crushed like that, so I think your server part sends invalid data, which is not in utf-8
format.
I suggest you to put a debug code like this:
data = b''.join(iter(read_socket, b''))
print(bytes(data))
text += data.decode().strip()
so you can see what bytes you sent and find the invalid data, that your server sends.
Upvotes: 2