K
K
KorwinD2016-04-25 00:38:56
Python
KorwinD, 2016-04-25 00:38:56

How to solve a problem using sockets in python?

There are two programs for transferring files over the TCP protocol over the Internet, a client and a server, everything worked until the moment I redid the programs using functions. The bottom line is that the server sends information to the client three times in a row, the client must accept it three times, but it turns out a one-time acceptance of all three requests.

Example:

Server: Server to Client reporting2373896/home/corwin/Documents/NoCloud/

These are three queries glued together.

Below is the program code.
import socket
import os

def open_server():
    host = '0.0.0.0'
    port = 9091
    serv_addr = (host, port)

    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    tcp_socket.bind(serv_addr)
    tcp_socket.listen(1)

    print('Waiting connection')

    connection(tcp_socket)

def connection(tcp_socket):
    conn, clnt_addr = tcp_socket.accept()
    print('Connection is established')

    data_to_recv = conn.recv(1024)
    if not data_to_recv:
        conn.close()
        tcp_socket.close()
        sys.exit(1)
    else:
        print('Client: '+data_to_recv)
        conn.send(b'Server to Client reporting') #Первый раз отсылаю информацию.

    transerf(conn, tcp_socket)

def transerf(conn, tcp_socket):
    conn.send(str(os.path.getsize(os.path.dirname(__file__)+'list.dat'))) #Второй раз отсылаю информацию.
    fl = open(os.path.dirname(__file__)+'list.dat', 'rb')
    st = fl.read()
    conn.send(st) #Третий раз отсылаю информацию.
    conn.close()
    tcp_socket.close()

def server():
    open_server()

server()


import socket
import os

def connection():
    serv_addr = ('127.0.0.1', 9091)
    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcp_socket.connect(serv_addr)
    print('Connection is established')

    tcp_socket.send(b'Client to Server reporting')

    data_to_recv = tcp_socket.recv(1024) #Получаю первый запрос.
    print('Server: '+data_to_recv)

    transerf(tcp_socket)

def transerf(tcp_socket):
    size = tcp_socket.recv(1024) #Второй раз получаю запрос.
    data = tcp_socket.recv(int(size)) #Третий раз получаю запрос.
    fl = open(os.path.dirname(__file__)+'list1.dat', 'w')
    fl.write(data)
    tcp_socket.close()

def client():
    connection()

client()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
nirvimel, 2016-04-25
@nirvimel

I understood what the essence of the problem was only from one description, before I had time to look into the code (rarely this happens). Do you understand the meaning of the only parameter in recv? Why are you passing the constant 1024 there? This is the amount in bytes that is read from the socket in one call. A socket is not a queue of transmitted packets, but a stream of bytes that is filled from one side via send and drained from the other via recv (the socket.SOCK_STREAM flag symbolizes this). If in some cases recv returns exactly the same packet of data that was transmitted in one send call, it is only because there is no other data in the stream at that moment.
Therefore, if the logic of your client-server involves the exchange of packets, then you must implement all the breakdown into packets yourself. For example, in the header of each packet, send its length, and after reading this header, read exactly the specified number of bytes from the socket.
All this is the overhead of working with bare TCP. To solve just these problems, there are special packet protocols and libraries for working with them. You can try, for example, ZeroMQ (via PyZMQ ) if you are interested in packet exchange (that is, "messages" in ZeroMQ terms) and do not want to write this bike yourself.

L
lega, 2016-04-25
@lega

They can not only stick together, but also break in different places, for example sent 3 times, received 5 times.
In normal http, a separate connection is created for this for a separate transfer (one file).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question