How to open two sockets in one single server

May I open two sockets with different listening port numbers in a single server? For each socket, the way to deal with the inputstream and outputstream is different. May the thread be like this:
public class twoSocketServer implements Runnable {
public twoSocketServer() {
try{
SSLServerSocketFactory fact = SSLServerSocketFactory.getInstance(rand, selfPrivateKey, selfCertificate, trustEngine, null);
serverSocket1 = (SSLServerSocket) fact.createServerSocket(ListeningPort1);
serverSocket1.setNeedClientAuth(false);
serverSocket2 = (SSLServerSocket) fact.createServerSocket(ListeningPort2);
serverSocket2.setNeedClientAuth(false);
} catch {...}
public void run()
while(true)
try
SSLSocket socket1 = (SSLSocket) serverSocket1.accept();
new handler1(socket1).start();
SSLSocket socket2 = (SSLSocket) serverSocket2.accept();
new handler2(socket2).start();
} catch {...}
class handler1 extends Thread {...}
class handler2 extends Thread {...}
Is there something wrong with this idea? If yes, how can I correct it? Thanks.

try
SSLSocket socket1 = (SSLSocket)
serverSocket1.accept();
new handler1(socket1).start();
SSLSocket socket2 = (SSLSocket)
serverSocket2.accept();
new handler2(socket2).start();
Is there something wrong with this idea? If yes, how
can I correct it? Thanks.You can do it.
In the above code block you are going to need two threads. One for each SocketServer. The accept() method blocks until it recieves a connection. So in your above code it would first wait only for a connection on port 1. Then it would wait only for a connection on port 2. And then start over. That probably isn't what you want.

Similar Messages

  • Two sockets in a single server

    May I open two sockets with different listening port numbers in a single server? For each socket, the way to deal with the inputstream and outputstream is different. May the thread be like this:
    public class twoSocketServer implements Runnable {
    public twoSocketServer() {
    try{
    SSLServerSocketFactory fact = SSLServerSocketFactory.getInstance(rand, selfPrivateKey, selfCertificate, trustEngine, null);
    serverSocket1 = (SSLServerSocket) fact.createServerSocket(ListeningPort1);
    serverSocket1.setNeedClientAuth(false);
    serverSocket2 = (SSLServerSocket) fact.createServerSocket(ListeningPort2);
    serverSocket2.setNeedClientAuth(false);
    } catch {...}
    public void run()
    while(true)
    try
    SSLSocket socket1 = (SSLSocket) serverSocket1.accept();
    new handler1(socket1).start();
    SSLSocket socket2 = (SSLSocket) serverSocket2.accept();
    new handler2(socket2).start();
    } catch {...}
    class handler1 extends Thread {...}
    class handler2 extends Thread {...}
    Is there something wrong with this idea? If yes, how can I correct it? Thanks.

    This is better :
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.*;
    import java.net.*;
    public class MultSrv extends JFrame
         JTextArea   panel   = new JTextArea();
         JScrollPane sbr     = new JScrollPane(panel);
         Vector      sockets = new Vector();
         Vector      servers = new Vector();
    public MultSrv() 
         super();
         SocServer ss1 = new SocServer(this,4444);
         if (ss1.servS != null) ss1.start();
              else               System.out.println("err");
         SocServer ss2 = new SocServer(this,4445);
         if (ss2.servS != null) ss2.start();
              else               System.out.println("err");
         SocServer ss3 = new SocServer(this,4446);
         if (ss3.servS != null) ss3.start();
              else               System.out.println("err");
         try
              InetAddress inet = InetAddress.getLocalHost();
              setTitle("MultServer v1 - "+inet+"  Port=4444/5/6");
         catch(IOException e){}
         setBounds(10,10,400,450);
         setContentPane(sbr);
         panel.setEditable(false);
         addWindowListener(new WindowAdapter()
        {     public void windowClosing(WindowEvent ev)
              {   dispose();
                   System.exit(0);}});
         setVisible(true);
         javax.swing.Timer ta = new javax.swing.Timer(2000, new ActionListener()
              public void actionPerformed(ActionEvent e)
                   panel.setText("");
                   for (int i = 0; i < sockets.size(); i++)
                        SocServer ss = (SocServer)sockets.get(i);
                        if (ss.servS == null) panel.append(""+ss.port +ss.err);
                             else              panel.append(""+ss.port +" Active");
                        panel.append("\n");     
                   for (int i = 0; i < servers.size(); i++)
                        Server server = (Server)servers.get(i);
                        panel.append(""+server.clnt.getInetAddress()+" Is serviced");
                   repaint();
         ta.start();     
    public void go(Socket clnt)
         Server server = new Server(clnt);
         servers.add(server);
         server.start();
    //  The ServerSocket
    public class SocServer extends Thread
         ServerSocket servS;
         int          port;
         String       err = "";
         Socket       clnt;
         MultSrv      mserv;
    public SocServer(MultSrv mserv, int port) 
         this.mserv = mserv;
         this.port  = port;
         sockets.add(this);
         try
              servS = new ServerSocket(port);
         catch (IOException e)
              err = e.toString();;
    public void run()
         try
              while (true)
                   clnt = servS.accept();
                   mserv.go(clnt);
         catch (IOException e)
    //  The Server.. one for each client
    public class Server extends Thread
         Socket                clnt = null;
         OutputStream       out  = null;
         InputStream        ins  = null;
    public Server(Socket soc) 
         clnt = soc;
    public void run()
         try
              ins  = clnt.getInputStream();
              out  = clnt.getOutputStream();
              while (true)
         catch (IOException e){}
         try
              ins.close();
              out.close();
              clnt.close();
         catch (IOException e){}
    public static void main (String[] args) 
         new MultSrv();
    }     

  • How to concatenate two colums into one single column

    I need some ideas to concatenate two different columns into one single column using a set of distinct values.
    For Example,
    Customer Product Number
    xyz A 1
    xyz B 2
    xyz B 1
    AAA C 7
    AAA A 1
    The result should look like this,
    Customer Value
    xyz A1 B2 B1
    AAA C7 A1
    How would I group this into once value ?
    Thanks in advance ...

    Tom's discussion of writing your own aggregate routines
    http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:2196162600402
    starts off with a link to the 8i alternatives
    "see
    http://asktom.oracle.com/pls/ask/f?p=4950:8:::::F4950_P8_DISPLAYID:229614022562
    for 8i methods (not aggregates)"
    Unforutnately, it's a lot more work in 8i.
    Justin

  • How to open two program in one click??

    hi there Java lovers , can anyone help me on this ?
    I want to open two program ( 2 windows ) after I click on a button... how can I do this ??
    please help me & Thanks for the help ....

    hi there Java lovers , can anyone help me on this ?
    I want to open two program ( 2 windows ) after I click
    on a button... how can I do this ??
    please help me & Thanks for the help ....I suppose that you can open 2 windows,two Dialog windows.You cann't open 2 Frame windows at the same time.
    To do this ,you can use Dialog.show() to make the windows be visible.

  • How to make two tables from one single taable

    Hi,
    i have one table with 100 records and i want to create two tables each with 50 records and with no duplicates. if i will do on bases on empno then i have to check the min and max emp no and then divide them in two tables, is there any automate way of performing this task.
    Regards,
    Abida

    Hi,
    A rather quick and dirty solution, but definitely works.
    I used the HR sample schema (employees table) for this.
    hsaprd@706447>create table tab1 as select * from employees where rownum <51;
    Table created.
    hsaprd@706447>select count(*) from tab1;
    COUNT(*)
    50
    hsaprd@706447>create table tab2 as select * from employees where employee_id nnot in (select employee_id from tab1);
    Table created.
    hsaprd@706447>select count(*) from tab2;
    COUNT(*)
    56
    hsaprd@706447>
    Hope this helps!
    -Anand

  • How do I fix this error message, The address wasn't understood Firefox doesn't know how to open this address, because one of the following protocols (connectag

    We have a new computer and I cannot get my garmin running watch to sync with mozilla. The error message that I keep getting is"The address wasn't understood
    Firefox doesn't know how to open this address, because one of the following protocols (connectagent) isn't associated with any program or is not allowed in this context.
    You might need to install other software to open this address."
    Help please

    This is for a special URL that starts with
    connectagent://
    instead of with
    http://
    is that right?
    Two thoughts:
    (1) Could you try changing a setting for your Garmin plugin. Here's what I mean:
    Open the Add-ons page using either:
    * Ctrl+Shift+a
    * orange Firefox button (or Tools menu) > Add-ons
    In the left column, click Plugins.
    Find your Garmin plugin change "Ask to Activate" to "Always Activate".
    Your system information seems to list two different versions of a similarly named plugin. I'm not sure whether that is causing a problem.
    (2) Does Garmin's documentation specify any extra steps to set up Firefox, other than installing its add-ons?

  • How to Append two  word documents into single  using   java

    How to Append two word documents into single using java
    we tried this but it's not append the one word document to other
    source code:public class AppendTwoWordFiles {
         public static void main(String []arg)throws IOException
              FileInputStream fi=null;
              FileOutputStream fo=null;
              try {
                   System.out.println("Enter the source file name u want to append");
                   BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                   File f1=new File(br.readLine().toString());
                   System.out.println("Enter the Destination file name ");
                   File f2=new File(br.readLine().toString());
                   fi = new FileInputStream(f1);
                   fo = new FileOutputStream(f2,true);
                   byte b[]=new byte[2];
                   while((fi.read(b))!=-1);
              fo.write(b);
    System.out.println("Successfully append the file");
              } catch (FileNotFoundException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
              } catch (IOException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
              finally{
              fi.close();
              fo.close();
    plz reply me quickly ,,,what can i follow

    Use this code ..
    and give the path of the both file like this.....
    source file ---- C:/workspace/Practice/src/com/moksha/ws/test/practice.text
    destination file ---- C:/workspace/City/src/com/moksha/ws/test/practice1.text
    import java.io.*;
    public class AppendTwoWordFiles {
         public static void main(String[] arg) throws IOException {
              FileInputStream fi = null;
              FileOutputStream fo = null;
              try {
                   System.out.println("Enter the source file name u want to append");
                   BufferedReader br = new BufferedReader(new InputStreamReader(
                             System.in));
                   File f1 = new File(br.readLine().toString());
                   System.out.println("Enter the Destination file name ");
                   File f2 = new File(br.readLine().toString());
                   fi = new FileInputStream(f1);
                   fo = new FileOutputStream(f2, true);
                   byte b[] = new byte[2];
                   int len = 0;
                   while ((len = fi.read(b)) > 0) {
                        fo.write(b, 0, len);
                   System.out.println("Successfully append the file");
              } catch (FileNotFoundException e) {
                   e.printStackTrace();
              } catch (IOException e) {
                   e.printStackTrace();
              } finally {
                   fi.close();
                   fo.close();
    }

  • How to Open Two Excel Files in Multiple Monitors in Windows 7

    How to open two excel files in two excel windows using multiple monitors in Windows 7.
    Currently it opens multiple files on top of each other on the same one monitor.
    I found this article in a blog it says
    "The snap feature that you are looking for will not work unless you open two instances of Excel. This is because Excel Unlike Word is not a True SDI Application. Microsoft is aware of the Issue however there is no resolution to the problem but the workaround"

    If you are working almost the entire day in front of your computer at your office with lots of Excel Sheets and Word, then probably you might be working with a
    dual monitor or may be even more than that. Studies have shown that having an additional monitor increases the productivity by 20 to 30 percent (Source: NY Times)
    But some applications like MS Office Excel, even though you open multiple files, they are all from the same instance of the application. So if you want to compare two
    Excel
    files, then you may not be able to have it in two
    separate monitors as the files are loaded using the same instance of Excel. If you move one
    Excel
    file to the other window, the other Excel files are also moved to the other window.
    So how to have two separate Excel files or other application side by side in dual monitors?
    Option A:
    In Excel 2003, go to Tools -> Options ->
    General tab.
    Make sure the option, ‘Ignore other applications’ is checked. Now all the Excel files will be opened as separate instance and you can move the Excel files individually across the monitors.
    In Excel 2007, Click the Office button ->
    Excel Options -> Advanced.
    Under General, check ‘Ignore other applications that use Dynamic Data Exchange’.
    or
    As this method forces each Excel file as a separate instance, the memory consumption will be more. If you don’t want too many memory consumption then you can open only two instances (see
    Option B) and manage wisely to view in both the monitors.
    Note: If you are having issues like Excel opens without displaying a workbook, then you may have to
    uncheck this option. (See Microsoft Help for more details on this). You can use option B in this case. I have this option checked and I have not faced any issue yet.
    Option B:
    They key here is, the application has to be loaded as separate instances. Lets say you have opened an Excel file in
    Monitor 1 and you want to open the next excel file in Monitor 2. You can usually open another instance of Excel by browsing through the
    Start Menu -> Programs -> Microsoft Office ->
    Excel. Make sure this newly opened Excel file is the last Excel file you had viewed and then double click on the Excel file that you wanted to open. This will force the Excel to
    open
    in the second instance of Excel. Now you can move these
    two excel files separately across windows or monitors.
    This may be little cumbersome way to open new instances of Excel every time. The easy solution would be to keep these links in the
    quick links near the Start button. So, every time you want to open a new instance of the application, you can just use those quick links.
    hope work thanks
    http://www.lytebyte.com/2008/05/13/how-to-open-two-excel-files-side-by-side-in-separate-monitors/

  • How to put two ipods on one computer

    how to put two ipods on one computer

    Set up the second exactly the same way as you set up the first. If iTunes is already installed, it doesn't need to be installed again.
    (64746)

  • How to call two RFC in a single JAVA method.

    Dear all,
    I just want to know that how to call two RFC in a single java method which is defined in CRM implementation file. I'm using NWDS as the customization IDE & working on ISA 7.0.

    Hi Sunil,
    In the Backend Implementation class, in any method you can call multiple RFCs.
    It will be the same way as you do for the single RFC call.
    Following syntax is for your reference.
    Get the JCO connection
    JCoConnection  connection = getDefaultJCoConnection();
    JCO.Function func = connection.getJCoFunction("ZXXXXXXX");
    set the import parameters
    Execute it.
    connection.execute(func);
    get the data from export / table parameters
    Now call the second RFC
    func = connection.getJCoFunction("ZYYYYYYYYYY");
    set the import parameters
    Execute it.
    get the data from export / table parameters
    close the connection
    Hope this will help you.
    -Chandra.
    Edited by: Chandra Sekhar Seeli on Jan 13, 2011 2:04 PM

  • How to sync two IPhone with one PC , but with different applications

    How to sync two IPhone with one PC , but with different applications ?
    We have two IPhones , but one PC , we want to sync separately , is that possible ?

    Absolutely, connect each phone, select what content you want on each phone, then hit the sync button. iTunes will keep everything straight, by device, upon subsequent syncs, including separate backups.

  • How to set two radius servers one is window NPS another is cisco radius server

    how to set two radius servers one is window NPS another is cisco radius server
    when i try the following command, once window priority is first , i type cisco radius user name, it authenticated fail
    i can not use both at the same time
    radius-server host 192.168.1.3  is window NPS
    radius-server host 192.168.1.1 is cisco radius
    http://blog.skufel.net/2012/06/how-to-integrating-cisco-devices-access-with-microsoft-npsradius/
    conf t
    no aaa authentication login default line
    no aaa authentication login local group radius
    no aaa authorization exec default group radius if-authenticated
    no aaa authorization network default group radius
    no aaa accounting connection default start-stop group radius
    aaa new-model
    aaa group server radius IAS
     server 192.168.1.1 auth-port 1812 acct-port 1813
     server 192.168.1.3 auth-port 1812 acct-port 1813
    aaa authentication login userAuthentication local group IAS
    aaa authorization exec userAuthorization local group IAS if-authenticated
    aaa authorization network userAuthorization local group IAS
    aaa accounting exec default start-stop group IAS
    aaa accounting system default start-stop group IAS
    aaa session-id common
    radius-server host 192.168.1.1 auth-port 1812 acct-port 1813
    radius-server host 192.168.1.2 auth-port 1812 acct-port 1813
    radius-server host 192.168.1.3 auth-port 1645 acct-port 1646
    radius-server host 192.168.1.3 auth-port 1812 acct-port 1813
    privilege exec level 1 show config
    ip radius source-interface Gi0/1
    line vty 0 4
     authorization exec userAuthorization
     login authentication userAuthentication
     transport input telnet
    line vty 5 15
     authorization exec userAuthorization
     login authentication userAuthentication
     transport input telnet
    end
    conf t
    aaa group server radius IAS
     server 192.168.1.3 auth-port 1812 acct-port 1813
     server 192.168.1.1 auth-port 1812 acct-port 1813
    end

    The first AAA server listed in your config will always be used unless/until it becomes unavailable. At that point the NAD would move down to the next AAA server defined on the list and use that one until it becomes unavailable and then move to third one, and so on. 
    If you want to use two AAA servers at the same time then you will need to put a load balancer in front of them. Then the virtual IP (vip) will be listed in the NADs vs the individual AAA servers' IPs. 
    I hope this helps!
    Thank you for rating helpful posts!

  • How can update two rows in one query?

    How can update two rows in one query?
    Edited by: OracleM on May 4, 2009 12:16 AM

    What do you mean with "two rows"? May be two columns??
    If the where clause of UPDATE query matches two rows, then it will update two rows. If you want to update two columns, then add column names to your query
    UPDATE your_table SET col1=value1, col2=value2 WHERE your_where_clause- - - - - - - - - - - - - - - - - - - - -
    Kamran Agayev A. (10g OCP)
    http://kamranagayev.wordpress.com
    [Step by Step install Oracle on Linux and Automate the installation using Shell Script |http://kamranagayev.wordpress.com/2009/05/01/step-by-step-installing-oracle-database-10g-release-2-on-linux-centos-and-automate-the-installation-using-linux-shell-script/]

  • Firefox doesn't know how to open this address, because one of the following protocols (itms-service) isn't associated with any program or is not allowed in this

    Firefox doesn't know how to open this address, because one of the following protocols (itms-services) isn't associated with any program or is not allowed in this context.
    You might need to install other software to open this address."
    tried to fix in about:config but itms-services not foundn; also tried to fix using network.protocol-handler.external.foo but that wasn't listed either
    trying to download an app related to a rewards gift card
    firefox and os versions are up to date on MacBook Air

    Hello!
    Would you please provide the link you were trying to visit?
    Also, you may want to post this question to the [https://support.mozilla.org/en-US/questions/new/desktop Firefox for Desktop Support Forum] . The Mozilla Support volunteers there may be able to better assist you.
    I hope we can help you solve your problem.

  • How to open a file created at the server through form/report at client end

    How to open a file created at the server through form/report at client end
    Dear Sir/Madame,
    I am creating a exception report at the server-end using utl file utility. I want to display this report at the client end. A user doesn't have any access to server. Will u please write me the solution and oblige me.
    Thanks
    Rajesh Jain

    One way of doing this is to write a PL/SQL procedure that uses UTL_FILE to read the file and DBMS_OUTPUT to display the contents to the users.
    Cheers, APC

Maybe you are looking for