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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:18:31+00:00 2026-05-13T13:18:31+00:00

i have coded a socket listener that should listen on port 80 and 81

  • 0

i have coded a socket listener that should listen on port 80 and 81 and when data arrive on these ports execute operations on these data. I want this listener to concurrently listen on both these ports and hav coded in the following way.

 import java.net.*;
    import java.io.*;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;

    public class MultipleSocketServer implements Runnable {

     private int a;
  private ServerSocket connection;
  private String TimeStamp;
  private int ID;
public static void main(String[] args){

       // System.out.print("ip");
     // String gh="12345";
      //System.out.println(gh.substring(1,3));
  int port = 80;
  int port1 = 81;
  int count = 0;
  double a=234.52121;

   //System.out.println(bf3.toString());
    try{

      ServerSocket socket1 = new ServerSocket(port);
      ServerSocket socket2=new ServerSocket(port1);
      System.out.println("MultipleSocketServer Initialized");
      Runnable runnable = new MultipleSocketServer(socket1, ++count);
        Runnable run = new MultipleSocketServer(socket2, ++count);
        Thread thread = new Thread(runnable);
        Thread thread1 = new Thread(run);
      while (true) {
        //Socket connection = socket1.accept();

        thread.start();
        thread1.start();
      }
    }

    catch (Exception e) {}
  }
MultipleSocketServer(ServerSocket s, int i) {
  this.connection = s;
  this.ID = i;
}
public void run() {
    while(true){


    try {
        Socket incoming=connection.accept();


            BufferedInputStream is = new BufferedInputStream(incoming.getInputStream());
            int character;
     while((character = is.read())!=-1) {
    .
    .
    do the input data handling here
    .
    .

    }
    }
    catch(Exception e){}
    }
    }
}

But for some reason this does not seem to show the threaded/conncurrent behaviour.
I am testing this code using Hyperterminal, and every time i disconnect from hyperterminal, the program execution stops and “Socket is closed” exception is raised.

Any pointers would be of great help

Cheers

  • 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-13T13:18:31+00:00Added an answer on May 13, 2026 at 1:18 pm

    You’re starting threads in an endless loop.

    while (true) {
      //Socket connection = socket1.accept();
      thread.start();
      thread1.start();
    }
    

    I think though, that this is handled (ignored) in

    } catch (Exception e) {}
    

    However, I suspect that the problem you describe is in in the handling code you didn’t include. One pretty obvious idea: you don’t call connection.close() instead of incoming.close(), do you?

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can enable almost full support for CakePHP in NetBeans… May 13, 2026 at 7:50 pm
  • Editorial Team
    Editorial Team added an answer I've never used the functionality, so I'm not 100% certain… May 13, 2026 at 7:50 pm
  • Editorial Team
    Editorial Team added an answer From webmaster tools: Google generates these links automatically, but you… May 13, 2026 at 7:50 pm

Related Questions

I have a windows service written around some code similar to this Asynchronous Server
How can I detect that a client has disconnected from my server? I have
I'm working on some Python code modeled on Apache's MPM prefork server. I am
In VB.net I'm using the TcpClient to retrieve a string of data. I'm constantly
I am trying to setup a simple demo of activemq and mina. I edited

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.