gpt4 book ai didi

java - Java 中的 KnockKnockServer

转载 作者:行者123 更新时间:2023-12-02 05:05:57 25 4
gpt4 key购买 nike

我是java网络方面的新手,我正在尝试用java制作一个名为即时消息传递的服务器应用程序,无需GUI。所以每当我运行该程序时它都会说 "在方法类中找不到主方法,请将主方法定义为: 公共(public)静态无效主(字符串[]参数)或者 JavaFX 应用程序类必须扩展 javafx.application.Application"-ECLIPSE

请帮我看看我的代码有什么问题。

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

//第一个文件//
公共(public)类主{

    public static void main (String[] args) {
Server s=new Server();
s.runningserver();

}
}

//第二个文件//
公共(public)类服务器{

    private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;

//wait For Connection,then display connection information
private void waitforConnection() {
System.out.println("wait for Someone to Connect.....\n");
try {
connection=server.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("error In Acceptance of Server!!\n...");
}
System.out.println(("Connection Established"+connection.getInetAddress().getHostName()));

}


//Setting Up Streams to Get Input and Output

private void setupStreams(){
try {
output=new ObjectOutputStream(connection.getOutputStream());
output.flush();
input=new ObjectInputStream(connection.getInputStream());
System.out.println("Your Streams are Perfectly Working...");


} catch (IOException e) {
// TODO Auto-generated catch block

System.err.println("Error Found! in Streaming Connectionos");
e.printStackTrace();
}
}

// While Chatting Method....!!//
private void whileChatting() throws IOException{
String Message="You are now Connected!!\n";
sendMessage(Message);

//abletoType(true);
do{
try{
Message=(String) input.readObject();
System.out.println("\n"+Message);
}
catch(ClassNotFoundException e ){
System.out.println("wtf---Fuck\n YOu\n Bloody\n HAcker!!!\n");
}



}
while(!Message.equals("CLIENT--END"));

}
// Closing All Streams and Socket after you are done//
private void closeCrap(){
System.out.println("\nClosing Connection...........Bye Bye\n");
//abletoType(false);
try {
output.close();
input.close();
connection.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Couldn't Close Connections!");
}

}
//Sending Messages to Client
private void sendMessage(String message){
try {

output.writeObject("SERVER--- "+message);

output.flush();
System.out.println("\nServer- "+message);

} catch (IOException e) {
// TODO Auto-generated catch block




e.printStackTrace();
System.out.println("Dude I cant Send yeaah..");
}

}






// Setting up the Server

public void runningserver(){
try {
server=new ServerSocket(4444,100);
while(true){try{
//connect and Have connection
waitforConnection();
setupStreams();
whileChatting();
}
finally{
closeCrap();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



}




}

最佳答案

编辑:这是解决方案,我检查了它,对我来说它有效(运行 MainClass.java 文件!)://MainClass.java 文件:

public class MainClass {
public static void main (String[] args) {
Server s=new Server();
s.runningserver();
}
}

//Server.java文件:

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {

private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;

// wait For Connection,then display connection information
private void waitforConnection() {
System.out.println("wait for Someone to Connect.....\n");
try {
connection = server.accept();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("error In Acceptance of Server!!\n...");
}
System.out.println(("Connection Established" + connection
.getInetAddress().getHostName()));

}

// Setting Up Streams to Get Input and Output

private void setupStreams() {
try {
output = new ObjectOutputStream(connection.getOutputStream());
output.flush();
input = new ObjectInputStream(connection.getInputStream());
System.out.println("Your Streams are Perfectly Working...");

} catch (IOException e) {
// TODO Auto-generated catch block

System.err.println("Error Found! in Streaming Connectionos");
e.printStackTrace();
}
}

// While Chatting Method....!!//
private void whileChatting() throws IOException {
String Message = "You are now Connected!!\n";
sendMessage(Message);

// abletoType(true);
do {
try {
Message = (String) input.readObject();
System.out.println("\n" + Message);
} catch (ClassNotFoundException e) {
System.out.println("wtf---Fuck\n YOu\n Bloody\n HAcker!!!\n");
}

} while (!Message.equals("CLIENT--END"));

}

// Closing All Streams and Socket after you are done//
private void closeCrap() {
System.out.println("\nClosing Connection...........Bye Bye\n");
// abletoType(false);
try {
output.close();
input.close();
connection.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Couldn't Close Connections!");
}

}

// Sending Messages to Client
private void sendMessage(String message) {
try {

output.writeObject("SERVER--- " + message);

output.flush();
System.out.println("\nServer- " + message);

} catch (IOException e) {
// TODO Auto-generated catch block

e.printStackTrace();
System.out.println("Dude I cant Send yeaah..");
}

}

// Setting up the Server

public void runningserver() {
try {
server = new ServerSocket(4444, 100);
while (true) {
try {
// connect and Have connection
waitforConnection();
setupStreams();
whileChatting();
} finally {
closeCrap();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

我可以毫无异常(exception)地运行它。

问题是你的主要方法

public static void main (String[] args) {}

不在您运行它的类中。

//separate file A.java
public class A
{}

//separate file B.java
public class B
{
public static void main (String[] args) {}

}

如果您现在运行 B,它就会起作用,因为 B.java 有一个 main 方法。但是:如果你运行 A.java 它会说:没有找到主要方法。要运行的文件(A、B等必须定义main方法)如果你运行一个java文件,它会寻找一个main方法(执行的起点)。

关于java - Java 中的 KnockKnockServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27781464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com