Socket.accept()抛出空指针异常(Socket.accept() throws null

2019-09-26 14:39发布

我写一个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();
    }

}

}

Answer 1:

下面是当前的问题,你可以调用new Thread((Runnable) new echoer())这开始你的线程。

然而,这要求echoer空的默认构造函数目前还没有实际的代码吧!

所以,即使你构建插座一次,你这样做之后,你只需创建echoer的新实例与所有新的套接字并调用run()

这意味着,在运行所有插槽均为空,因为他们从来没有通过一组,因此NullPointerException当您尝试使用它们。



文章来源: Socket.accept() throws null pointer exception