Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a client–server application using Java sockets that allows a client to wri

ID: 3673686 • Letter: W

Question

Write a client–server application using Java sockets that allows a client to write a message (as a String) to a socket. A server will read this message, count the number of characters and digits in the message, and send these two counts back to the client. The server will listen to port 6100. The client can obtain the String message that it is to pass to the server either from the command line or by using a prompt to the user. One strategy for sending the two counts back to the client is for the server to construct an object containing

a. The message it receives from the client

b. A count of the number of characters in the message

c. A count of the number of digits in the message

please comment well

Explanation / Answer

//Programs:

//Server.java

import java.io.*;
import java.net.*;


public class Server {

  
   public static void main(String[] args) throws Exception{
       System.out.println("Server started ");      
      
       /*Create new UDP Socket and Bind or attach it to the port number. This acts as an ID for this process. */
       DatagramSocket recvSkt = new DatagramSocket(6100);
      
       /*Create a Receive Buffer to receive an incoming UDP packet. The Buffer type is bytes */
       byte[] recvBytes = new byte[1024];
      
       /*Infinite Loop -- keep receiving packets until program is terminated*/
       while(true){
           /*receive the packet. Received information is written into the buffer we created earlier.*/
           DatagramPacket recvPkt = new DatagramPacket(recvBytes, recvBytes.length);
           recvSkt.receive(recvPkt);
InetAddress senderAddr = recvPkt.getAddress();
           String recvStr = new String(recvBytes, 0, recvPkt.getLength());
  
// Preparing reply message
/* a. count the number of characters and digits*/
int NumOfDigits=0;
int NumOfChars=0;
System.out.println(recvStr);
char array[]=recvStr.toCharArray();
  
/* Counting characters and digits*/
for(int i=0;i<array.length;i++)
{
if(Character.isDigit(array[i]))
{
NumOfDigits++;
}
else
{
NumOfChars++;
}
}
// create reply object
Reply reply=new Reply(recvBytes,NumOfChars,NumOfDigits);
/* To write Reply object in output stream */
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(outputStream);
os.writeObject(reply);
  
byte[] data = outputStream.toByteArray();
// creating datagram pocket
DatagramPacket dp = new DatagramPacket(data , data.length , senderAddr , recvPkt.getPort());
//send the pocket to client
recvSkt.send(dp);
       }

   }

}

//Client.java

import java.io.*;
import java.net.*;


public class Client {

   public static void main(String[] args) throws Exception{
       //Create a BufferedReader using an InputStreamReader on standard input (console)
       BufferedReader userIP = new BufferedReader(new InputStreamReader(System.in));
  
try (
//Create a new UDP Socket.
DatagramSocket sndrSkt = new DatagramSocket()) {

/*Get the receiver's IP address.*/
InetAddress IPAddress = InetAddress.getByName("127.0.0.1");
while(true)
{
//sending the packets
//Get the user input and convert that to Byte/Binary format
System.out.println("Enter the to send:");
String userStr = userIP.readLine();
byte[] sendData = userStr.getBytes();
  
//Create a UDP packet
DatagramPacket sendPkt = new DatagramPacket (sendData, sendData.length, IPAddress, 6100);

//Send the packet
sndrSkt.send(sendPkt);
  
  
//Receiving part
byte[] recvBytes = new byte[1024];
//receive pocket
DatagramPacket recvPkt = new DatagramPacket(recvBytes, recvBytes.length);
       sndrSkt.receive(recvPkt);
  
byte[] data = recvPkt.getData();
ByteArrayInputStream in = new ByteArrayInputStream(data);  
ObjectInputStream is = new ObjectInputStream(in);
//convert the data to reply object
try {
Reply reply=(Reply) is.readObject();
System.out.println(reply.toString());  
} catch (IOException | ClassNotFoundException e) {  
}
}

}
  
   }

}

//Reply.java

import java.io.Serializable;
import java.util.Arrays;

public class Reply implements Serializable
{
byte[] recvBytes;
int numOfChars;
int numOfDigits;
Reply(byte[]Bytes, int chars, int digits)
{
recvBytes=Bytes;
numOfChars=chars;
numOfDigits=digits;
}
@Override
public String toString()
{
String msg="Data:"+Arrays.toString(recvBytes)+" Number of characters:"
+ " "+numOfChars+" Number of Digits: "+numOfDigits;
return msg;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote