Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java accept()引发空指针异常_Java_Sockets_Nullpointerexception_P2p - Fatal编程技术网

Java accept()引发空指针异常

Java accept()引发空指针异常,java,sockets,nullpointerexception,p2p,Java,Sockets,Nullpointerexception,P2p,我正在编写一个p2p文件共享程序,它可以接受连接,也可以作为服务器本身 正在进行中,但生产线:60 套接字sock1=tcpSocket.accept() 抛出空指针异常,我不知道出了什么问题。什么都试过了 import java.net.*; import java.io.*; public class echoer implements Runnable { int i,backlog; public Socket tcpClient= null; public

我正在编写一个p2p文件共享程序,它可以接受连接,也可以作为服务器本身

正在进行中,但生产线:60

套接字sock1=tcpSocket.accept()

抛出空指针异常,我不知道出了什么问题。什么都试过了

import java.net.*;
import java.io.*;


public class echoer implements Runnable {
    int i,backlog;
    public Socket tcpClient= null;
    public ServerSocket tcpSocket= null;
    public echoer(int tcpPort, int udpPort, int backlog) {
        try {
            this.tcpSocket = new ServerSocket(tcpPort,backlog);
            System.out.println("Server connected to "+ InetAddress.getLocalHost() + "on TCP port " + tcpPort + "and UDP port " + udpPort );
            this.backlog= backlog;
            listening();
        }
        catch (SocketTimeoutException s) {
            System.out.println("timeout");
        }
        catch (IOException ioe) {
            System.out.println("could not listen on port 10009");
            System.exit(-1);
        }
}
public echoer () {

}
void listening(){
        try {
                //i++;
                tcpSocket.getInetAddress();
                System.out.println();

                //Thread t1= new Thread((Runnable) new AcceptInput());
                //t1.start();
                //tcpSocket.accept();
                //System.out.println("Connection accepted");
                //messaging();
                Thread t2 = new Thread((Runnable) new echoer());
                t2.start();
            }
            catch (Exception e) {
                System.out.println("Cannot accept connection");
            }
        }

public void Client(String addr, int port) throws IOException 
{
    System.out.println("address= "+ addr+ "port= "+ port);
    tcpClient = new Socket(addr,port);
}
/*void messaging () {
    System.out.println("Starting Thread");
    Thread t = new Thread((Runnable) new echoer());
    t.start();
}*/
public void run() {
    while (true) {
        try {
            //System.out.println("Listening on "+ InetAddress.getLocalHost().getHostAddress() + "on TCP port " + tcpSocket.getLocalSocketAddress());
            Socket sock1= tcpSocket.accept();
            //Client(InetAddress.getLocalHost().getHostAddress(),tcpSocket.getLocalPort());
            System.out.println("Connection accepted");
            ObjectOutputStream out= new ObjectOutputStream(sock1.getOutputStream());
            //Now start the messaging thread nad pass this sock1 to tcpClient
            /*String line;
            System.out.println("Write a message");
            DataInputStream din= new DataInputStream(tcpClient.getInputStream());
            line= din.readUTF();
            if (line == null) {
                din.close();
                tcpClient.close();
                }
            System.out.println("Recvd message:" + line);*/
            if (sock1 != null) {
            tcpSocket.close();
            }
        }
        catch (IOException o) {
            System.out.println("Read Failed");
            }
        }
    }

    /*catch (IOException i) {
        System.out.println("Last statement");
    }
}*/
public static void main(String[] args) {
    new echoer(Integer.parseInt(args[0]),Integer.parseInt(args[1]),5);

}
}

 class AcceptInput implements Runnable {
    String token;
    public void run () {
        BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
        try {
            token= br.readLine();
        if (token== "connect" ) {
            System.out.print("Enter IP address: ");
            BufferedReader ip= new BufferedReader(new InputStreamReader(System.in));
            //accept ip and port
            // pass ip and port to tcpclient socket to initiate a connection
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

这是当前的问题,您调用
新线程((Runnable)new echore())
并启动线程

但是,这将调用echorer的空默认构造函数,该构造函数当前没有实际代码

因此,即使您构造了一次套接字,在构造之后,您只需使用所有新套接字创建一个新的Echor实例,并在该实例上调用
run()


这意味着run中的所有套接字都是空的,因为它们从未设置过,因此在您尝试使用它们时通过
NullPointerException

为什么要粘贴注释掉的代码?@Jagger,以便我们了解他在做什么。当tcpSocket为空时,很可能您正在调用accept。