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 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

Related Questions

I have a listener that returns a TcpClient that must read data until the
I have a listener that will pass arbitrary data, HTTP requests, to a network
Java Socket Server I have a Java process that is creating a listener on
I have to create an sample code that connect to a socket server, and
I have coded a view that relies on a scalar function value for one
We have an application in production that is supposed to be a multithreaded socket
I have the following piece of code to listen to a port which is
I have a problem with a c++ socket. I'm using CAsyncSocket from MFC that
I have an asynchronous socket listener class like this: using System; using System.Collections.Generic; using
I am doing some basic Socket messaging. I have a routine that works well

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.