java.lang.NullPointerException - Java 服务器和 C 客户端

标签 java c network-programming

我正在尝试实现Java服务器和C客户端。当我尝试多次发送字符串时,出现 java.lang.NullPointerException。

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

public class EchoServer
{        
    ServerSocket m_ServerSocket;


    public EchoServer() 
    {
        try
        {
            // Create the server socket.
            m_ServerSocket = new ServerSocket(12111);
        }
        catch(IOException ioe)
        {
            System.out.println("Could not create server socket at 12111. Quitting.");
            System.exit(-1);
        }

        System.out.println("Listening for clients on 12111...");

        // Successfully created Server Socket. Now wait for connections.
        int id = 0;
        while(true)
        {                        
            try
            {
                // Accept incoming connections.
                Socket clientSocket = m_ServerSocket.accept();

                // accept() will block until a client connects to the server.
                // If execution reaches this point, then it means that a client
                // socket has been accepted.

                // For each client, we will start a service thread to
                // service the client requests. This is to demonstrate a
                // multithreaded server, although not required for such a
                // trivial application. Starting a thread also lets our
                // EchoServer accept multiple connections simultaneously.

                // Start a service thread

                ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);
                cliThread.start();
            }
            catch(IOException ioe)
            {
                System.out.println("Exception encountered on accept. Ignoring. Stack Trace :");
                ioe.printStackTrace();
            }
        }
    }

    public static void main (String[] args)
    {
        new EchoServer();    
    }


    class ClientServiceThread extends Thread
    {
        Socket m_clientSocket;        
        int m_clientID = -1;
        boolean m_bRunThread = true;

        ClientServiceThread(Socket s, int clientID)
        {
            m_clientSocket = s;
            m_clientID = clientID;
        }

        public void run()
        {            
            // Obtain the input stream and the output stream for the socket
            // A good practice is to encapsulate them with a BufferedReader
            // and a PrintWriter as shown below.
            BufferedReader in = null; 
            PrintWriter out = null;

            // Print out details of this connection
            System.out.println("Accepted Client : ID - " + m_clientID + " : Address - " + 
                             m_clientSocket.getInetAddress().getHostName());

            try
            {                                
                in = new BufferedReader(new InputStreamReader(m_clientSocket.getInputStream()));
                out = new PrintWriter(new OutputStreamWriter(m_clientSocket.getOutputStream()));

                // At this point, we can read for input and reply with appropriate output.

                // Run in a loop until m_bRunThread is set to false
                while(m_bRunThread)
                {                    
                    // read incoming stream
                    String clientCommand = in.readLine();

                    System.out.println("Client Says :" + clientCommand);


                    if(clientCommand.equalsIgnoreCase("quit"))
                    {
                        // Special command. Quit this thread
                        m_bRunThread = false;   
                        System.out.print("Stopping client thread for client : " + m_clientID);
                    }
                    else
                    {
                        // Echo it back to the client.
                        out.println(clientCommand);
                        out.flush();
                    }
                }
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                // Clean up
                try
                {                    
                    in.close();
                    out.close();
                    m_clientSocket.close();
                    System.out.println("...Stopped");
                }
                catch(IOException ioe)
                {
                    ioe.printStackTrace();
                }
            }
        }
    }
} 


#include <stdio.h>      
#include <sys/types.h>
#include <sys/socket.h>   
#include <netdb.h>

int main(int argc, char *argv[]) {

    int simpleSocket = 0;
    int simplePort = 0;
    int returnStatus = 0;
    char buffer[256] = "some string\n";
    char buff[256] = "quit\n";
    struct sockaddr_in simpleServer;

    if (3 != argc) {

        fprintf(stderr, "Usage: %s <server> <port>\n", argv[0]);
        exit(1);

    }

    /* create a streaming socket      */
    simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (simpleSocket == -1) {

        fprintf(stderr, "Could not create a socket!\n");
        exit(1);

    }
    else {
        fprintf(stderr, "Socket created!\n");
    }

    /* retrieve the port number for connecting */
    simplePort = atoi(argv[2]);

    /* setup the address structure */
    /* use the IP address sent as an argument for the server address  */
    bzero(&simpleServer, sizeof(simpleServer)); 
    simpleServer.sin_family = AF_INET;
    inet_addr(argv[2], &simpleServer.sin_addr.s_addr);
    simpleServer.sin_port = htons(simplePort);

    /*  connect to the address and port with our socket  */
    returnStatus = connect(simpleSocket, (struct sockaddr *)&simpleServer, sizeof(simpleServer));

    if (returnStatus == 0) {
        fprintf(stderr, "Connect successful!\n");
    }
    else {
        fprintf(stderr, "Could not connect to address!\n");
    close(simpleSocket);
    exit(1);
    }

    send(simpleSocket, buffer, sizeof(buffer), 0);
    read(simpleSocket, buffer, sizeof(buffer), 0);
    send(simpleSocket, buffer, sizeof(buffer), 0);
    read(simpleSocket, buffer, sizeof(buffer), 0);
    send(simpleSocket, buffer, sizeof(buffer), 0);
    read(simpleSocket, buffer, sizeof(buffer), 0);
    send(simpleSocket, buffer, sizeof(buffer), 0);
    read(simpleSocket, buffer, sizeof(buffer), 0);
    send(simpleSocket, buffer, sizeof(buffer), 0);
    read(simpleSocket, buffer, sizeof(buffer), 0);
    send(simpleSocket, buffer, sizeof(buffer), 0);
    read(simpleSocket, buffer, sizeof(buffer), 0);
    send(simpleSocket, buff, sizeof(buff), 0);
//    read(simpleSocket, buffer, sizeof(buffer), 0);

    /* get the message from the server   */
//    returnStatus = read(simpleSocket, buffer, sizeof(buffer), 0);

        fprintf(stderr, "Return Status = %d \n", returnStatus);

    close(simpleSocket);
    return 0;

}

我在 Java 服务器中收到此错误消息。

Accepted Client : ID - 2 : Address - localhost
Client Says :some string
Client Says :some string
Client Says :some string
Client Says :some string
Client Says :quit
Client Says :
Client Says :null
java.lang.NullPointerException
    at EchoServer$ClientServiceThread.run(EchoServer.java:102)
...Stopped

如何解决这个问题?

最佳答案

问题是 clientCommandnull,因此您会得到 NullPointerException

从您的日志输出中,我看到您打印了 quit ,因此如果您执行 m_bRunThread = false; 并停止阅读,您应该已经转到分支。

但之后您会继续打印/阅读,如日志所示。

我建议尝试两件事:
将 boolean 值声明为 volatile

volatile boolean m_bRunThread = true;   

并考虑空白:clientCommand.trim().equalsIgnoreCase("quit")

实际上,防御性的做法如下:

 while(m_bRunThread) {                    
       // read incoming stream
       String clientCommand = in.readLine();    
       if(clientCommand == null || "".equals(clientCommand.trim()){  
         bRunThread = false;//here you could also do break and just use a while(true)  
       }  
       //go on with the code

关于java.lang.NullPointerException - Java 服务器和 C 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9547714/

相关文章:

c - 发送到特定远程 ip 的第一条 UDP 消息丢失

java - 如何使用按钮按下操作在面板中显示图像

c - C 内核模块中数组初始化中的省略号

C:如何通过套接字读取和解包消息?

sockets - 关闭套接字和关闭网络流之间的区别(System.Net.Sockets)

c# - 在渲染集群中的节点之间同步 View 状态

java - JAXB 解码返回 null

java - 如何使用分布式API连接数据库?

java - ActiveMQ 传输类型选择取决于系统类型

c++ - 可以将自旋锁与 O(1) 非内存连续代码一起使用吗?