2014-01-24 18 views
1


該應用程序由一個服務器和一個客戶端組成。當我使用ip(127.0.0.1)在本地運行應用程序時,聊天工作得很好。當然,這裏的全部要點是在在線服務器上運行Messenger,以便我可以從另一臺計算機連接到它,並充滿自豪和歡樂。我有一個java服務器和客戶端聊天程序,但我可以在哪裏上傳它?

我的問題是(我知道這聽起來像我沒有做任何研究,我偷懶......相信我,我很困惑)我需要做什麼用的程序做,在那裏我可以把它上傳?

我想從我的本地網絡訪問我的程序,而不僅僅是在我的電腦上測試它。


的客戶端應用程序:

package helloworldC; 
import java.io.*; 
import java.net.*; 
import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

public class client extends JFrame { 
private JTextField userText; 
private JTextArea chatWindow; 
private ObjectOutputStream output; 
private ObjectInputStream input; 
private String message = ""; 
private String serverIP; 
private Socket connection; 


//constructor 
public client(String host){ 
    super("Client"); 
    serverIP = host; 
    userText= new JTextField(); 
    userText.setEditable(false); 
    userText.addActionListener(new ActionListener() { 

     public void actionPerformed(ActionEvent event) { 
      sendMessage(event.getActionCommand()); 
      userText.setText(""); 

     } 
    } 
    ); 
    add(userText, BorderLayout.NORTH); 
    chatWindow= new JTextArea(); 
    add(new JScrollPane(chatWindow), BorderLayout.CENTER); 
    setSize(300,150); 
    setVisible(true); 

} 

//connect 

public void startRunning(){ 
    try{ 
    connectToServer(); 
    setupStreams(); 
    whileChatting(); 
    }catch(EOFException eofException){ 
    showMessage("\n Client terminated") ; 
    }catch(IOException ioException){ 
     ioException.printStackTrace(); 
    } 
    finally{ 
     closeCrap(); 
    } 


} 
//conect to server 

private void connectToServer() throws IOException{ 
    showMessage("connecting...\n"); 
    connection = new Socket(InetAddress.getByName(serverIP),6789);//52 
    showMessage("Connected to: " + connection.getInetAddress().getHostName()); 
} 
// set up streams 53 
private void setupStreams() throws IOException{ 
    output = new ObjectOutputStream(connection.getOutputStream()); 
    output.flush(); 
    input = new ObjectInputStream(connection.getInputStream()); 
    showMessage("\n streams are ready"); 

} 

//while chatting 54 

private void whileChatting() throws IOException{ 
    ableToType(true); 
    do{ 
     try{ 
      message = (String) input.readObject(); 
      showMessage("\n"+message); 
     }catch(ClassNotFoundException classNotFoundException){ 
      showMessage("\n class problen"); 
     } 
    }while(!message.equals("SERVER -END")); 

} 

//close the streams and sockets 
private void closeCrap(){ 
    showMessage("\n closing sheet"); 
    ableToType(false); 
    try{ 
     output.close(); 
     input.close(); 
     connection.close(); 
    }catch(IOException ioException){ 
     ioException.printStackTrace(); 

    } 

} 
//send messages 56 

private void sendMessage(String message){ 
    try{ 
     output.writeObject("CLIENT - "+ message); 
     output.flush(); 
     showMessage("\nCLIENT -"+message); 

    }catch(IOException ioException){ 
     chatWindow.append("\n something is wrong"); 

    } 

} 
//update chatWindow57 

private void showMessage(final String m){ 
    SwingUtilities.invokeLater(
      new Runnable() { 
       public void run() { 
      chatWindow.append(m); 

     } 
    }); 


} 

//permission to type 

private void ableToType(final boolean tof){ 
    SwingUtilities.invokeLater(
      new Runnable() { 
       public void run() { 
      userText.setEditable(tof); 

     } 
    }); 


} 

} 

服務器:

package helloworld; 

import java.io.*; 
import java.net.*; 
import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 


public class Server extends JFrame { 
private JTextField userText; 
private JTextArea chatWindow; 
private ObjectOutputStream output; 
private ObjectInputStream input; 
private ServerSocket server; 
private Socket connection; 






//constructor 
public Server(){ 
super("Messenger"); 
userText=new JTextField(); 
userText.setEditable(false); 
userText.addActionListener(
    new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent event) { 
      sendMessage(event.getActionCommand()); 
      userText.setText(""); 
     } 
    } 
); 
add(userText, BorderLayout.NORTH); 
chatWindow = new JTextArea(); 
add(new JScrollPane(chatWindow)); 
setSize(300,150); 
setVisible(true); 
} 
// set up and run the server 
public void startRunning(){ 
try{ 
server = new ServerSocket(6789, 100); 
    while(true){ 
     try{ 
      //connect and have conversation 
      waitForConnection(); 
      setupStreams(); 
      whileChatting(); 

     }catch(EOFException eofException){ 
      showMessage("\n Server ended the connection! "); 
     }finally{ 
      closeCrap(); 
     } 



    } 
}catch(IOException ioException){ 
    ioException.printStackTrace(); 
} 
} 

//wait for connection, then display connection information 
    private void waitForConnection() throws IOException { 
showMessage("Waiting for connection...\n"); 
connection = server.accept(); 
showMessage("now connected to "+connection.getInetAddress().getHostName()+"\n"); 
} 

//get stream to send and receive data 
private void setupStreams() throws IOException { 
output = new ObjectOutputStream(connection.getOutputStream()); 
output.flush(); 
input = new ObjectInputStream(connection.getInputStream()); 
showMessage("\n Streams are good!\n"); 
} 
//during the chat conversation 
private void whileChatting() throws IOException{ 
    String message = " You are now connected! "; 
    sendMessage(message); 
    ableToType(true); 
    do{ 
     try{ 
      message = (String) input.readObject(); 
      showMessage("\n"+message); 
     }catch(ClassNotFoundException classNotFoundException){ 
      showMessage("\n wtf???"); 
     } 
    }while(!message.equals("CLIENT - END")); 

} 

// close streams and sockets 
private void closeCrap(){ 
    showMessage("\n Closing all...\n"); 
    ableToType(false); 
    try{ 
     output.close(); 
     input.close(); 
     connection.close(); 
    }catch(IOException ioException){ 
     ioException.printStackTrace(); 
    } 
} 
//send a message to client 
private void sendMessage(String message){ 
    try{ 
     output.writeObject("SERVER - "+message); 
     output.flush(); 
     showMessage("\nSERVER - " + message); 
    }catch(IOException ioException){ 
     chatWindow.append("\n ERROR: cant send"); 
    } 
} 
private void showMessage(final String text){ 
    SwingUtilities.invokeLater(
      new Runnable() { 
      public void run() { 
       chatWindow.append(text); 
      } 
     } 
    ); 
} 
private void ableToType(final boolean tof){ 
    SwingUtilities.invokeLater(
      new Runnable() { 
      public void run() { 
       userText.setEditable(tof); 
      } 
     } 
    ); 

} 
} 
+0

我會推薦一個簡單的雲服務器或支持Java的共享託管服務,後者的選項很少見。 – kshikama

+0

我相信Amazon EC2支持它。 –

回答

0

首先你需要一個服務器提供商,使您可以運行Java程序。我不是進入哪個提供商的好或不好,所以你可以在你家裏自己建立一個服務器(例如使用http://de.dyn.com/dns/爲你提供必要的固定DNS)。但要記住要跟蹤安全問題(開放端口和其他東西),並且計算機必須聯機以使聊天可訪問,這可能導致高能耗成本。另一方面,你也可以使用它來加載其他的東西(例如Web服務器,FTP等)。

看着它這樣你的問題不是所以Java相關的,你只需要更換IP在你的程序中,我猜;)

+0

非常感謝,但我不想使用我自己的電腦...有沒有可以託管我的應用程序的網站? – misha312

+0

我知道的唯一一個向我推薦的是strato.com,但我沒有自己嘗試。 – omgBob

2

的IP 127.0.0.1的工作原理是東陽這就是知識產權的原因的東西叫localhost。由於您在機器上託管服務器和客戶端都可以正常工作。

當你想擁有讓說,一臺計算機和客戶端上的其他本地網絡(僅訪問內部網絡的)內部的服務器程序,你必須在客戶端的IP地址指向您的服務器的IP ADRESS。

但是當你想從你的網絡外部訪問它時,事情有點不同。它與之前類似,只是你必須端口你的服務器的IP從你的路由器與端口(你的情況是6789)。這樣做的目的是當你的路由器的公網IP接收到端口6789的數據時,它知道把它發送到哪裏。如果它不知道,它會丟棄它。

當您有端口轉發您的路由器接受來自端口6789的數據。您需要找出您的公共IP地址。 (這很容易做到,只需前往:http://www.whatsmyip.org/) 然後從外部您的本地網絡,在計算機上啓動您的客戶端。然後將客戶端連接的IP替換爲您的公有IP。

NOTE:您的路由器的公網IP地址會隨時發生變化,所以如果您嘗試連接某一天,您的路由器公網IP可能已經發生變化。一種避免這種情況的方法是使用DDNS(動態域名系統),但這是另一個話題。

希望這會有所幫助!
-Kad

0

我的閱讀是你正在嘗試Java和編程,現在對如何將應用程序部署到野外感到好奇。您可以嘗試一個原始服務器,例如Amazon Web Service免費賬戶;但你會咬掉很多設置,網絡管理員,系統管理員的工作。大多數服務不提供圖形界面,所以使用JFrame並且擺動不起作用。如果您想了解如何製作可在野外運行的軟件,您最好使用免費的網絡PaaS,例如redhat openshift,並製作websockets chat app