I have 2 program (client and server) and written using python 3.
function:
Server Program = Monitoring packet
Client Program = Sending packet
this is the server code:
import socket
from struct import *
def PacketMonitoring():
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_UDP)
except socket.error as e:
err = e.args[0]
print ("Errornya: ",e)
sys.exit()
while True:
packet = sock.recvfrom(65535)
packet = packet[0]
ip_header = packet[0:20]
iphead = unpack ("!BBHHHBBH4s4s", ip_header)
version_ihl = iphead[0]
version = version_ihl >> 4
ihl = version_ihl & 0xF
iph_length = ihl * 4
ttl = iphead[5]
source_addr = socket.inet_ntoa(iphead[8])
dest_addr = socket.inet_ntoa(iphead[9])
udp_header = packet[iph_length:iph_length+8]
udphead = unpack("!HHHH",udp_header)
source_port = udphead[0]
dest_port = udphead[1]
print("\nSource IP address: ",source_addr)
print("Destination Port: ",dest_port)
print("Message: ",packet)
if __name__ == "__main__":
PacketMonitoring()
and this is the client code:
import socket
def SendPacket(IP,Port):
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = (IP, Port)
try:
clientSocket.sendto("Hello".encode(), (server_address))
print("Packet 1 send")
except:
print("Packet Failed to Send")
def Main():
IP = "192.168.67.101"
Port = 4000
SendPacket(IP,Port)
if __name__ == "__main__":
Main()
From the client side I send packet that contain message "Hello" to the server. How to fix my code, so I can get that message and print that message (just "Hello" at the server side).
thanks