Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8783715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:53:37+00:00 2026-06-13T20:53:37+00:00

I have the following Situation. I have a Server class. I have a Client

  • 0

I have the following Situation.

I have a Server class.
I have a Client class.
I have a MultiServerThread class.

When a Client connects to a Server, the Server creates a new MultiServerThread, which is processing the Input from the Client. That way I can have multiple Clients. So far so good.
The connection goes via TCP.

A short example:

Server class:

...
public static void main(String[] args) throws IOException {     
    ServerSocket serverSocket = null;
    boolean listening = true;


    try {
        serverSocket = new ServerSocket(9999);
    } catch (IOException e) {
      System.err.println("Could not listen on port: " +  serverSocket.getLocalPort() + ".");
        System.exit(-1);
    }

    while (listening) {
        new MultiServerThread(serverSocket.accept()).start();
    }
    serverSocket.close();
}
...

Client class:

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

    socket = new Socket(hostname, port);
    out = new PrintWriter(socket.getOutputStream(), true);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    stdIn = new BufferedReader(new InputStreamReader(System.in));
    String fromServer;
    String fromUser;

    while ((fromServer = in.readLine()) != null) {
        System.out.println("Server: " + fromServer);
        if (fromServer.equals("Bye.")) {
            break;
        }

        fromUser = stdIn.readLine();
        if (fromUser != null) {
            System.out.println("Client: " + fromUser);
            out.println(fromUser);
        }
    }

    out.close();
    in.close();
    stdIn.close();
    socket.close();
}
...

MultiServerThread class:

...
public MultiServerThread(Socket socket) throws SocketException {
    super("MultiServerThread");
    this.socket = socket;
    //   dSocket = new DatagramSocket(4445);
}

public void run() {
    try {
        PrintWriter myOutput = new PrintWriter(socket.getOutputStream(), true);
        BufferedReader myInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        myOutput.println("Connected to client and ready to accept commands.");

        while ((clientInput = myInput.readLine()) != null) {

            //A SIMPLE LOGIN A USER
            if (clientInput.contains("!login")) {

            //save some string given by client into loggedUser
            String loggedUser = clientInput.substring(7);

            }

        myOutput.close();
        myInput.close();
        socket.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

What I need is:

I need to implement a notification that comes from a Server when for example the Username is “Bob”. If the username is “Bob”, the server should give a notification to the Client “Bob is here again!”. In my project/homework this should be done with datagrams in Java.

So if the clientinput is “!login bob” then a datagram packet with the message (“Bob is here again!”) should be sent to the client.

Question: Where exactly should I put the code of the Datagram request in? Can I put the datagram packet request into the MultiServerThread or into the Client?

It would be easier in the MultiServerThread because it already handles the !login.
Here:

     if (clientInput.contains("!login")) {
            //save some string given by client into loggedUser
            String loggedUser = clientInput.substring(7);

            //send datagram request to Server???

            }

But this is going against the principle of networking?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:53:39+00:00Added an answer on June 13, 2026 at 8:53 pm

    I got it working 😉

    I definied a udp port in the thread and client class…
    the client class got his port with arguments… it gave the udp Port to the thread… so both had the udp ports 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following situation - Have a MongoService class, which reads host, port, database
If I have a Server/Client application that both reference the same DLL which contains
So I have the following situation. I have a configuration class config.py that holds
I have following Situation, Server A sends some data (HTML form) to server B,
I have following situation. In a constructor of a pseudo class I attach a
I have following situation (simplified, of course): MyDomain.groovy: class MyDomain { MyAnotherDomain anotherDomain //
I have the following situation: An Conversations entity/table which has multiple Tags associated to
I have the following situation: I have class User with the follwing properies :
I have the following situation: User makes request to PAGE A that displays a
My situation is the following: I have a template wrapper that handles the situation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.