Commit 4add83f3 authored by yogesh.m's avatar yogesh.m

update

parents
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/tcp-over-udp.iml" filepath="$PROJECT_DIR$/.idea/tcp-over-udp.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RunManager">
<configuration name="main" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
<module name="tcp-over-udp" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/main.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
</component>
</project>
\ No newline at end of file
import socket
TCP_IP = 'localhost'
TCP_PORT = 5005
BUFFER_SIZE = 1024
# create TCP socket and bind to IP address and port
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_socket.bind((TCP_IP, TCP_PORT))
tcp_socket.listen(1)
# create UDP socket
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
# accept incoming TCP connection
conn, addr = tcp_socket.accept()
print('Connection address:', addr)
while True:
# receive data from TCP connection
data = conn.recv(BUFFER_SIZE)
if not data: break
print('Received data:', data)
# encapsulate data in UDP packet and send to server
udp_socket.sendto(data, ('localhost', 5006))
# receive response from UDP server
response, server_addr = udp_socket.recvfrom(BUFFER_SIZE)
print('Received response:', response)
# send response back to TCP client
conn.send(response)
# close TCP connection
conn.close()
import socket
UDP_IP = 'localhost'
UDP_PORT = 5006
BUFFER_SIZE = 1024
# create UDP socket and bind to IP address and port
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udp_socket.bind((UDP_IP, UDP_PORT))
# create TCP socket to send packet back to original server
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
TCP_IP = 'localhost'
TCP_PORT = 5005
tcp_socket.connect((TCP_IP, TCP_PORT))
while True:
# receive UDP packet
data, addr = udp_socket.recvfrom(BUFFER_SIZE)
print('Received data:', data)
# send TCP packet to original server
tcp_socket.send(data)
# receive response from TCP server
response = tcp_socket.recv(BUFFER_SIZE)
print('Received response:', response)
# encapsulate response in UDP packet and send back to client
udp_socket.sendto(response, addr)
tcp_socket.close()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment