Skip to content
Snippets Groups Projects
Commit b4f6920d authored by Ayres, Kevin (CAPT)'s avatar Ayres, Kevin (CAPT)
Browse files

Ayres_Server_HW2

parent 03ac7083
No related branches found
No related tags found
No related merge requests found
/*
* 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.homework2.Ayres;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
*
* @author kjayr
*/
public class Ayres_Server {
@SuppressWarnings("ConvertToTryWithResources")
public static void main(String[] args)
{
try {
// ServerSocket waits for a connection from a client.
// Notice that it is outside the loop; ServerSocket
// needs to be made only once.
//Good reminder
System.out.println("TcpServer has started..."); // it helps debugging to put this on console first. Keep.
ServerSocket serverSocket = new ServerSocket(2317);
// Server is up and waiting (i.e. "blocked" or paused)
// Loop, infinitely, waiting for client connections.
// Stop the program somewhere else.
while (true)
{
Socket clientConnection = serverSocket.accept(); // block until connected to a client
// Set up the streams, Java style:
OutputStream os = clientConnection.getOutputStream();
PrintStream ps = new PrintStream(os);
//Message to client
ps.println("**Strong connection to Server** Client operating...");
//________________________
//Set up input streams/messages from client
InputStream is = clientConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String serverMessage = br.readLine();
System.out.println("==================================================");
System.out.println("The message the client sent was " + serverMessage);
//________________________
// 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.flush();
clientConnection.close(); // like it or not, you're outta here!
}
}
catch (IOException e) {
System.out.println("problem with networking");
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment