0

I am beginner in programming I have a program to sending .wav files via TCP sockets in python from web. Program works fine but i have question. When transfer is complete between server and client, program on server side is still running. Is there a way how "kill him"? I mean after transfer is complete, program will end by itself. Sorry for bad English. Here is code:

import socket                   # Import socket module
import os

port = 60001                  # Reserve a port for your service.
s = socket.socket()             # Create a socket object
host = socket.gethostname()


# Get local machine name
s.bind((host, port))
f = open('fare.wav', 'wb')
# Bind to the port
s.listen(1)                     # Now wait for client connection.

print 'Server listening....'

while True:
    conn, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    print "Recieving..."
    l = conn.recv(4096)
    while (l):
       print "Recieving..."
       f.write(l)
       l = conn.recv(4096)
    f.close()
    print('Done receiving')
    conn.send('Thank you for connecting')
    conn.close()

Client:

import os
import socket
s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 60001 # Reserve a port for your service.

s.connect((host, port))

f = open('fanfare2.wav','rb')
print("Sending...")
l = f.read(4096)
while (l):
    print("Sending...")
    s.send(l)
    l = f.read(4096)
f.close()
print("Done Sending")
s.shutdown(socket.SHUT_WR)
print s.recv(1024)
s.close()
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Yety
  • 1
  • 1
  • 2

1 Answers1

0

If you don't want it to close after completing the transaction just use break to get out of the while loop on server code also please take a look at this post

#!/usr/bin/python

import socket                   # Import socket module
import os

port = 60001                  # Reserve a port for your service.
s = socket.socket()             # Create a socket object
host = socket.gethostname()


# Get local machine name
s.bind((host, port))
f = open('fare.wav', 'wb')
# Bind to the port
s.listen(1)                     # Now wait for client connection.

print 'Server listening....'

while True:
    conn, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    print "Recieving..."
    l = conn.recv(4096)
    while (l):
       print "Recieving..."
       f.write(l)
       l = conn.recv(4096)
    f.close()
    print('Done receiving')
    conn.send('Thank you for connecting')
    conn.close()
    break
eorochena
  • 176
  • 12