views:

121

answers:

6

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

A: 

I would start by looking at multicasting in Java:

http://java.sun.com/docs/books/tutorial/networking/datagrams/broadcasting.html

Nissan Fan
A: 

you will probably need to use the serversocket class.

clamp
Yeh I saw a little about that but find it quite difficult to understand. Thanks though :)
A: 

I did a Java program that basically implemented a sort of chat between a client and a server. Used a socket to open up a port of the server that would hear incoming connections. You should have a thread hearing them and calling actions when ever a valid connection would come in.

JorgeO
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!
+1  A: 

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

Philippe
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!
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.
Philippe
+3  A: 

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.

David Titarenco
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?
http://www.kieser.net/linux/java_server.html - you simply make a class that handles connections and invoke it via thread()
David Titarenco
A: 

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.

Adam MacLeod