Skip to content
Snippets Groups Projects
SeversonAssignment1.java 2.91 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.
 */
package MV3500Cohort2018JulySeptember.homework1;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * homework assignment
 * @author Peter
 */
public class SeversonAssignment1
{
    /**
     * Default constructor to silence javadoc warning
     * @see <a href="https://stackoverflow.com/questions/4488716/java-default-constructor">StackOverflow: Java default constructor</a>
     */
    public SeversonAssignment1 ()
    {
        // default initializations 
    }

    /**
     * Program invocation, execution starts here
     * @param args command-line arguments
     */
    public static void main(String[] args) {

        try {
            
            //Instantiate socket and wait for connection
            ServerSocket serverSocket = new ServerSocket(2317);
            
            //Count the number of connections
            int connection = 0;
            
            //Loop until client connection is made
            while (true) {

                //Socket making connection with the client
                Socket clientConnection = serverSocket.accept();

                //number of connections is incremented by one when the connection occurs
                connection++;
                
                //write output text to the client and server
                OutputStream os = clientConnection.getOutputStream();
                PrintStream ps = new PrintStream(os);

                //connection responses to the server and client
                ps.println("This client response was written by server"); // to remote client
                System.out.println("This server response was written by server"); // to server console
                ps.println("Got another connection! " + "--Number " + connection);
                
                //Print port and IP addresses of the server connection
                InetAddress  localAddress = clientConnection.getLocalAddress();
                InetAddress remoteAddress = clientConnection.getInetAddress();
                
                int  localPort = clientConnection.getLocalPort();
                int remotePort = clientConnection.getPort();
                
                System.out.println("Socket pair: (( " + localAddress.toString() + ", " + localPort + " ), ( " + remoteAddress.toString() + ", " + remotePort + " ))");
                //close the connection and inform the client of the close
                ps.println("The client connection is closed ---- BYE!");
                ps.flush();
                clientConnection.close();

            }

        } catch (IOException ex) {

            System.out.println("problem with networking: " + ex);

        }

    }

}