我有一个java服务器和客户端聊天程序,但我可以上传到哪里?
我有一个java服务器和客户端聊天程序,但我可以上传到哪里?,java,sockets,client-server,chat,java-server,Java,Sockets,Client Server,Chat,Java Server,应用程序由服务器和客户端组成。当我使用ip(127.0.0.1)在本地运行应用程序时,聊天功能正常。当然,这其中的全部要点是在在线服务器上运行messenger,这样我就可以从另一台计算机连接到它,并充满自豪和喜悦 我的问题是(我知道这听起来好像我没有做任何研究,我很懒……相信我,我很困惑)我需要用这个程序做什么?我可以上传到哪里 我想从本地网络外部访问我的程序,而不仅仅是在我的计算机上测试它 客户端应用程序: package helloworldC; import java.io.*; im
应用程序由服务器和客户端组成。当我使用ip(127.0.0.1)在本地运行应用程序时,聊天功能正常。当然,这其中的全部要点是在在线服务器上运行messenger,这样我就可以从另一台计算机连接到它,并充满自豪和喜悦 我的问题是(我知道这听起来好像我没有做任何研究,我很懒……相信我,我很困惑)我需要用这个程序做什么?我可以上传到哪里 我想从本地网络外部访问我的程序,而不仅仅是在我的计算机上测试它
客户端应用程序:
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程序的服务器提供程序。我不知道哪个提供商是好是坏,所以你可以自己在家里设置服务器(例如,使用提供必要的固定dns)。但请记住,要跟踪安全问题(开放端口和其他内容),并且计算机必须在线才能访问聊天,这可能会导致高能耗。另一方面,你也可以使用它来装载其他东西(例如Web服务器、FTP等)
从这个角度来看,您的问题并不依赖于java,我想您只需要在程序中替换ip;) IP 127.0.0.1之所以有效,是因为它是一种称为localhost的IP。因为您在工作正常的机器上同时托管服务器和客户机 当您想在本地网络内(仅从网络内部访问)让一台计算机上的服务器程序和另一台计算机上的客户端程序连接时,您必须将客户端的IP地址指向服务器的IP地址 但是,当您想从网络外部访问它时,情况就有点不同了。它与以前类似,只是您必须使用端口(在您的情况下为6789)将服务器的IP从路由器进行端口转发。这样做的目的是,当您的路由器公共IP通过端口6789接收数据时,它知道将数据发送到哪里。如果它不知道,就丢弃它 当您将端口转发给路由器以接受来自端口6789的数据时。你需要找出你的公共IP地址。(这是非常容易做到的,只需前往:) 然后从本地网络外部,在计算机上启动客户端。然后将客户端应连接的IP替换为您的公共IP 注意:您的路由器的公共IP地址会不时更改,因此,如果有一天您尝试连接,您的路由器的公共IP可能已更改。避免这种情况的一种方法是使用DDNS(动态域名系统),但这是另一个主题 希望这有帮助
-Kad我的理解是,您正在试验Java和编程,现在对如何将应用程序部署到野外感到好奇。您可以尝试一个原始服务器,比如Amazon Web服务免费帐户;但是,你将咬了很多设置,网络管理,系统管理工作。大多数服务都不提供图形界面,所以使用JFrame和swing是行不通的。如果你想学习如何制作在野外运行的软件,最好使用免费的web PaaS,如redhat openshift和making 非常感谢,但我不想使用我自己的电脑。。。有没有一个网站可以托管我的应用程序?我知道唯一一个向我推荐的网站是strato.com,但我自己没有尝试过。我会推荐一个简单的云服务器或支持Java的共享托管服务,后一种选择比较少见。我相信Amazon EC2支持它。