开发者

How to create a basic Java Server? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answ开发者_如何学运维ers and spam. Instead, describe the problem and what has been done so far to solve it.

Closed 9 years ago.

Improve this question

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


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.


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.


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


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.


I would start by looking at multicasting in Java:

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


you will probably need to use the serversocket class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜