Implement a UDP echo program using socket programming in either C, C++, Java or
ID: 3721671 • Letter: I
Question
Implement a UDP echo program using socket programming in either C, C++, Java or Python.
An “echo” function may be created in your Server file while a “get_stdin” function may be created in your Client file.
Server:
Create a socket, IP protocol family, modified UDP protocol.
Establish IP address and port number.
Port numbers are network byte order, please convert it to host byte order and print it.
Echo every datagram.
Echo:
Need to know how big address struct is, len must be set before the call.
Read a datagram from the socket Print out the address of the sender.
Print out if there are errors.
If yes, please print out the correct sections.
Client:
Create a socket, IP protocol family, modified UDP protocol.
Create a sockaddr that will be used to contact the server.
Fill in an address structure that will be used to specify the address of the server you want to connect to.
Server IP address is found by calling with the name of the server.
Copy the IP address into the sockaddr.
Establish the server port number.
Read everything possible and send it to the echo server.
Wait for a reply.
Send what you get back to stdout.
get_stdin:
reads from standard input until EOF is found or the maximum bytes have been read.
Explanation / Answer
UDPCLient.java:
import java.io.*;
import java.net.*;
public class UDPClient{
public static void main(String args[]) throws Exception{
try {
BufferedReader infromUser = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
while(true) {
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
System.out.println("Enter data:");
String sentence = infromUser.readLine();
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String receivedSentence = new String(receivePacket.getData());
System.out.println("From server:" + receivedSentence);
}
//clientSocket.close();
}catch (SocketTimeoutException ex) {
System.out.println("Timeout error: " + ex.getMessage());
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("Client error: " + ex.getMessage());
ex.printStackTrace();
}
}
}}
UDPServer.java:
import java.io.*;
import java.net.*;
public class UDPServer {
public static void main(String[] args) throws Exception{
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
BufferedReader infromUser = new BufferedReader(new InputStreamReader(System.in));
while(true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String(receivePacket.getData());
System.out.println("Received from "+receivePacket.getAddress() + ": " + sentence);
InetAddress IPAddress = receivePacket.getAddress();
int Port = receivePacket.getPort();
System.out.println("Enter data:");
String response = infromUser.readLine();
sendData = response.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,IPAddress, Port);
serverSocket.send(sendPacket);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.