Java 中的 KnockKnockServer
KnockKnockServer in Java
我是 java 网络方面的新手,我正在尝试在 java 中制作一个名为不带 GUI 的即时消息传递的服务器应用程序。所以每当我运行它说的程序时
" 在 class 方法中找不到 Main 方法,请将 main 方法定义为:
public static void main(String[] args)
或 JavaFX 应用程序 class 必须扩展 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 class 主要{
public static void main (String[] args) {
Server s=new Server();
s.runningserver();
}
}
//第二个文件//
public class 服务器 {
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();
}
}
}
main方法必须是class:
的一个方法
import java.net.Socket;
public class Server {
public static void main (String[] args) {
Server s=new Server();
s.runningserver();
}
所以它必须在 class 声明之后。
编辑:这是解决方案,我检查了它,对我来说它有效(运行 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();
}
}
}
我可以 运行 无一例外。
问题是你的主要方法
public static void main (String[] args) {}
不在class你所在的运行吧。
//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 中制作一个名为不带 GUI 的即时消息传递的服务器应用程序。所以每当我运行它说的程序时 " 在 class 方法中找不到 Main 方法,请将 main 方法定义为: public static void main(String[] args) 或 JavaFX 应用程序 class 必须扩展 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 class 主要{
public static void main (String[] args) {
Server s=new Server();
s.runningserver();
}
}
//第二个文件//
public class 服务器 {
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();
}
}
}
main方法必须是class:
的一个方法import java.net.Socket;
public class Server {
public static void main (String[] args) {
Server s=new Server();
s.runningserver();
}
所以它必须在 class 声明之后。
编辑:这是解决方案,我检查了它,对我来说它有效(运行 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();
}
}
}
我可以 运行 无一例外。
问题是你的主要方法
public static void main (String[] args) {}
不在class你所在的运行吧。
//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 方法(执行的起点)。