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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:55:46+00:00 2026-05-31T22:55:46+00:00

I have 2 classes where I have one to send commands to a socket,

  • 0

I have 2 classes where I have one to send commands to a socket, another which receives (and sometimes answers commands) .

Do I have to synchronize this? Or is this not necessary?

Both classes run in their own threads with the socket object passed down to each of them as argument upon thread.start();

Is this the proper way to do it or could I do something more efficient?

Would this have chances of causing errors?
The sending part:

    public void run(){
        send_chatline("roomf");

        int vvv = 0;
        while (this.socket.isConnected()){
            try{
                Thread.sleep(10000);
                vvv++;

                Thread.sleep(10000);
                send_chatline("alive");

                Thread.sleep(10000);

                if (vvv == 1) {
                    this.socket.flush();
                    this.socket.send("T,-1," + this.playerid * 3);
                    this.socket.flush();
                }
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

But remember! The recieveFromSock class also writes sometimes when specific commands appear.

The only function of sendTosock is to keep the connection alive (being able to remain online).

  • 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-05-31T22:55:47+00:00Added an answer on May 31, 2026 at 10:55 pm

    The only guarantee you have is that a byte sent, is sent once, and that a byte read is read once. Others may say that the synchronization is taken care of by the runtime-library, or the underlying operating system. Any such statement is runtime-implementation dependent and/or operating system dependent (and should in my opinion be disregarded from).

    So, if you have one thread reading from the socket, and one thread writing to the socket, you should be fine without synchronization.

    If more than one thread may write to the socket, you should synchronize their actions to make sure the output sent from the two threads doesn’t get interleaved.

    Same applies if two threads read from the socket. You should synchronize the reads to make sure that data read by one thread doesn’t have “gaps” due to reads of another thread.

    Similar question, same conclusion:

    • in what way is java.net.Socket threadsafe?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 pages one is index.php other is classes.php should not this work
I have two classes one of which inherits from the other. Im trying to
I have some classes that, for one reason or another, cannot be or need
I have created two viewController classes such that one is superclass of another.i have
i have two data classes which hold only data members(no functions). One is CallTask
I have one activity, off this I have two classes. When my app starts
In one of my projects, I have some classes that represent entities that cannot
I am using Hibernate to do mapping. One of my classes have a set
First I want to describe my situation briefly. I have two classes, one MainClass
I've worked out most of the code and have several game classes. The one

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.