Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Mehrere Verbindungen mit Sockets?

Empfohlene Antworten

Veröffentlicht

Kann ich mehrere Clients auf einen Socket verbinden? Ich habe das ausprobiert und anschliessend mit Input und OutputStreams getestet, aber der "Server" erhielt immer nur die Daten von dem Client, der sich als erster verbunden hat. Wenn ich dann die Verbindung auf dem Client mit close() geschlossen habe, war gar keine Verbindung mehr möglich...

Vielleicht hilft dieser Codeausschnitt


import java.io.*;

import java.net.*;


public class MyServer extends Thread

{

	private ServerSocket sock;

	private int numQuestions;


	public MyServer()

	{

		super("MyServer");

		try

		{

			sock=new ServerSocket(4413);

			System.out.println("MyServer up and running ... ");

		}

		catch (IOException exc)

		{

			System.err.println("Error: couldn't create socket." + exc);

			System.exit(1);

		}

	}


	public static void main(String[] args)

	{

		MyServer server = new MyServer();

		server.start();

	}


	public void run()

	{

		Socket client = null;



		while (true)

		{

			if (sock==null)	return;


			try

			{

				client =sock.accept();

			}

			catch (IOException exc)

			{

				System.err.println("Error: couldn't connect to client.");

				System.exit(1);

			}


			try

			{

				InputStreamReader isr = new InputStreamReader(client.getInputStream());

				BufferedReader is = new BufferedReader(isr);

				PrintWriter os = new PrintWriter(new BufferedOutputStream(client.getOutputStream()), false);

				String outLine;


				outLine = processInput(null);

				os.println(outLine);

				os.flush();


				while (true)

				{

					String inLine = is.readLine();

					if (inLine.length() >0) outLine = processInput(inLine);

					else outLine = processInput("");

					os.println(outLine);

					os.flush();

					if (outLine.equals("Bye.")) break;

				}

				os.close();

				is.close();

				client.close();

			}

			catch (Exception exc)

			{

				System.err.println("Error: " + exc.getMessage());

				exc.printStackTrace();

			}

		}

	}

}


Hallo,

hast du die Input- und OutputReader auch wieder geschlossen?

Gruß Jaraz

@Jaraz

Ja, habe ich ... glaube ich zumindest :D morgen poste ich den Quellcode ...

@E-T

morgen wenn ich wieder in der Arbeit bin probiere ich es aus und gebe dann bescheid ob es hinhaut.

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.