How to create a basic Java Server?

92,718

Solution 1

Here is a simple "Knock Knock" server courtesy of Sun:

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

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

        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(4444);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            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;
        KnockKnockProtocol kkp = new KnockKnockProtocol();

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

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

You can't get much simpler than this.

Solution 2

There is a straightforward tutorial available via Sun:

http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/socket.html#server

It starts with a basic single thread as above and extends to use multiple as required.

Solution 3

Try using the Jetty server API. http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty looks like a good starting point...

Share:
92,718

Related videos on Youtube

IApp
Author by

IApp

Updated on July 09, 2022

Comments

  • IApp
    IApp almost 2 years

    Essentially I want a basic Java Server which multiple people can be connected to and when one of the connected clients (Already coded in Obj-c) sends data to it, it sends it back to everyone who is connected.

    I'm a real Java Newbie and I'm not going to need Java in the forseeable future for anything but this so I want it out the way as soon as possible rather than learning Java properly from scratch. So if anyone has some source code for this or perhaps a tutorial it would be greatly appreciated.

    Thanks :) Ozzie

    • Kaleb Pederson
      Kaleb Pederson about 14 years
      What kind of protocol are the clients using?
    • AlikElzin-kilaka
      AlikElzin-kilaka almost 10 years
      Voted to reopen - SO really need to figure out how to handle questions that may have several good answers.
  • IApp
    IApp about 14 years
    Yeh, I managed to get a basic PHP socket server working but it was only able to connect 1 client at a time. Unless it's Actionscript or Objective-C I'm lost basically!
  • IApp
    IApp about 14 years
    Thanks! Ill take a look! The only problem is is that I have literally no Java knowledge whatsoever so I find it difficult to understand quite a lot of what they are on about. I only really need a Java Server to demonstrate my client. Although I am actually planning to learn Java next year in line with the university course that I'm doing!
  • IApp
    IApp about 14 years
    Yeh I saw a little about that but find it quite difficult to understand. Thanks though :)
  • IApp
    IApp about 14 years
    I did have a quick look over that earlier today and I coded something similar in PHP which worked great for earlier testing. At the moment though I'm looking to connect and send to multiple users, apparently i need Multi-threading? Do you know how I'd go about doing that on the example you have provided?
  • David Titarenco
    David Titarenco about 14 years
    kieser.net/linux/java_server.html - you simply make a class that handles connections and invoke it via thread()
  • Philippe
    Philippe about 14 years
    Yeah it requires some knowledge of Java even though it can be quite simple for simple web based Request/response scenarii. I think Nissan Fan's answer is better for your use case.
  • Galen Nare
    Galen Nare about 10 years
    what is KnockKnockProtocol?
  • AlikElzin-kilaka
    AlikElzin-kilaka almost 10 years
    @IApp - For the project you described, I do not recommend using any frameworks (Jetty is a framework). KISS - Keep It Short & Simple.
  • AlikElzin-kilaka
    AlikElzin-kilaka almost 10 years
    BTW, currently, web-server frameworks have poor support by IDEs.
  • Asenar
    Asenar over 9 years
    @GalenNare It's explained in an official tutorial. You might have to adapt the tutorial class