networking authentication program 1.0
Submitted By:
manoveg
Rating:
(Not rated) (
Rate It)
package client;
//import server.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
public class Client2 {
static int count=0;
public static void main() {
// make sure you give the port number on which the server is listening.
int serverPort = 8189;
// this is the IP address of the server program's computer.
// the address given here means "the same computer as the client".
String address = "127.0.0.1";
try
{
// create an object that represents the above IP address.
InetAddress ipAddress = InetAddress.getByName(address);
System.out.println("Any of you heard of a socket with IP address " + address + " and port " + serverPort + "?");
// create a socket with the server's IP address and server's port.
Socket socket = new Socket(ipAddress, serverPort);
System.out.println("Yes! I just got hold of the program.");
// Get the input and output streams of the socket, so that you can receive and send data to the client.
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
// Just converting them to different streams, so that string handling becomes easier.
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
// Create a stream to read from the keyboard.
//BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
String line = null;
//System.out.println("Type in something and press enter.");
System.out.println();
// while(true) {
// wait for the user to type in something and press enter.
//line = keyboard.readLine();
System.out.println("Sending this line to the server...");
// send the above line to the server.
method m=new method();
line=m.sclientclass("","");
out.writeUTF(line);
out.flush(); // flush the stream to ensure that the data reaches the other end.
// wait for the server to send a line of text.
line = in.readUTF();
if(line.equals("Login successful"))
{
CDialogBox12 cd = new CDialogBox12(new CFrontEnd(),"Status",line);
cd.setVisible(true);
}
else
{
if(line.equals("Login Failed"))
{
count++;
CDialogBox16 cd1 = new CDialogBox16(new CFrontEnd(),"Status",line,count);
cd1.setVisible(true);
}
}
System.out.println("The server was very polite. It sent me this : " + line);
//}
} catch(Exception x) {
x.printStackTrace();
}
}
}