In: Computer Science
For the assignment you have to do two thing
.................................
import java.io.*;
import java.net.*;
public class Client {
public static void main(String[] args) throws IOException {
String serverHostname = new String ("127.0.0.1");
if (args.length > 0) {
//pass the hsotname through cmd argument
serverHostname = args[0];
}
System.out.println ("Attemping to connect to host " + serverHostname + " on
port 10007.");
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
//Connect to server and open IO stream
echoSocket = new Socket(serverHostname, 10007);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: " + serverHostname);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for " + "the connection to: " +
serverHostname);
System.exit(1);
}
BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in));
String userInput;
System.out.print ("input-comand: ");
while ((userInput = stdIn.readLine()) != null) {
//send to server using socket
out.println(userInput);
//read reply from server
String replyFromServer = in.readLine();
System.out.println("Answer: " + replyFromServer);
if(replyFromServer.trim().toLowerCase().equals("bye")){
break;
}
System.out.print ("input-command: ");
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
--------------------------------------------
import java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(10007);
}
catch (IOException e) {
System.err.println("Could not listen on port: 10007.");
System.exit(1);
}
Socket clientSocket = null;
System.out.println ("Waiting for connection ...");
try {
clientSocket = serverSocket.accept();
}
catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
System.out.println ("Connection successful");
System.out.println ("Waiting for input ...");
//open IO streams
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader( clientSocket.getInputStream()));
String inputLine;
//wait and read input from client. Exit when input from socket is Bye
while ((inputLine = in.readLine()) != null)
{
System.out.println ("Server: " + inputLine);
//reply back to client
out.println(inputLine);
if (inputLine.trim().toLowerCase().equals("bye"))
break;
}
//close IO streams and at the end socket
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
---------------------------------
DNS-table.txt
www.google.com 192.168.73.1
www.uncc.edu 192.168.73.2
www.abc.com 192.168.73.3
www.espn.com 192.168.73.4
www.cyberDNA.com 192.168.73.55
www.ccaa.uncc.edu 192.168.73.5
www.ccigrad.uncc 192.168.73.13
www.cis.uncc.edu 192.168.73.14
www.unc.edu 192.168.73.12
//first run Server then client....
//Java code
import java.io.*; import java.net.*; public class Client { public static void main(String[] args) throws IOException { String serverHostname = new String ("127.0.0.1"); if (args.length > 0) { //pass the hsotname through cmd argument serverHostname = args[0]; } System.out.println ("Attemping to connect to host " + serverHostname + " on port 10007."); Socket echoSocket = null; PrintWriter out = null; BufferedReader in = null; try { //Connect to server and open IO stream echoSocket = new Socket(serverHostname, 10007); out = new PrintWriter(echoSocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader( echoSocket.getInputStream())); } catch (UnknownHostException e) { System.err.println("Don't know about host: " + serverHostname); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for " + "the connection to: " + serverHostname); System.exit(1); } BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; System.out.print ("input-comand: "); while ((userInput = stdIn.readLine()) != null) { //send to server using socket out.println(userInput); //read reply from server String replyFromServer = in.readLine(); System.out.println("Answer: " + replyFromServer); if(replyFromServer.trim().toLowerCase().equals("bye")){ break; } System.out.print ("input-command: "); } out.close(); in.close(); stdIn.close(); echoSocket.close(); } }
//=================================================
import java.net.*; import java.io.*; import java.util.Scanner; public class Server { public static void main(String[] args) throws IOException { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(10007); } catch (IOException e) { System.err.println("Could not listen on port: 10007."); System.exit(1); } Socket clientSocket = null; System.out.println ("Waiting for connection ..."); try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } System.out.println ("Connection successful"); System.out.println ("Waiting for input ..."); //open IO streams PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); BufferedReader in = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); String inputLine; //wait and read input from client. Exit when input from socket is Bye Scanner scanFile = new Scanner(new File("DNS-table.txt")); boolean found =false; while ((inputLine = in.readLine()) != null) { System.out.println ("Server: " + inputLine); String line =""; if (inputLine.trim().toLowerCase().equals("bye")) { out.println(inputLine); break; } while (scanFile.hasNextLine()) { //reply back to client line = scanFile.nextLine(); String[] tokens = line.split(" "); if(inputLine.equalsIgnoreCase(tokens[0]) || inputLine.equalsIgnoreCase(tokens[1])) { found = true; break; } else found = false; } if(found) { out.println(line); } else { out.println("Not found..."); } } //close IO streams and at the end socket scanFile.close(); out.close(); in.close(); clientSocket.close(); serverSocket.close(); } }
//text file
//output
//If you need any help regarding this solution ........... please leave a comment .......... thanks