Java You are to create a direct messenger program. In particular, the program wi
ID: 3754926 • Letter: J
Question
Java
You are to create a direct messenger program. In particular, the program will perform as follows:
It must behave as either a client or a server, depending on the command line arguments supplied when started.
Specifically, the -l option will direct the program to listen for connections, thereby acting as a server. If the -l option is not present on the command line, your program will connect to a server, thereby acting as a client.
The port number must also be supplied as a command line argument.
Although your messenger may be hard-coded to connect only to localhost when acting as a client, you may include a server address on the command line as well, thereby permitting your messenger to connect between hosts.
The -l option, if used, should be placed immediately after the name of the program and before the port number and server address arguments.
Once the client and server messengers are connected, either end can send text messages to the other end.
Messages must be provided to the program using standard input.
Immediately after receiving a message, the program must output the message to standard output.
The program is terminated by first shutting down sending, then closing the socket. Once one end shuts down, the other side will receive a zero-length message, informing it of the shut down. The other side must then shut down immediately.
Usage
Usage as a server: Java:
Usage as a client: Java
Explanation / Answer
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
/**
*
* @author VISHAL
*/
public class Messenger
{
// constructor to put ip address and port
public void Client(String address, int port) throws IOException
{
Socket socket = null;
DataInputStream input = null;
DataOutputStream out = null;
// establish a connection
try
{
socket = new Socket(address, port);
System.out.println("Connected to server");
// takes input from terminal
input = new DataInputStream(System.in);
// sends output to the socket
out = new DataOutputStream(socket.getOutputStream());
}
catch(UnknownHostException u)
{
System.out.println(u);
}
catch(IOException i)
{
System.out.println(i);
}
// string to read message from input
String line = "TEST";
// keep reading until "Over" is input
while (line.length()!=0)
{
try
{
line = input.readUTF();
out.writeUTF(line);
}
catch(IOException i)
{
System.out.println("Error in INPUT OUTPUT");
}
}
// close the connection
try
{
input.close();
out.close();
socket.close();
}
catch(IOException i)
{
System.out.println("Closin Connection");
}
}
public void Server(int port)
{
//initialize socket and input stream
Socket socket = null;
ServerSocket server = null;
DataInputStream in = null;
// starts server and waits for a connection
try
{
server = new ServerSocket(port);
System.out.println("Server started");
System.out.println("Waiting for a client ...");
socket = server.accept();
System.out.println("Client accepted");
// takes input from the client socket
in = new DataInputStream( new BufferedInputStream(socket.getInputStream()));
String line = "TEST";
// reads message from client until "Over" is sent
while (line.length()!=0)
{
try
{
line = in.readUTF();
System.out.println(line);
}
catch(IOException i)
{
System.out.println(i);
}
}
System.out.println("Closing connection ");
// close connection
socket.close();
in.close();
}
catch(IOException i)
{
System.out.println("CLOSING SERVER");
}
}
public static void main(String args[]) throws IOException
{
Messenger M = new Messenger();
int port;
if(args.length!=2)
{
System.out.println("Invalid Number of argument input");
return;
}
if(args[0].equals("-I"))
{
port=Integer.parseInt(args[1]);
M.Server(port);
}
else
{
String serveraddress=args[1];
port=Integer.parseInt(args[0]);
M.Client(serveraddress, port);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.