2014-11-05 113 views
0

我試圖通過調用類的默認構造函數來啓動一個套接字服務器,但是服務器沒有啓動無法啓動socket服務器的Java

下面是我的服務器套接字類:

import java.io.*; 
import java.net.*; 


public class TransactionServer 
{ 
    public void TransactionServer() throws IOException { 

     System.out.println("Inside Create Server"); 
     Socket echoSocket; 
     InputStream sin = null; 
     OutputStream sout = null; 

     ObjectInputStream sinO=null; 
     ObjectOutputStream soutO=null; 

     try { 
      ServerSocket s = new ServerSocket(2000); 
      System.out.println("Server Ruinning"); 
      echoSocket = s.accept(); 
      System.out.println("Connection from: " + echoSocket.getInetAddress()); 

      sinO = new ObjectInputStream(echoSocket.getInputStream()); 
      soutO = new ObjectOutputStream(echoSocket.getOutputStream()); 

      String temp = (String) sinO.readObject(); 
      System.out.println("" + temp); 
     } catch (Exception e) { 
      System.err.println(e); 
      return; 
     } 
    } 
} 

下面是我的調用此類的對象:

public class TabbedPane extends JFrame { 

    public TabbedPane() { 

     TransactionServer newServer=new TransactionServer(); 
    } 
} 

但是服務器沒有啓動。

回答

1

您的newServer實例不會調用TransactionServer()! 這樣:

TransactionServer newServer=new TransactionServer(); 
        newServer.TransactionServer(); 
+1

好的。他認爲構造函數實際上是一個無效的方法。 +1 – 2014-11-05 05:11:46

1

您已經定義TransactionServer構造函數時使用void。刪除void,並且構造函數將被觸發,以運行套接字服務器。

更換行:

public void TransactionServer() throws IOException { 

有了:

public TransactionServer() throws IOException { 

除此之外,我假設你有一個主要方法某處來調用 「invokation對象」。 :-)