DATA CLIENT:
package soc;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import javax.swing.JOptionPane;
/**
* Trivial client for the date server.
*/
public class DateClient {
/**
* Runs the client as an application. First it displays a dialog
* box asking for the IP address or hostname of a host running
* the date server, then connects to it and displays the date that
* it serves.
*/
public static void main(String[] args) throws IOException {
String serverAddress = JOptionPane.showInputDialog(
"Enter IP Address of a machine that is\n" +
"running the date service on port 9090:");
Socket s = new Socket(serverAddress, 9090);
BufferedReader input =
new BufferedReader(new InputStreamReader(s.getInputStream()));
String answer = input.readLine();
JOptionPane.showMessageDialog(null, answer);
System.exit(0);
}
}
DATE SERVER
package soc;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
/**
* A TCP server that runs on port 9090. When a client connects, it
* sends the client the current date and time, then closes the
* connection with that client. Arguably just about the simplest
* server you can write.
*/
public class DateServer {
/**
* Runs the server.
*/
public static void main(String[] args) throws IOException {
ServerSocket listener = new ServerSocket(9090);
try {
while (true) {
Socket socket = listener.accept();
try {
System.out.println("dateserver is started.........");
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
out.println(new Date().toString()+"welcome");
} finally {
socket.close();
}
}
}
finally {
listener.close();
}
}
}
No comments:
Post a Comment