java - Java 中的 KnockKnockServer

标签 java server

我是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/

相关文章:

java - Gradle 测试在本地失败,但在 CI 服务器上成功

java - Xpages:OpenNTF ODA 日期出现问题

c - 写入服务器不会返回任何内容

node.js - NodeJs 服务器随机崩溃 : events. js:72 EIO

java - 如何更快地修改托管在 tomcat 服务器上的 java 类?

java - 如何使用 websocket 检查消息是否真的在 Netty 中传递?

java - 将一个值转换为两个类

Java线程启动时间

javascript - 即使检查了权限、限制和文件大小,Php 也无法打开流

rest - 使用 REST 和 PUB/SUB 的微服务间通信