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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:52:48+00:00 2026-06-03T09:52:48+00:00

I’m following an example from java net programing, and decided to write a client-server

  • 0

I’m following an example from java net programing, and decided to write a client-server in java-scala respectively, here’s the scala code:

import scala.actors.Actor
import scala.actors.Actor._

import java.io._
import java.net._

class EchoActor(private val sock: Socket,
    private val id: Int) extends Actor{

    def act(){
        try{
            val in = new BufferedReader(
                new InputStreamReader(sock.getInputStream()))

            val out = new PrintWriter(
                sock.getOutputStream(), true)

            out.println("Hello! press BYE to exit")
            var done = false
            while(!done){
                var str = in.readLine()
                if (str == null) done = true
                else{
                    println(str) // debug
                    out.println("Echo (" + id + "): " + str)
                    if (str.trim().equals("BYE")) done = true
                }
            }
            sock.close()
        }
        catch{
            case e => e printStackTrace
        }
    }
}


object Main{
    def main(args: Array[String]){
        try{
            var i = 1
            val s = new ServerSocket(8189)

            while(true){
                var inc = s.accept()
                println("Spawning " + i)
                var a = new EchoActor(inc, i)
                a.start()
                i += 1
            }
        }
        catch{
            case e => e printStackTrace
        }
    }
}

And here’s java’s:

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


public class Client{
    public static void main(String[] args){

        try{
            Socket s = new Socket("127.0.0.1",8189);

            BufferedReader in = new BufferedReader(
                new InputStreamReader(System.in));

            PrintWriter out = new PrintWriter(
                s.getOutputStream(), true);

            BufferedReader socketIn = new BufferedReader(
                new InputStreamReader(s.getInputStream()));

            System.out.println("Enter: to exit");
            String resp;
            String st
            while(true){
                st = in.readLine();
                if(st.equals("exit") || st.equals("quit")) break;
                else{
                    out.println(st);
                    resp = socketIn.readLine();
                    System.out.println(resp);
                }
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}

The output from the server:

Spawning 1
s

s

l
l
scala

java

java’s output:

Enter: to exit
s
Hello! press BYE to exit

Echo (1): s
s
Echo (1):

Echo (1): s
l
Echo (1):
l
Echo (1): l
scala
Echo (1): l

Echo (1): scala
java
Echo (1):

Echo (1): java

As you can see im receiving Echo (1) response empty where i expect to print the Echo (1) plus the string i just send, you got to re-type “Enter” to get the string previouly send, so that’s the problem, i need just one response from the server like this just once: Echo (1): some string, i’m out of ideas, i tracked it with printing to the console but can’t figure out what’s going on.

Edit:

Expected client’s output:

...
s
Echo (1): s
java
Echo (1): java

On the expected output you don’t have to re-type enter to get the response from the server.

Server’s output is already working as expected.

  • 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-03T09:52:49+00:00Added an answer on June 3, 2026 at 9:52 am

    You are getting confused because you are sending “Hello! press BYE to exit” when the client first connects, but the client doesn’t actually read this line specifically. So you are reading it in the client when you are expecting to read the echoed message you sent, so your client is always reading one echo behind where you think it is up to.

    Solution: either remove the “Hello! press BYE to exit” from the server, or read it when you first connect the client, before you enter the loop.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

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.