該應用程序由一個服務器和一個客戶端組成。當我使用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);
}
}
);
}
}
我會推薦一個簡單的雲服務器或支持Java的共享託管服務,後者的選項很少見。 – kshikama
我相信Amazon EC2支持它。 –