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

Problem: Write a client and a server program where the server can be connected b

ID: 3870899 • Letter: P

Question

Problem: Write a client and a server program where the server can be connected by multiple clients. The clients will send a message (a complete sentence) to the server and the server will count the total number of vowels present in that message and will return that number to the client. The client will print that number. If the client wants to close the connection, it will send “Bye” message to the server. Upon receiving that message, the server will close that particular connection.

Requirements:

1. Create a C/ Java based client-server architecture using sockets

2. The server should be able to accept and service multiple client’s requests one at a time

3. The server should be run on cse01.cse.unt.edu machine and the clients should be run on any machine between cse02.cse.unt.edu and cse06.cse.unt.edu. The IP address of cse01.cse.unt.edu is 129.120.151.94 Example: Client sends a message “A quick brown fox jumps over the lazy dog” to the server The sever counts the number of vowels present in that message, i.e. 11. It sends that number to the corresponding client. The client then print the message “The number of vowels present is 11”.

Instructions: Compile your programs (both client and server) and make sure they are working. Your code should be well commented and indented. Create a Readme file (text file) and write how to compile and execute your code. Please create a zip archive of your assignment folder (code, header files if any and Readme file) and upload the zip file. Not following the above instructions could result up to 20% deduction from your programming assignment score. Late submissions are not allowed.

Explanation / Answer

Write a client and a server program where the server can be connected by multiple clients. The clients will send a message (a complete sentence) to the server and the server will count the total number of vowels present in that message and will return that number to the client.
// Chat client

import java.net.*;

import java.io.*;

import java.util.*;

import java.awt.*;

class chatClient extends Frame implements Runnable { Socket soc;

TextField tf;

TextArea ta;

Button btnSend,btnClose;

String sendTo;

String LoginName;

Thread t=null;

DataOutputStream dout;

DataInputStream din;

chatClient(String LoginName,String chatwith) throws Exception { super(LoginName);

this.LoginName=LoginName;

sendTo=chatwith;

tf=new TextField(50);

ta=new TextArea(50,50);

btnSend=new Button("Send");

btnClose=new Button("Close");

soc=new Socket("127.0.0.1",5217);

din=new DataInputStream(soc.getInputStream()); dout=new DataOutputStream(soc.getOutputStream()); dout.writeUTF(LoginName);

t=new Thread(this);

t.start();

} void setup() {

setSize(600,400);

setLayout(new GridLayout(2,1));

add(ta); Panel p=new Panel();

p.add(tf);

p.add(btnSend);

p.add(btnClose);

add(p);

show();

} public boolean action(Event e,Object o) { if(e.arg.equals("Send"))

{ try

{ dout.writeUTF(sendTo + " " + "DATA" + " " + tf.getText().toString());

ta.append(" " + LoginName + " Says:" + tf.getText().toString());

tf.setText("");

} catch(Exception ex) { }

} elseif(e.arg.equals("Close"))

{ try

{ dout.writeUTF(LoginName + " LOGOUT"); System.exit(1); } catch(Exception ex) { }

} return super.action(e,o);

} publicstaticvoid main(String args[]) throws Exception { chatClient Client1=new chatClient(args[0],args[1]); Client1.setup();

} publicvoid run() { while(true) { try { ta.append( " " + sendTo + " Says :" + din.readUTF());

} catch(Exception ex) { ex.printStackTrace();

} } } } // Chat Server

import java.net.*;

import java.util.*;

import java.io.*;

class chatServer { static Vector ClientSockets; static Vector LoginNames;

chatServer() throws Exception { ServerSocket soc=new ServerSocket(5217);

ClientSockets=new Vector();

LoginNames=new Vector();

while(true) { Socket CSoc=soc.accept();

AcceptClient obClient=new AcceptClient(CSoc); } } publicstaticvoid main(String args[]) throws Exception { chatServer ob=new chatServer();

}

class AcceptClient extends Thread { Socket ClientSocket; DataInputStream din;

DataOutputStream dout;

AcceptClient (Socket CSoc) throws Exception { ClientSocket=CSoc;

din=new DataInputStream(ClientSocket.getInputStream()); dout=new DataOutputStream(ClientSocket.getOutputStream()); String LoginName=din.readUTF();

System.out.println("User Logged In :" + LoginName); LoginNames.add(LoginName); ClientSockets.add(ClientSocket);

start();

}

publicvoid run()

{ while(true)

{ try

{ String msgFromClient=new String(); msgFromClient=din.readUTF();

StringTokenizer st=new StringTokenizer(msgFromClient); String Sendto=st.nextToken();

String MsgType=st.nextToken();

int iCount=0; if(MsgType.equals("LOGOUT")) { for(iCount=0;iCount<LoginNames.size();iCount++) { if(LoginNames.elementAt(iCount).equals(Sendto)) { LoginNames.removeElementAt(iCount); ClientSockets.removeElementAt(iCount); System.out.println("User " + Sendto +" Logged Out ..."); break; } } } else { String msg=""; while(st.hasMoreTokens()) { msg=msg+" " +st.nextToken(); } for(iCount=0;iCount<LoginNames.size();iCount++) { if(LoginNames.elementAt(iCount).equals(Sendto)) { Socket tSoc=(Socket)ClientSockets.elementAt(iCount); DataOutputStream tdout=new DataOutputStream(tSoc.getOutputStream()); tdout.writeUTF(msg); break; } } if(iCount==LoginNames.size()) { dout.writeUTF("I am offline"); } else { } } if(MsgType.equals("LOGOUT")) { break; } } catch(Exception ex) { ex.printStackTrace(); } } } } }

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