使用线程python发送和接收不起作用

时间:2018-08-16 09:42:50

标签: python multithreading udp

我正在编写一个程序,可以在其中发送和接收数据,但是在服务器端,它也开始发送消息并接收数据。但是在客户端,它说分配给该端口的错误。那么我需要分配哪个端口并尝试使用线程进行双向通信呢?请发表您的想法和解决方案,因为我是线程概念的新手。

谢谢

客户端:输出

Traceback (most recent call last):
  File "/home/PycharmProjects/new/ref.py", line 61, in <module>
    send = Sending('username', '192.168.1.27', 8000, 1500)
  File "/home/PycharmProjects/new/ref.py", line 13, in __init__
    self.sock = self.create_socket()
  File "/home/PycharmProjects/new/ref.py", line 17, in create_socket
    sock.bind((self.host, self.port))
OSError: [Errno 99] Cannot assign requested address
running

Process finished with exit code 1

客户端程序:

import socket
import threading
import logging
import time


class Sending():
    def __init__(self, name, tHost, tPort, target):
        self.name = name
        self.host = tHost
        self.port = tPort
        self.target_port = target
        self.sock = self.create_socket()

    def create_socket(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.bind((self.host, self.port))
        return sock

    def set_name(self, name):
        self.name = name

    def send_loop(self):
        while True:
            logging.debug('Starting send run')
            message = input('Enter message: ')
            data = bytearray()
            data.extend(message.encode('utf-8'))
            self.sock.sendto(bytearray(data), (self.host, self.target_port))

    def run(self):
        th2 = threading.Thread(name='send', target=self.send_loop)
        th2.start()


class Receiving():
    def __init__(self, host, port):
        self.host = host
        self.port = port

    def create_socket(self):
        logging.debug('Starting socket')
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.bind((self.host, self.port))
        print ('socket ready')
        time.sleep(5)
        while True:
            data, addr = sock.recvfrom(1500)
            print('gyg' + data.decode('utf-8') + str(addr))

    def run(self):
        th1 = threading.Thread(name='rec', target=self.create_socket)
        print("Made it here")
        th1.daemon = True
        th1.start()
        return

if __name__ == '__main__':
    print('running')
    rec = Receiving('192.168.1.27', 1485)
    send = Sending('username', '192.168.1.27', 8000, 1500)
    rec.run()
    send.run()

服务器端:

import threading
from threading import Thread
import socket
from socket import *
import time
import os
import re
import struct
import socket
import threading
import logging
import time


class Sending():
    def __init__(self, name, tHost, tPort, target):
        self.name = name
        self.host = tHost
        self.port = tPort
        self.target_port = target
        self.sock = self.create_socket()

    def create_socket(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.bind((self.host, self.port))
        return sock

    def set_name(self, name):
        self.name = name

    def send_loop(self):
        while True:
            logging.debug('Starting send run')
            message = input('Enter message: ')
            data = bytearray()
            data.extend(message.encode('utf-8'))
            self.sock.sendto(bytearray(data), (self.host, self.target_port))

    def run(self):
        th2 = threading.Thread(name='send',target=self.send_loop)
        th2.start()


class Receiving():
    def __init__(self, host, port):
        self.host = host
        self.port = port

    def create_socket(self):
        logging.debug('Starting socket')
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.bind((self.host, self.port))
        print ('socket ready')
        time.sleep(5)
        while True:
            data, addr = sock.recvfrom(1500)
            print('ggg:' + data.decode('utf-8') + str(addr))

    def run(self):
        th1 = threading.Thread(name='rec', target=self.create_socket)
        print("Made it here")
        th1.daemon = True
        th1.start()
        return

if __name__ == '__main__':
    print('running')
    rec = Receiving('192.168.1.27', 8000)
    send = Sending('username', '192.168.1.27', 1435, 1024)
    rec.run()
    send.run()

0 个答案:

没有答案