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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:17:28+00:00 2026-05-20T11:17:28+00:00

I have been programming in Java for a while, but have never dealt with

  • 0

I have been programming in Java for a while, but have never dealt with threads before. In one of my classes, we learned about the easy-to-use yet vastly powerful Socket and ServerSocket classes. We made programs that could talk to each other through on the same machine (using “localhost”). There were two classes, a Server.java and a Client.java, but they could only talk to each other in a turn-based fashion.

I’d like to put the socket.accept() method on a new thread so the Server and Client programs can both send message while socket.accept() is waiting for new messages.

I have done well with threads so far, but I am unable to think of where to put the ServerSocket/Socket objects. If I put them in the thread that waits for incoming messages, I can’t use that object for outgoing messages. If I put the ServerSocket/Socket objects in the Server.java/Client.java file directly, I can’t (or I just don’t know how) pass in the object to the thread, without creating a copy of it.

Any suggestions on how I should structure this program?

Thanks,
Derek

  • 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-20T11:17:28+00:00Added an answer on May 20, 2026 at 11:17 am

    I would say that the best way of running this application is as two separate processes/virtual machines, rather than two threads. Although you’re running them on the same (virtual) machine at the moment, the whole point of instant messaging – and sockets – is to allow communication between different processes, which may be on different machines. You already have the two classes – simply make sure that both have main() methods so that they can run on their own.

    Also, if you want full-duplex communication, you’d need to have two threads per process – one to wait for input from the user and send it to the other side, the other to wait for messages from the other side and present them to the user. Although you could set up a half-duplex system where a token is passed back and forwards on a single socket, you will likely find it much easier if you set up one incoming socket and one outgoing socket per end.

    If you want to do full duplex communication from both ends, I don’t think this is really a client/server relationship. If both sides are able to talk to each other equally, they’re really more peers. By all means have a ‘server’ class that listens for incoming messages, and a ‘client’ class that sends outgoing ones, but have one of each on every machine. These map quite closely to the sending and receiving threads.

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

Sidebar

Related Questions

I'e been programming Java forever, but have been away from it for a while.
I have been programming in Java since 2004, mostly enterprise and web applications. But
I've been using Objective-C for a while now, but have never really understood what
I have been programming in C# and Java for a little over a year
I have been programming in Python, PHP, Java and C for a couple or
I have been programming in Perl, off and on, for years now, although only
I have been programming using PHP, Javascript and have been a designer for a
At school we have been programming in MIPS assembly language for some time. I'm
I have been trying to learn multi-threaded programming in C# and I am confused
I have been researching and playing with functional programming lately, solely to broaden my

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.