开发者

How to make TCP and UDP server both get started? Where only any one of those getting started

开发者 https://www.devze.com 2023-04-05 03:28 出处:网络
When i boot this, TCPServer starts gets running but after that UDPServer does not run? And 开发者_如何学运维if i swap those always the first one runs. How can i boot both?

When i boot this, TCPServer starts gets running but after that UDPServer does not run? And 开发者_如何学运维if i swap those always the first one runs. How can i boot both?

All the rest boots fine. How can i solve this?

/**
 * Boot baby boot
 * @param args 
 */
public static void main(String[] args)
{       
    /* Heavy load - 1 */
  new Thread(new Runnable() 
  {
      public void run()
      {
        SwingUtilities.invokeLater(new Runnable() 
        {      
            public void run() 
            {              
                TCPServer tcpserver = null;
                try {
                    tcpserver = new TCPServer(8888);           
                }
                catch (IOException e){
                    e.printStackTrace(System.err);
                }
                tcpserver.waitForConnections();              
            }
        });                
      }
  }).start();

  new Thread(new Runnable() 
  {
      public void run()
      {
        SwingUtilities.invokeLater(new Runnable() 
        {      
            public void run() 
            {              
                UDPServer udpserver = null;
                try {
                    udpserver = new UDPServer(8889);           
                }
                catch (IOException e){
                    e.printStackTrace(System.err);
                }
                udpserver.waitForConnections();
            }
        });                 
      }
  }).start();           

    /* Heavy load - 2 */                     
    try {                                                                      
        Game3Dstart();        
    } catch (Exception ex) {

    }

    /* Finally */
    j = new main();
    j.setVisible(true);                
}


It's because waitForConnections is blocking. You need to launch them async, in separate threads.

new Thread(new Runnable(){
    public void run(){
        TCPServer tcpserver = null;
        try {
            tcpserver = new TCPServer(8888);             
        }
        catch (IOException e){
            e.printStackTrace(System.err);
        }
        tcpserver.waitForConnections();  
    }
}).start();

new Thread(new Runnable(){
    public void run(){
        UDPServer udpserver = null;
        try {
            udpserver = new UDPServer(8889);
        }
        catch (IOException e){
            e.printStackTrace(System.err);
        }
        udpserver.waitForConnections();
    }
}).start();


Put both UDP and TCP servers on different threads:

/* Heavy load - 1 */
SwingUtilities.invokeLater(new Runnable() 
{      
    public void run() 
    {              
        TCPServer tcpserver = null;
        try {
            tcpserver = new TCPServer(8888);             
        }
        catch (IOException e){
            e.printStackTrace(System.err);
        }
        tcpserver.waitForConnections();           

        catch (IOException e){
            e.printStackTrace(System.err);
        }

    }
} 
/* Heavy load - 1 */
SwingUtilities.invokeLater(new Runnable() 
{      
    public void run() 
    {              

        UDPServer udpserver = null;
        try {
            udpserver = new UDPServer(8889);

        }
        catch (IOException e){
            e.printStackTrace(System.err);
        }
        udpserver.waitForConnections();
    }
}); 

);

0

精彩评论

暂无评论...
验证码 换一张
取 消