简体   繁体   中英

Python Socket Server receives gibberish text files, after client sends an text file

I created a client (C Language to send file) and server (Python Language to receive) to test sending and receiving.txt file. However, when I did try to send and receive it with the server, the received file contains the original text+gibberish text. Below is the server code (receive file) in Python:

import socket

#Set up socket
IP_SERVER = "127.0.0.1"
HEADER = 1024
PORT = 15000
ADDR = (IP_SERVER,PORT)
FORMAT = "utf-8"

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(ADDR)
server_socket.listen()


#Set up file 
file =open("dari_client.txt","wb")
connected = True
while connected :
    conn,addr = server_socket.accept()
    terima = conn.recv(HEADER).decode(FORMAT)
    print("Client connected \n")
    while terima:
        file.write(terima)
        terima = conn.recv(HEADER).decode(FORMAT)
    file.close()
    connected = False

conn.close()

Below is my Client code in C language:

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

void kirim_file(int socket,FILE *fl)
{
    int n;
    char data[1024];
    while (fgets(data,1024,fl) != NULL){
        send(socket,data,sizeof(data),0);
    }
    bzero(data,1024);
}

int main(){
    int network_socket,cli;
    FILE *fl;
    char nama_file[20] = "pesan.txt";
    network_socket = socket(AF_INET ,SOCK_STREAM, 0);

    // socket server address
    struct sockaddr_in server_address,client_address;
    server_address.sin_family = AF_INET;
    server_address.sin_port = htons(15000);
    server_address.sin_addr.s_addr = inet_addr("127.0.0.1");

    connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address));
    fl = fopen(nama_file,"r");
    kirim_file(network_socket,fl);

    close(network_socket);
}
 

Gibberish can be seen in this picture: Gibberish received file

Any idea on how to fix this?

Most likely this is at least in part because you are sending the entire array data rather than just the content that you read from the file. From the man page for string.h :

char *fgets(char *s, int size, FILE *stream)

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

So each time you read the file you are sending both the read data, and all the garbage data stored in the rest of the buffer data . Probably the simplest way is to check for the amount of data read, rather than sending all of data each time:

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

void kirim_file(int socket,FILE *fl)
{
    int n;
    char data[1024];

    while (fgets(data, 1024, fl) != NULL){
        int length = strnlen(data, 1024);

        send(socket, data, length, 0);
    }

    bzero(data,1024);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM