S
S
shansung2015-11-11 01:00:43
Python
shansung, 2015-11-11 01:00:43

Why is the socket server not shutting down?

Why isn't the server shutting down? In this case, the client closes correctly
Client:

import time
import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
addres = ('localhost', 9091)
sock.connect(addres)

f1 = open('array.txt','rb')
arr = f1.read().decode('utf-8').split(' ')
f2 = open('array2.txt','rb')
arr2 = f2.read().decode('utf-8').split(' ')
f3 = open('database.txt','wb')
for i in range(0, 3):
  f = open('send.txt','wb')

  f.write(bytes(arr[i] + ' ', 'utf-8'))
  c = 0
  while(c < 1512 - len(arr[i]) - 1):
    f.write(bytes('0', 'utf-8'))
    c += 1
  f.close()
  f = open('send.txt', 'rb')

  print('Sending...')

  l = f.read(1512)
  while (l):
    sock.sendto(l,addres)
    l = f.read(1512)
  f.close()
  time.sleep(int(arr2[i]))
  f3.write(bytes(sock.recv(1024) + b' ') ))
sock.close()

Server:
import socket
import time

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('', 9091))
#sock.listen(1)
#conn, addr = sock.accept()

print ('connected:',)
while True:
  
  data,addr = sock.recvfrom(1512)
  if not data:
    print ("Client has exited!")
    break
  else:
    data = data.decode("utf-8")
    data_arr = data.split(' ')
    counter = int(data_arr[0])
    res = 0
    while(counter):

      res += 100000
      counter -= 1
  sock.sendto(bytes(str(res), 'utf-8'), addr)
  
sock.close()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question