找不到符号类服务器套接字

时间:2018-10-01 03:47:24

标签: java sockets netbeans web-applications serversocket

因此,我一直在尝试学习如何为学校和生活编程Web应用程序(我不确定这些术语是否正确,对不起英语,对此感到抱歉)

使用套接字并在服务器应用程序和客户端应用程序周围移动软件包

无论如何,我“写”(用视频课来写)这段代码

package server;

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

public class Server {

private ServerSocket serverSocket;
//declaração de um servidor de conexões
//declaration of a socket server

private void criarServerSocket(int porta) throws IOException{
    //inicia o servidor de sockets da aplicação servidor
    //initializes the socket server from the server application
    serverSocket = new ServerSocket(porta);
}

private Socket esperaConexao() throws IOException {
    //espera pela conexao de uma aplicação cliente e cria um socket para esse cliente
    //waits for the conetion of a client application and creates a socket for him
    Socket socket = serverSocket.accept();
    return socket;

}

private void trataConexao(Socket socket) throws IOException {   //o throw aqui é do fechaSocket(s)
                                                                //this throw is from fechaSocket(s)
    //aqui seria tudo que se faz com a conexao de um cliente, por isso o socket é fechado no final
    //here comes everything we do when we get a conetion for a client, after that the socket is closed
    try {
        //cria os streams de objeto para passar objetos java pelo socket
        //creates the object stream so java objects can go through
        ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
        ObjectInputStream input = new ObjectInputStream(socket.getInputStream());

        //protocolo de troca de mensagens da aplicação
        //protocol for message exchange
        String msg = input.readUTF();
        System.out.println("Mensagem recebida.");
        output.writeUTF("Hello, World!");

        //fecha os streams de objeto
        //closes the object streams
        output.close();
        input.close();
    } catch (IOException e) {
        //TODO: handle exception
    } finally {
        //Independentemente de dar certo ou de dar erro, ele fecha o socket no final
        //Regardless of what works and don't work the socket has to be closed at the end
        fechaSocket(socket);
    }

}

private void fechaSocket(Socket s) throws IOException {
    //fecha o socket
    //closes the socket
    s.close();
}




public static void main(String[] args) {
    int port = 5555;
    try {
        Server server = new Server();
        System.out.println("Aguardando conexão...");
        server.criarServerSocket(port);
        Socket socket = server.esperaConexao();
        System.out.println("Cliente conectado.");
        server.trataConexao(socket);
        System.out.println("Cliente desconectado.");
    } catch (IOException e) {

    }
}
}

当我在PowerShell上编译它时,它会创建Server.class,但是当我尝试运行它时,会得到一个

  

错误:找不到或加载主类。\ Server.class

所以我下载了NetBeans,并使用了相同的代码,但是它在Socket和ServerSocket包导入时始终闪烁警告

  

找不到符号

当我尝试运行它时

  

运行:

     

java.lang.ExceptionInInitializerError

     

由以下原因引起:java.lang.RuntimeException:无法编译的源代码-找不到符号

     

符号:类ServerSocket

     

位置:类java.net

     

在server.Server。(Server.java:6)

     

线程“ main”中的异常C:\ Users \ Raphael Ting \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml:53:Java返回:1

     

建立失败(总时间:3秒)

我真的在这里迷路了,我应该把这项工作直到周三上学……我检查了错别字,却没有发现……


edit1:所以我尝试使用命令java server.Server运行它,但是得到了相同的错误消息

  

PS C:\ Users \ Raphael Ting> cd。\ Documents \ Infracom \ Server \ PS   C:\ Users \ Raphael Ting \ Documents \ Infracom \ Server> ls

     

目录:C:\ Users \ Raphael Ting \ Documents \ Infracom \ Server

     

模式LastWriteTime长度名称

     

---- ------------- ------ ----

     

-a ---- 01/10/2018 00:25 1801 Server.class

     

-a ---- 30/09/2018 23:48 2385 Server.java

     

PS C:\ Users \ Raphael Ting \ Documents \ Infracom \ Server> java服务器   错误:找不到或加载主类服务器。服务器

0 个答案:

没有答案