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 8355629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:55:28+00:00 2026-06-09T09:55:28+00:00

i have a server that is as follows: import java.io.*; import java.net.*; import java.util.StringTokenizer;

  • 0

i have a server that is as follows:

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

public class MyTcpServer {

    static ServerSocket server = null;
    static Socket connectionSocket = null;
    static BufferedReader inFromClient = null;
    static PrintWriter outToClient = null;

    public static void initiateService() {
        try {
            server = new ServerSocket(1234);

            System.out.println("TCPServer Waiting for client on port 1234");
        } catch (IOException e) {
            System.out
                    .println("There is some error!! Please try some other port!");
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void awaitRequest() throws IOException {
        String instructionsToClient = null;
        System.out.println("Service Ready");
        while (true) {
            connectionSocket = server.accept();

            inFromClient = new BufferedReader(new InputStreamReader(
                    connectionSocket.getInputStream()));
            outToClient = new PrintWriter(connectionSocket.getOutputStream(),
                    true);

            String messageToClient;
            String messageFromClient = null;

            synchronized (Thread.currentThread()) {

                System.out.println("Client:"
                        + connectionSocket.getInetAddress()
                        + " connected on port :"
                        + connectionSocket.getLocalPort());

                messageToClient = "Welcome!!!! You are now connected to our server!! We ensure a reliable service.\n"
                        + "\n Your IP:"
                        + connectionSocket.getInetAddress()
                        + "  \n"
                        + connectionSocket.getLocalPort()
                        + "::::MESSAGE FROM SERVER::::\n"
                        + "Lets play Water jug puzzel!! You give me sizes of two water Jars\n"
                        + "And a desired level to be achieved! Ill tell you whether the problem\n"
                        + "is solvable or not!!\n"
                        + "Comprande~?\n"
                        + "Enter your choice Y/N \n"
                        + "--------------------------";

                synchronized (outToClient) {

                    outToClient.println(messageToClient);
                    outToClient.flush();

                    System.out.println("invite sent to the client!!");

                }

                synchronized (inFromClient) {

                    messageFromClient = inFromClient.readLine();
                    System.out.println("The user says: " + messageFromClient);

                }

                if (messageFromClient.equalsIgnoreCase("y")) {
                    messageToClient = "Wow!! awesome! Throw a challenge at me!"
                            + "\nEnter Large jug volume,Small jug volume,Desired level in comma separated fashion"
                            + "\nFor example (5,3,2)"
                            + "\n--------------------------";

                } else if (messageFromClient.equalsIgnoreCase("n")) {
                    messageToClient = "Great! we dont have a problem with you not playing the puzzel!"
                            + "Do you wanna exit? press q and smack Enter key!"
                            + "\n--------------------------";
                } else {
                    messageToClient = "psssst!!! Wanna quit??"
                            + "press q and smack Enter key!"
                            + "\n--------------------------";
                }

                synchronized (messageToClient) {

                    outToClient.println(messageToClient);
                    outToClient.flush();

                    System.out.println("reply sent to the client!!");

                }
                messageFromClient = "";
                synchronized (inFromClient) {
                    System.out.println("Entered");

                    messageFromClient = inFromClient.readLine();
                    System.out.println("The user says: " + messageFromClient);



                if (messageFromClient.equalsIgnoreCase("q")) {
                    System.out.println("ui");
                    messageToClient = "Thank you!!"
                            + "\n--------------------------";
                } else {
                    StringTokenizer st = new StringTokenizer(messageFromClient,
                            ",");

                    if (st.countTokens() != 3) {
                        messageToClient = "See! the input you gave has some problem!"
                                + "\n you havent followed the instructions properly"
                                + "\n Thank you!! Bye for now! come back later!"
                                + "\n--------------------------";
                    } else {
                        messageToClient = "Thanks! i am solving it!"
                                + "\n--------------------------";


                    }
                }
                }

                synchronized (messageToClient) {

                    outToClient.println(messageToClient);
                    outToClient.flush();

                    System.out.println("problem answered sent to the client!!");

                }

            }
        }
    }



    public static void main(String[] args) throws IOException {
        initiateService();
        awaitRequest();
    }

}

and i have a client accessing this particular server!

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

public class MyTcpClient {

    static Socket clientSocket;
    static BufferedReader inFromUser;
    static PrintWriter outToServer;
    static BufferedReader inFromServer;

    public static void main(String argv[]) throws Exception {
        String FromServer;
        String ToServer;

        clientSocket = new Socket("localhost", 1234);

        // for user to type messages in client mode
        inFromUser = new BufferedReader(new InputStreamReader(System.in));

        // client program uses this to write on the server
        outToServer = new PrintWriter(clientSocket.getOutputStream(), true);

        // servers talk back to the client
        inFromServer = new BufferedReader(new InputStreamReader(
                clientSocket.getInputStream()));

        FromServer = "";
        while (!FromServer.equals("--------------------------")) {
            FromServer = readFromServer();
            System.out.println(FromServer);

        }


        System.out.println("Enter choice!");
        String choice = "";

        choice = inFromUser.readLine();
        System.out.println("" + choice);

        synchronized (outToServer) {

            outToServer.println(choice);
            outToServer.flush();
        }

        FromServer = "";
        while (!FromServer.equals("--------------------------")) {
            FromServer = readFromServer();
            System.out.println(FromServer);

        }

        choice = inFromUser.readLine();
        System.out.println("" + choice);

        synchronized (outToServer) {

            outToServer.println(choice);
            outToServer.flush();
        }

        FromServer = "";
        while (!FromServer.equals("--------------------------")) {
            FromServer = readFromServer();
            System.out.println(FromServer);
        }

    }

    static String readFromServer() throws IOException {
        String response = inFromServer.readLine();
        return response;
    }

}

on a successful run the output at the server side is something like this:

TCPServer Waiting for client on port 1234
Service Ready
Client:/127.0.0.1 connected on port :1234
invite sent to the client!!
The user says: y
reply sent to the client!!
Entered
The user says: 5,3,2

and the output at client side is something like this:

Welcome!!!! You are now connected to our server!! We ensure a reliable service.

 Your IP:/127.0.0.1  
1234::::MESSAGE FROM SERVER::::
Lets play Water jug puzzel!! You give me sizes of two water Jars
And a desired level to be achieved! Ill tell you whether the problem
is solvable or not!!
Comprande~?
Enter your choice Y/N 
--------------------------
Enter choice!
y
y
Wow!! awesome! Throw a challenge at me!
Enter Large jug volume,Small jug volume,Desired level in comma separated fashion
For example (5,3,2)
--------------------------
5,3,2
5,3,2
Exception in thread "main" java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
    at java.io.InputStreamReader.read(InputStreamReader.java:167)
    at java.io.BufferedReader.fill(BufferedReader.java:136)
    at java.io.BufferedReader.readLine(BufferedReader.java:299)
    at java.io.BufferedReader.readLine(BufferedReader.java:362)
    at MyTcpClient.readFromServer(MyTcpClient.java:85)
    at MyTcpClient.main(MyTcpClient.java:78)

The exception that is being raised here is only when a string separated with commas is supplied. Dont have an idea what is going wrong , can anybody share something?

  • 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-09T09:55:31+00:00Added an answer on June 9, 2026 at 9:55 am

    You have some other problems in your code:

    static ServerSocket server = null;
    

    Should not be static.

    static Socket connectionSocket = null;
    

    Should be neither static nor even an instance member. It should be a local variable in the method that calls accept().

    static BufferedReader inFromClient = null;
    static PrintWriter outToClient = null;
    

    Ditto in this case, but in the general case they should be instance members of a per-connection class that you instantiate for every newly accepted socket. This is usually a Runnable, whose run() method does all the I/O for that client, or calls methods that do so.

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

Sidebar

Related Questions

I have a scenario as follows: I have one Windows 2003 server that has
I have a server that runs on Solaris 10, whenever I go to the
I have a server that runs different websites on different ports. All of them
I have a server that is serving files to several windows clients using the
I have a server that returns JSON in the following format: [{Name: Student1, Email:
I have a server that is running Ubuntu Linux Server Edition. I once had
I have a server that has several virtual machines running on it. I'm trying
I have a server that connects to multiple clients using TCP/IP connections, using C
I have a server that I have no control over, it's JSON based and
I have a server that communicates to a lot of devices (>1000). Each connection

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.