【问题标题】:Java socket - simple program doesn't workJava 套接字 - 简单的程序不起作用
【发布时间】:2013-05-19 17:31:47
【问题描述】:

我花了很多时间找出问题所在,但没有成功。服务器正在正确启动,但是当我启动客户端时,我得到“意外错误”异常。我也更改了端口,但没有任何影响。我应该怎么做才能使它工作?

/* Server.java */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class Server
{
private static final int PORT = 50000;
static boolean flaga = true;

private static ServerSocket serverSocket;
private static Socket clientSocket;

public static void main(String[] args) throws IOException
{
    serverSocket = null;
    try
    {
        serverSocket = new ServerSocket(PORT);
    }
    catch(IOException e)
    {
        System.err.println("Could not listen on port: "+PORT);
        System.exit(1);
    }

    System.out.print("Wating for connection...");

    Thread t = new Thread(new Runnable()
    {
        public void run()
        {
            try
            {
                while(flaga)
                {
                    System.out.print(".");
                    Thread.sleep(1000);
                }
            }
            catch(InterruptedException ie)
            {
                //
            }

            System.out.println("\nClient connected on port "+PORT);
        }
    });
    t.start();

    clientSocket = null;
    try
    {
        clientSocket = serverSocket.accept();
        flaga = false;
    }
    catch(IOException e)
    {
        System.err.println("Accept failed.");
        t.interrupt();
        System.exit(1);
    }

    final PrintWriter out = new PrintWriter(clientSocket.getOutputStream(),true);
    final BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

    t = new Thread(new Runnable()
    {
        public void run()
        {
            try
            {
                Thread.sleep(5000);

                while(true)
                {
                    out.println("Ping");
                    System.out.println(System.currentTimeMillis()+" Ping sent");

                    String input = in.readLine();

                    if(input.equals("Pong"))
                    {
                        System.out.println(System.currentTimeMillis()+" Pong received");
                    }
                    else
                    {
                        System.out.println(System.currentTimeMillis()+" Wrong answer");

                        out.close();
                        in.close();
                        clientSocket.close();
                        serverSocket.close();
                        break;
                    }


                    Thread.sleep(5000);
                }
            }
            catch(Exception e)
            {
                System.err.println(System.currentTimeMillis()+" Unexpected Error");
            }
        }
    });
    t.start();
}
}

和客户端类

/* Client.java */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client
{
private static final int PORT = 50000;
private static final String HOST = "localhost";

public static void main(String[] args) throws IOException
{
    Socket socket = null;

    try
    {
        socket = new Socket(HOST, PORT);
    }
    catch(Exception e)
    {
        System.err.println("Could not connect to "+HOST+":"+PORT);
        System.exit(1);
    }

    final PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
    final BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

    Thread t = new Thread(new Runnable()
    {
        public void run()
        {
            long start = System.currentTimeMillis();

            while (true)
            {
                try
                {
                    String input = in.readLine();

                    if (input != null)
                    {
                        System.out.println(System.currentTimeMillis() + " Server: " + input);
                    }

                    if (input.equals("Ping"))
                    {
                        if(System.currentTimeMillis()-start>30000)
                        {
                            out.println("Pon g");
                            System.out.println(System.currentTimeMillis() + " Client: Pon g");
                            break;
                        }

                        out.println("Pong");
                        System.out.println(System.currentTimeMillis() + " Client: Pong");
                    }
                }
                catch (IOException ioe)
                {
                    //
                }
            }
        }
    });
    t.start();

    out.close();
    in.close();
    socket.close();
}
}

这是运行时的输出

Wating for connection............
Client connected on port 50000
1368986914928 Ping sent
java.lang.NullPointerException
    at Server$2.run(Server.java:84)
    at java.lang.Thread.run(Thread.java:722)

【问题讨论】:

标签: java sockets exception


【解决方案1】:

你在那些空的或打印出你无用信息的 catch 块上犯了一个大错误。

如果您打印或记录堆栈跟踪,您将获得更多信息。这是必须的。

您需要一些介绍性说明 - 看看这个,看看它与您的有何不同。

http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html

【讨论】:

  • 感谢这个链接,有一个很好的例子说明我需要做什么:)
  • 不客气。 Oracle/Sun 在网上有很多教程信息。将其作为 Java 如何提问的第一站。
【解决方案2】:

它显示您的out 对象是null。在 Server.java 的第 84 行中使用 input != null && input.equals("Pong") 代替 input.equals("Pong")。我猜你会收到Pong received,但在后面的阶段,当你什么都不听时,你可能会收到这个NPE

【讨论】:

  • 感谢您提供这些信息。我会将其设置为已接受的答案,但再次感谢 duffymo :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-09
  • 1970-01-01
相关资源
最近更新 更多