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

  • SEARCH
  • Home
  • 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 7712841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:43:42+00:00 2026-06-01T01:43:42+00:00

I am having some problems figuring out this issue. I have a server that

  • 0

I am having some problems figuring out this issue. I have a server that takes a string from a client, the first four characters of which act as a ‘command’ of sorts. rep: replaces the stored string, app: appends to it.

This works mostly fine, however when I use the rep: command, it repeats the client’s string twice. So, if I inputted rep:foo the server returns ‘foofoo’. I have tried analysing the code and can’t see any immediate problems.

When adding some test output commands to both the server and the client, to see what the variables hold, I get the result as expected (the string input on the client minus the command characters). The code for both classes is below:

Server:

import java.io.*;
import java.net.*;
import java.util.*;

public class SynchServer
{
    public static void main(String[] args) throws IOException
    {
    ServerSocket serverSocket = null;
    final int PORT = 1234;
    Socket client;
    ClientHandler2 handler; // thread for client
    int clientCount = 0;

    //set up server socket
    try
    {
        serverSocket = new ServerSocket(PORT);
    }
    catch(IOException ioEx)
    {
        System.out.println("Cannot open socket!");
        System.exit(1);
    }

    // client connections and related actions:
    do
    {
        // wait for client...
        client = serverSocket.accept();
        System.out.println("Client accepted...\n");
        // assign client a connection number
        clientCount++;
        // create thread
        handler = new ClientHandler2(client, clientCount);

        // run thread
        handler.start();
    }while(true);
    }
}

class ClientHandler2 extends Thread
{
    // declare thread variables
    private Socket client;
    private Scanner input;
    private PrintWriter output;
    private static String text = "";
    int clientNum; // picked up from main

    // constructor - set up socket and streams
    public ClientHandler2(Socket socket, int clientCount)
        throws IOException
    {
    client = socket;
    clientNum = clientCount;
    // streams...
    // from client
    input = new Scanner(client.getInputStream());
    // to client
    output = new PrintWriter(client.getOutputStream(), true);
    }

    // thread actions:
    public void run()
    {
    String head, tail, received;

    do
    {
        // read in line from client
        received = input.nextLine();
        // split input line in two - head is first four
        // characters for the command, tail is for rest of
        // line - the text to be manipulated:
        head = received.substring(0,4);
        tail = received.substring(4);
        // find command and choose relevant method to execute
        if(head.equals("rep:"))
        {
        replaceText(tail);
        }
        else if(head.equals("app:"));
        {
        appendText(tail);
        }
        // no further tests needed - makes server ignore
        // invalid commands (Add some kind of message?)

        // send modified (or not) string back to client:
        output.println(clientNum + ": " + text);
    }while(!received.equals("QUIT"));

    // close socket connection
    try
    {
        System.out.println("Closing connection...");
        client.close();
    }
    catch(IOException ioEx)
    {
        System.out.println("Unable to close connection!");
    }
    }

    private synchronized void replaceText(String value)
    {
    text = value;
    }

    private synchronized void appendText(String value)
    {
    text += value;
    }
}

Client:

import java.io.*;
import java.net.*;
import java.util.*;

public class SynchClient
{
    public static void main(String[] args) throws IOException
    {
    // declare variables
    InetAddress host = null;
    final int PORT = 1234;
    Socket socket;
    Scanner networkInput, keyboard;
    PrintWriter networkOutput;

    // assign host address:
    try
    {
        host = InetAddress.getLocalHost();
    }
    catch(UnknownHostException uhEx)
    {
        System.out.println("Host ID not found!");
        System.exit(1);
    }

    // Set up socket to server and IO streams:
    socket = new Socket(host, PORT);
    // from server
    networkInput = new Scanner(socket.getInputStream());
    // to server
    networkOutput = 
        new PrintWriter(socket.getOutputStream(),true);
    // user input
    keyboard = new Scanner(System.in);

    String message, response;

    do
    {
        // get user input
        System.out.print("Enter message ('QUIT' to exit): ");
        message = keyboard.nextLine();
        // validate user input - ensure string is >= 4 chars
        // long
        while(message.length() < 4)
        {
        System.out.print("Try again: ");
        message = keyboard.nextLine();
        }
        // send message to server:
        networkOutput.println(message);
        // received response from server
        response = networkInput.nextLine();
        // output server response
        System.out.println(response);
    }while(!message.equals("QUIT"));
    }
}

I really cannot figure this out, and while not vitally important, I’d like to know for reference what is going wrong. So, any hints would be nice.

  • 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-01T01:43:43+00:00Added an answer on June 1, 2026 at 1:43 am

    Dude.

    else if(head.equals("app:"));

    see the semicolon at the end there? 🙂 Remove that, and your problems should magically go away.

    edited to add: The semicolon at the end of the else block terminates the condition and the code in the brackets below is thus executed every iteration of the while loop in ClientHandler2.run()

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

Sidebar

Related Questions

I having some trouble figuring this problem out where I have one button which
Having some problems figuring out the regex to match this: function Array() { [native
I'm having some problems figuring out a solution to this problem. I want to
I'm having some problems figuring out how to organise data pulled off XML in
I'm having some problems with my WCF server and client connections. If I use
I'm having some trouble figuring out the best/Djangoic way to do this. I'm creating
I'm having some conceptual trouble on figuring out how to best implement this... I
Having problems figuring this out. trying to do a rescue_from NoMethodError, :with => :try_some_options
I'm having some trouble figuring out the right selector syntax for this problem and
Ok, I have been having a VERY hard time figuring this out. I guess

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.