Skip to content
Snippets Groups Projects
AngelServer.java 3.02 KiB
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

// no package required or desired, use Netbeans menu item "Run File"
import java.io.*;
import java.net.*;
/**
 *
 * @author cs2017
 */
public class AngelServer {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    boolean goforit = true;
    try
        {
            // ServerSocket waits for a connection from a client. 
            // Notice that it is outside the loop; ServerSocket
            // needs to be made only once.
            
            ServerSocket serverSocket = new ServerSocket(2317);
            // Loop, infinitely, waiting for client connections.
            // Stop the program somewhere else.
            while(goforit)
            {
                Socket clientConnection = serverSocket.accept();
                OutputStream os = clientConnection.getOutputStream();
                PrintStream ps = new PrintStream(os);
                

                
                // Print some information locally about the Socket
                // connection. This includes the port and IP numbers
                // on both sides (the socket pair.)
                
                InetAddress localAddress = clientConnection.getLocalAddress();
                InetAddress remoteAddress = clientConnection.getInetAddress();
                
                int localPort = clientConnection.getLocalPort();
                int remotePort = clientConnection.getPort();
                
                // My socket pair connection looks like this, to localhost:
                // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54876 ))
                // Socket pair: (( /0:0:0:0:0:0:0:1, 2317 ), ( /0:0:0:0:0:0:0:1, 54881 ))
                //
                // Why is the first IP/port the same, while the second set has
                // different ports?


                System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + 
                        remoteAddress.toString() + ", " + remotePort + " ))");
                
                // Notice the use of flush() and close(). Without
                // the close() to Socket object may stay open for 
                // a while after the client has stopped needing this
                // connection. Close() explicitly ends the connection.
                ps.println("Abrams 1");
                ps.println(60);
                ps.println(152);
                ps.println(25);
                ps.println(30);

                ps.println("Exit");
                
                ps.flush();
                clientConnection.close();
            }
       }
        catch(Exception e)
        {
            System.out.println("problem with networking");
        }
       
    }
    
}