Subversion Repositories programming

Rev

Rev 189 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
186 ira 1
/*******************************************************************************
2
 * File: P1Server.java
3
 * Author: Ira W. Snyder (devel@irasnyder.com)
4
 * Class: CS380 - Computer Networking
5
 *
6
 * Assignment: Project #1
7
 * Date Last Modified: 2006-01-18
8
 *
9
 * Purpose: Opens a listening TCP Socket on port 1337. It echoes back
10
 *          every packet that it recieves.
11
 *
12
 ******************************************************************************/
13
 
14
import java.io.*;
15
import java.net.*;
16
 
17
public class P1Server
18
{
189 ira 19
    /* Instance variables */
187 ira 20
    public static final int portNumber = 1337;
188 ira 21
    public static final String exitSeq = "QUIT";
186 ira 22
 
187 ira 23
    /**
189 ira 24
     * Method: main
187 ira 25
     * Purpose: This method creates a listening socket on port 1337 and
26
     * accepts the first connection made to it. It then echoes back anything
27
     * sent to it, until it recieves a "QUIT", at which point it terminates.
28
     */
186 ira 29
    public static void main (String[] args) throws IOException
30
    {
31
        ServerSocket serverSocket = null;
32
 
33
        /* Try to open the port for listening */
34
        try
35
        {
36
            serverSocket = new ServerSocket (portNumber);
37
        }
38
        catch (IOException e)
39
        {
40
            System.err.println ("Could not listen on port: " + portNumber);
41
            System.exit(1);
42
        }
43
 
44
        Socket clientSocket = null;
45
 
46
        /* Try to accept the first connection that comes in */
47
        try
48
        {
49
            clientSocket = serverSocket.accept();
50
        }
51
        catch (IOException e)
52
        {
53
            System.err.println ("Accept failed.");
54
            System.exit(1);
55
        }
56
 
57
        /* Set up input and output streams */
58
        PrintWriter out = new PrintWriter (clientSocket.getOutputStream(), true);
59
        BufferedReader in = new BufferedReader(
60
                                new InputStreamReader(
61
                                    clientSocket.getInputStream()));
62
 
63
        /* Use Strings to hold the input and output */
64
        String inputLine, outputLine;
65
 
66
        /* Read input and echo back to output */
67
        while ((inputLine = in.readLine()) != null)
68
        {
189 ira 69
            /* Echo back what we recieved */
70
            out.println (inputLine);
190 ira 71
            System.out.println ("Recieved a packet");
72
 
186 ira 73
            /* If we recieve a message to quit, do so. */
188 ira 74
            if (inputLine.equals (exitSeq))
190 ira 75
            {
76
                System.out.println ("Recieved exitSeq");
186 ira 77
                break;
190 ira 78
            }
186 ira 79
        }
80
 
81
        /* Cleanup */
82
        out.close();
83
        in.close();
84
        clientSocket.close();
85
        serverSocket.close();
86
    }
87
}
88