다양한 IP 주소로 UDP 소켓 연결 전달

다양한 IP 주소로 UDP 소켓 연결 전달

UDP 소켓 클라이언트 프로그램이 있습니다. 프로그램이 전송되어야 하는 IP 주소를 결정할 수 있습니다. 안타깝게도 내 컴퓨터를 액세스 포인트로 구성했는데 다른 사이트에 있는 UDP 서버의 주소를 모릅니다(알고 있지만 액세스 포인트는 모릅니다). 내 생각은 메시지를 캡처하여 네트워크 인터페이스(wlan0) 범위 내에서 가능한 모든 IP로 보내는 것입니다. 나는 이 목적을 위해 작은 Python 에이전트를 작성했는데, 이는 또한 문제를 설명합니다. 상대방 서버에서도 메시지를 받은 것 같습니다. 하지만 답장이 없는 것 같습니다.

클라이언트와 서버 프로그램 간의 UDP 소켓 통신을 캡처하고 전달하는 방법이 있습니까? 어쩌면 이미 사용 가능한 프로그램/도구가 있을까요? 저는 사실 경영 경험이 별로 없어요.

# Make this Python 2.7 script compatible to Python 3 standard
from __future__ import print_function
# For remote control
import socket
# For sensor readout
import logging
import threading
# For system specific functions
import sys
import os
import time
import datetime
import fcntl
import struct

# Create a sensor log with date and time
layout = '%(asctime)s - %(levelname)s - %(message)s'

# find suitable place for the log file
logging_dir   = "/var/log/"
logging_file  = "ardu_proxy.log"
if not os.path.isdir(logging_dir):
  logging_dir = "/"
# configure logging
logging.basicConfig(filename=logging_dir+logging_file, level=logging.INFO, format=layout)

# Socket for WiFi data transport
udp_port = 14550
udp_server  = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_server.bind(('0.0.0.0', udp_port))

def get_if_address(ifname):
  s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  return socket.inet_ntoa(fcntl.ioctl(
    s.fileno(),
    0x8915,  # SIOCGIFADDR
    struct.pack('256s', ifname[:15])
  )[20:24])

def get_ip4_adr(adr):
  array = adr.split('.')
  str = ""
  for i in xrange(0, 3):
    str += array[i] + "."
  return str

def echo_thr():                                                         # trnm_thr() sends commands to Arduino
  udp_msg    = None
  if_name = "wlan0"

  # get local interface address
  apif_wlan0 = get_if_address(if_name)
  apif_wlan0 = get_ip4_adr(apif_wlan0)

  while True:
    # receive a message running on local host
    udp_msg, udp_client = udp_server.recvfrom(512)                    # Wait for UDP packet from ground station
    logging.debug(udp_msg)
    print(udp_msg)

    # forward the msg and broadcast it in the complete network :D
    for ofs in xrange(1, 254):
      adr = apif_wlan0 + str(ofs)
      print(adr, ": ", udp_msg)
      udp_server.sendto(udp_msg, (adr, udp_port) )

echo_thr()

답변1

어쩌면 당신은 살펴볼 수 있습니다소캇. 예:

 socat -u tcp-listen:50505,reuseaddr - | P | socat -u - tcp-listen:60606,reuseaddr

여기서 P는 포트 50505에서 입력을 받아 포트 60606으로 출력을 전달하는 프로그램입니다.

이 예는 다음에서 빌려왔습니다. 표준 입력 및 표준 출력을 포트로 리디렉션

관련 정보