Question: Can you convert this in C programming. UDPServer Implementation : package in.techdive.udp.server; import java.net.DatagramPacket; import java.net.DatagramSocket; /** * UDP Server listening to receive data from
Can you convert this in C programming.
UDPServer Implementation :
package in.techdive.udp.server;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* UDP Server listening to receive data from UDP Clients
*/
public class UDPServer
{
public static void main(String args[])
{
int server_port = 1111;
System.out.println("UDP Server Listening in " + server_port);
try
{
// DatagramSocket created and listening in Port 1111
DatagramSocket socket = new DatagramSocket(server_port);
byte[] msgBuffer = new byte[1024];
// DatagramPacket for receiving the incoming data from UDP Client
DatagramPacket packet = new DatagramPacket(msgBuffer, msgBuffer.length);
while (true)
{
socket.receive(packet);
String message = new String(msgBuffer, 0, packet.getLength());
System.out.println("UDPServer: Message received = " + message);
packet.setLength(msgBuffer.length);
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Error in getting the Data from UDP Client");
}
}
}
UDPClient Implementation :
package in.techdive.udp.client;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* UDP Client for sending message
*/
public class UDPClient
{
public static void main(String args[])
{
try
{
String server_address = "localhost";
int server_port = 1111;
String message = "Techdive.in";
InetAddress address = InetAddress.getByName(server_address);
// Create Datagram packet with the UDP Server Ipaddress/ Port and Message to be sent
DatagramPacket packet = new DatagramPacket(message.getBytes(), message.getBytes().length, address, server_port);
// Create DatagramSocket socket and send the Message
DatagramSocket socket = new DatagramSocket();
socket.send(packet);
System.out.println("UDPClient: Sent data to Server ; Message = " + message);
socket.close();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Error in sending the Data to UDP Server");
}
}
}
Testing :
In order to test the above UDP Client and Server, perform the below steps,
1. Execute the UDPServer.java class.
This will create the Datagram Socket and it will listen continuously in the port 1111.
2. Execute the UDPClient.java class which will send the data using Datagram Socket to the local host in port 1111.
Output :
UDP Server Listening in 1111
UDPServer: Message received = Techdive.in
Original Question:
Using C programing language. Network I/O. Write a server and client program to communicate using the UDP protocol. Both programs can be run on the same machine, or each program on different machines. As the UDP protocol is connectionless, no connect(3) need be made (except in the extra credit enhancement; see below). The server is to be invoked with 2 command line arguments: the address to which to bind the server process, and the port with which to bind the server process. The address should be in the form a.b.c.d and the port a number greater than 1024 and less than 64K. Both the address and port must be stored in the sockaddr_in structure in network standard formal (big endian). If you use either inet_addr(3) or inet_pton(3) to convert the human readable address into a 4-byte integer, these routines will ensure than the result is in network standard format. You will have to explicitly convert the port using htons(3). Algorithm for the server: Convert the command line arguments and initialize the sockaddr_in structure Open a socket (using socket(3)) for the AF_INET family, the SOCK_DGRAM type, and the IPPROTO_UDP protocol Bind the server (using bind(3)) to the address and port in the sockaddr_in structure Block on waiting for an incoming message (using recvfrom(3)) Display the message source (address and port) and the message contents The client is to be invoked with 2 command line arguments: the (remote) address and port of the server. Algorithm for the client: Convert the command line arguments and initialize the sockaddr_in structure Open a socket (using socket(3)) for the AF_INET family, the SOCK_DGRAM type, and the IPPROTO_UDP protocol Send a simple ASCII message to the server (using sendto(3)) Sample output, starting the server first in the background: rcm$ server 192.168.1.15 5000 & [1] 13447 rcm$ Server: waiting for message rcm$ client 192.168.1.15 5000 Client: send message to 192.168.1.15, port 5000 Client: message sent Server: Received from 192.168.1.15, port 58312: Hello world! [1]+ Done server 192.168.1.15 5000 Extra Credit: enhance this program by adding a protocol parameter to the command line arguments so the program will use EITHER the UDP or TCP protocol, and can be invoked as follows: rcm$ server UDP 192.168.1.15 5000 & rcm$ client UDP 192.168.1.15 5000 OR rcm$ server TCP 192.168.1.15 5000 & rcm$ client TCP 192.168.1.15 5000
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
