Write a client–server application using Java sockets that allows a client to wri
ID: 3670220 • 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.
Please test the program before giving the answer because I already got a non-working code.
Explanation / Answer
Server:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server
{
private static Socket socket;
public static void main(String[] args)
{
try
{
int port = 6100;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 25000");
//Server is running always. This is done using this while(true) loop
while(true)
{
System.out.println("Message received from client is ");
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message received from client is "+message);
char ch;
int i,j=0,k=0,l=0;
//Multiplying the number by 2 and forming the return message
String returnMessage;
l = message.length();
for (i=0;i<l;i++){
ch = message.charAt(i);
if (Character.isLetter(ch))
++j;
else if(Character.isDigit(ch))
++k;
}
returnMessage = String.valueOf(j) +' '+ String.valueOf(k)+ " ";
//Sending the response back to the client.
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(returnMessage);
System.out.println("Message sent to the client is "+returnMessage);
bw.flush();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){}
}
}
}
Client:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
public class Client
{
private static Socket socket;
public static void main(String args[])
{
try
{
String host = "localhost";
int port = 6100;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String sendMessage = "jhajshkjfhs1234 ";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);
//Get the return message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String message = br.readLine();
System.out.println("Message has characters and digits: " +message);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.