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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:05:28+00:00 2026-05-13T15:05:28+00:00

I have a multi-threaded server that handles client requests, and makes new threads for

  • 0

I have a multi-threaded server that handles client requests, and makes new threads for each one that is connected. This is working great and I am able to send “text” messages back and forth to the server without a problem. Like a chat system.

Now I have in mind a way to be able to send files across these client connections to the server, but every single example I see in java always has a hard-coded file name on server / client side – but I want to set my own for flexibility. And I would love if it were not only able to not only receive files but also receive “text” messages on the same port simulatanously.

I have an idea whereby the “text” message port I have at the moment, tells the server a file is going to be sent from a client, then the server opens up a “file transfer” port, just for file transfers. That way the “text” port could specify the name etc of the file. And the other port could send the files quite happily, without interruption.

Does anyone know a better way to do this though? I don’t really want to use two ports, it seems a bit messy. I would prefer to make a separate thread on the server side to deal with the file transfer and also deal with “text” message at the same time, if this is even possible?

Thanks in advance, I hope I have been clear enough 🙂

  • 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-13T15:05:29+00:00Added an answer on May 13, 2026 at 3:05 pm

    Sure, it’s easy. You just have to specify the message type first. Either with a byte or with a line of text.

    The easiest way would be with a single byte like this:

    //to send
    Socket s = ...
    OutputStream os = s.getOutputStream();
    if(messageIsText()){
      os.write(0);
      //send text
    else{
      os.write(1); 
      //send file
    }
    

    then in your server you could do this:

    Socket s = serverSocket.accept();
    InputStream in = s.getInputStream();
    int firstbyte = in.read();
    if(firstbyte = 0){
       //read text
    }
    else{
       //read file
    }
    

    Now, that’s not very flexable, but there are lot of things you can do. I would actually recommend you read RFC 2616, that’s the specification for HTTP. You don’t need to read the whole thing, and just the enough to write a simple web server. It’s actually really easy (HTTP is a really simple protocol at the core, although it has a lot of advanced features now)

    If you really want to learn network programming, try writing an HTTP server. That might sound daunting, but don’t worry, it’s actually not that hard at all.

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

Sidebar

Ask A Question

Stats

  • Questions 375k
  • Answers 375k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can change the JDK for Netbeans by modifying the… May 14, 2026 at 8:15 pm
  • Editorial Team
    Editorial Team added an answer Since everyone shouts "YOODAA!!!" but noone posts a concrete example,… May 14, 2026 at 8:15 pm
  • Editorial Team
    Editorial Team added an answer You should attempt to resolve the error on the service… May 14, 2026 at 8:15 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.