john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

Java Chat Server Alpha

import java.net.*;
import java.io.*;

public class ChatServe {
    public static void main(String[] args) throws IOException {

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(1234);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 1234.");
            System.exit(1);
        }

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                clientSocket.getInputStream()));
        String inputLine, outputLine;
        ChatPt cp = new ChatPt();

        outputLine = cp.processInput(null);
        out.println(outputLine);

        while ((inputLine = in.readLine()) != null) {
             outputLine = cp.processInput(inputLine);
             out.println(outputLine);
             if (outputLine.equals("Bye."))
                break;
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}

  • « Java Chat Client Alpha
  • Java Chat Processor Alpha »

Published

Mar 29, 2001

Category

java-chat-university

~79 words

Tags

  • chat 19
  • java 252
  • server 66