> For the complete documentation index, see [llms.txt](https://www.socketio4j.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.socketio4j.org/server-api/server-api-4.0-de/erste-schritte/server-instance.md).

# Serverinstanz

## Erstellen eines Servers

Um einen Socket.IO-Server zu starten, erstellen Sie ein `Konfiguration`, setzen Sie die Bind-Adresse und den Port und starten Sie dann den Server.

```java
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(9092);

SocketIOServer server = new SocketIOServer(config);
```

{% hint style="warning" %}
Bei der Verwendung von Frameworks wie **Spring Boot**, stellen Sie sicher, dass Sie importieren `com.socketio4j.socketio.Configuration` und nicht Framework-Klassen mit demselben Namen (zum Beispiel `org.springframework.context.annotation.Configuration`), um Importkonflikte zu vermeiden.
{% endhint %}

## Starten des Servers

{% tabs %}
{% tab title="Synchron" %}

```java
server.start()
```

{% endtab %}

{% tab title="Asynchron" %}

```java
server.startAsync().addListener(future -> {
            if (future.isSuccess()) {
                System.out.println("Server started on " + config.getPort());
            } else {
                System.out.println("Error " + future.cause().getLocalizedMessage());
            }
        });
```

{% endtab %}
{% endtabs %}

## Stoppen des Servers

Stoppen Sie den Server beim Herunterfahren immer sauber (gracefully).

```java
server.stop();
```

{% hint style="info" %}
socketio4j fügt nach dem Start des Servers einen Shutdown-Hook hinzu, der den Server in bestimmten Szenarien sauber beendet, aber nicht in allen. Es wird stets empfohlen, den Server explizit zu stoppen.
{% endhint %}

## Vollständiges Beispiel

{% code title="Server.java" %}

```java
import com.socketio4j.socketio.Configuration;
import com.socketio4j.socketio.SocketIOServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SocketIoServerMain {
    
    private static final Logger log = LoggerFactory.getLogger(SocketIoServerMain.class);
    
    public static void main(String[] args) throws Exception {
        // Konfiguration erstellen
        Configuration config = new Configuration();
        config.setHostname("localhost");
        config.setPort(9092);

        // Server erstellen
        SocketIOServer server = new SocketIOServer(config);
        
        server.addConnectListener(client -> {
            log.info("[/] connected -> " + client.getSessionId());
        });
        server.addDisconnectListener(client -> {
            log.info("[/] disconnected -> " + client.getSessionId());
        });
        
        server.addEventListener("hi", String.class, (client, data, ack) -> {
            // im Client auf "reply" hören
            log.info("received data : " + data);
            client.sendEvent("reply", "hello");
        });
        // Server starten
        server.start();
        log.info("Socket.IO server started on port 9092");

        // JVM am Leben halten
        Thread.currentThread().join();
    }
}

```

{% endcode %}

{% hint style="info" %}
Prüfen [Ereignisse](https://www.socketio4j.org/events/events-de/) für dokumentation zu ereignisbehandlung&#x20;
{% endhint %}

## Hinweise

* `hostname` ist optional. Wenn nicht gesetzt, bindet der Server an alle Schnittstellen (`0.0.0.0` / `::0`).
* `port` **muss** vor dem Starten des Servers gesetzt werden.
* Threading, Transports und andere erweiterte Optionen können über `Konfiguration` angepasst werden, bevor Sie `start() aufrufen`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.socketio4j.org/server-api/server-api-4.0-de/erste-schritte/server-instance.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
