Udp Server For Mac



We already know about two main transport layer protocols like TCP and UDP. For more information about TCP and UDP you can check reference section. In this article we will learn how to send and receive UDP packets using python program.

  1. Udp Server Mac
  2. Udp Server For Mac Catalina

The UdpClient class provides simple methods for sending and receiving connectionless UDP datagrams in blocking synchronous mode. Because UDP is a connectionless transport protocol, you do not need to establish a remote host connection prior to sending and receiving data. You do, however, have the option of establishing a default remote host in. UDP Overview: UDP is the abbreviation of User Datagram Protocol. UDP makes use of Internet Protocol of the TCP/IP suit. In communications using UDP, a client program sends a message packet to a destination server wherein the destination server also runs on UDP. Properties of UDP: The UDP does not provide guaranteed delivery of message packets. I'm trying to create a program for read, given IP address and port (in this case on localhost), of UDP packets on Mac OS X (current version 10.9.5). The only one that gave me some useful data is tcpdump and nc (netcat), but it worked only 1 time. Steps to create UDP Server. Create a socket using socket system call. The second argument of the socket will be SOCKDGRAM, it is socket over a datagram which is nothing, it is UDP only. Set socket option to reuse same IP address and port using system call setsockopt. UDP Client Server is a program that makes use of both an UDP server and client in order to test network services and applications.It can also come in handy to detect intruders and harmful incoming.

Expectations:

Here are some key points can be learned from this article

  1. Sending some text using python program which uses UDP protocol.
  2. Receiving some text using python program which uses UDP protocol.
  3. Check UDP packet in Wireshark.
  4. Learn about python code to send and receive UDP packets.

General Set Up Diagram:

Udp server mac

System A and B should able to ping each other.

Assumptions or Limitations:

  1. Both systems should be having Linux with Ubuntu. The code may or may not work on other operating system like Windows10, MAC etc.
  2. Both systems should have python version 3. This code may or may not work on python 2.7 version.

Note: You can check reference for trying out Send and Receive UDP packets via Linux CLI before going for python files to do the same task.

Python files:

There are two python files server.py and client.py. server file and client file should be present in Server system and Client system respectively.

Server.py

importsocket
importsys
iflen(sys.argv)3:
# Get 'IP address of Server' and also the 'port number' from
argument 1and argument 2
ip =sys.argv[1]
port =int(sys.argv[2])
else:
print('Run like : python3 server.py <arg1:server ip:this system IP 192.168.1.6> <arg2:server port:4444 >')
exit(1)
# Create a UDP socket
s =socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
# Bind the socket to the port
server_address =(ip, port)
s.bind(server_address)
print('Do Ctrl+c to exit the program !!')
whileTrue:
print('####### Server is listening #######')
data, address = s.recvfrom(4096)
print('nn 2. Server received: ', data.decode('utf-8'),'nn')
send_data =input('Type some text to send => ')
s.sendto(send_data.encode('utf-8'), address)
print('nn 1. Server sent : ', send_data,'nn')

Client.py

Udp Server Mac

importsocket
importsys
iflen(sys.argv)3:
# Get 'IP address of Server' and also the 'port number' from argument 1 and argument 2
ip =sys.argv[1]
port =int(sys.argv[2])
else:
print('Run like : python3 client.py <arg1 server ip 192.168.1.102> <arg2 server port 4444 >')
exit(1)
# Create socket for server
s =socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
print('Do Ctrl+c to exit the program !!')
# Let's send data through UDP protocol
whileTrue:
send_data =input('Type some text to send =>');
s.sendto(send_data.encode('utf-8'),(ip, port))
print('nn 1. Client Sent : ', send_data,'nn')
data, address = s.recvfrom(4096)
print('nn 2. Client received : ', data.decode('utf-8'),'nn')
# close the socket
s.close()

Send/Receive UDP packet:

Let’s take an example like we will send UDP packet from System A to System B. So, in server-client concept, we have to run server at System B side and client at System A side.

Also we have valid IP addresses.

System A IP: 192.168.1.6
System B IP: 192.168.1.102

Now unlike Send and Receive UDP packets via Linux CLI we will run server.py in System, B [192.168.1.102] and then we will run client.py in System A [192.168.1.6].

How to run server.py in 192.168.1.102?

Here is the command to run server.py

Here is the screenshot

Here there are two arguments for the python program. 1st argument is IP address of server itself , here its 192.168.1.102 and 2nd argument is port which server will be listening, here we have chosen 4444.

How to run client.py in 192.168.1.6?

Here is the command to run client.py

Here is the screenshot

Here there are two arguments for the python program. 1st argument is IP address of server , here its 192.168.1.102 and 2nd argument is port where server is running. For our example it’s 4444.

Send or receive some text:

Udp Server For Mac Catalina

Now as you can see we are ready to communicate between two systems. But we need to start from client first. Let’s type something in client and see if it reaches to server or not.

Send Data from client: “I am from Client”

Screenshot form client:

Now this client message should come to server. Here is the server screenshot.

Now we can see in server side also we have option to send something to client. Let’s try that.

Send Data from client: “I am from Server”

Server screenshot:

And here is the screenshot on client side.

Now this will go on infinite times until we stop the python program using Ctrl+c.

Check UDP packet in Wireshark:

Now we have done some communication but how do we come to know that UDP was used to send or receive those packets. So, we can check capture in Wireshark.

Let’s see the packet when client [192.168.1.6] sent data [“I am from Client”] to server [192.168.1.6].

Code explanation:

For basic python code explanation refer “Python Socket File Transfer Send” in reference section.

We will only explain important lines codes for Client and Server python file. There are useful comments inside the client and server code.

Client code explanation:

The above line is required to check whether user has passed required mandatory arguments. Or else program will exit. Same check is there in server program.

s =socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)

The above line is to create socket server with UDP [SOCK_DGRAM] protocol. Same code is there in server.py.

To run program in infinite loop until user does Ctrl+c. Same code is there in server.py.

To send data for mentioned ip and port number.

To receive any data coming from server. Same code is there in server.py.

Server code explanation:

Send data to client address.

Conclusion:

We can send or receive UDP data using python program. Internally it uses server client mechanism.

References:

To understand TCP: https://linuxhint.com/tcp_packet_capture_analysis/
To understand UDP: https://linuxhint.com/udp_wireshark_analysis/
Send and Receive UDP packets via Linux CLI:
https://linuxhint.com/send_receive_udp_packets_linux_cli/
Python Socket File Transfer Send :
https://linuxhint.com/python_socket_file_transfer_send/