Skip to content
Snippets Groups Projects
AyresAssignment1.java 2.17 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.*;
import java.net.*;

/**
 * homework assignment
 * @author kjayr
 */
public class AyresAssignment1
{
    /**
     * Default constructor to silence javadoc warning
     * @see <a href="https://stackoverflow.com/questions/4488716/java-default-constructor">StackOverflow: Java default constructor</a>
     */
    public AyresAssignment1 ()
    {
        // default initializations occur here
    }
    /** run the program
     * @param args command-line arguments, string parameters (unused) */
    public static void main(String[] args) 
    {
        try
        {
            System.out.println("socket creation running");
                    
            // The ServerSocket waits for a connection from a client.
            // It returns a Socket object when the connection occurs.
            ServerSocket serverSocket = new ServerSocket(2317);
            
            // The Socket object represents the connection between
            // the server and client, including a full duplex
            // connection
            Socket clientConnection = serverSocket.accept();
            
            // Use Java io classes to write text (as opposed to
            // unknown bytes of some sort) to the client
            OutputStream os = clientConnection.getOutputStream();
            PrintStream ps = new PrintStream(os);
            
                    ps.println("This client response was written by server Ayres"); // to remote client
            System.out.println("This server response was written by server Ayres"); // to server console
                    ps.println("All Done.");
            
            // "flush()" in important in that it forces a write 
            // across what is in fact a slow connection
            ps.flush();
            
            clientConnection.close();
        
        }
        catch(Exception err)
        {
            System.out.println("Something went wrong...problem with networking: " + err);
        }
    }
}