java - socket 未连接

标签 java sockets networking network-programming nullpointerexception

我的问题如下:我有以下客户端类:

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;


public class Client {

    public final int portNumber = 6040;
    public Socket socket;
    private PrintWriter pw;
    /**
     * @param args
     * @throws IOException 
     */
    public void connect() throws IOException{


        // du kan vælge at bruge inetadressen til at connecte i socketet.
        InetAddress adr = InetAddress.getByName("localhost");
        socket = new Socket("localhost", portNumber);
        pw = new PrintWriter(socket.getOutputStream());
        pw.println("Connected waiting for input");
        pw.flush();

    }
    /**
     * This method sends the message (that the client(chat person) writes to the user)
     * @param x
     * @throws NullPointerException
     * @throws IOException 
     */
    public void SendChat(String x) throws NullPointerException{
            pw.print(x);
            pw.flush(); 


    }
    public int sendCommando(int id) throws IOException{

         PrintWriter pw = new PrintWriter(socket.getOutputStream());
        pw.print(id);

        /*
         * this part of the program sends a command to the server if the command is 1 then 1 is = Connect.
         * the program then ask the server is the server is full or is it ok to connect? 
         * if the response is not 10 then the program will allow a connection to happen the return type will be the Id of which 
         * the chat person becomes!
         */

        // should the method return 0 the Application will do NOTHING!
        switch (id) {
        case 1:
    int k = reciveCommando();
            if (k== 10) {
                return 10;
            }else if (k<= 3) {
                return k;
            }else {

            return 10;
            }
            /*
             * Closes the connection with the server!
             */
        case 3:

            socket.close();
            return 0;

        default:
            return 0;
        }

    }
    /*
     * this method recives a command from the server! the comands can be found in the ChatCommands.txt
     * returns the command as an integer!
     */
    public int reciveCommando() throws IOException{
        Scanner commandoFromServer = new Scanner(socket.getInputStream());
        Integer i = Integer.parseInt(commandoFromServer.nextLine());
        return i;
    }
    /**
     * Gets a String response from the server. This method i used to create other users and give them the correct username.
     * 
     * @param i
     * @return
     * @throws IOException
     */
    public String getStringResponse(int i) throws IOException {
        pw.print(i);
        pw.flush();
        Scanner commandFromServer = new Scanner(socket.getInputStream());
        String x = commandFromServer.nextLine();
        return x;

    }


}

除此之外,我有一个 GUI 用作我的“主”类。

我有一个名为 SocketTest v3.0.0 的程序(如果你们有人知道的话),它的基本功能是为我创建一个可以连接的服务器。

现在我的程序运行并连接到服务器!但是当我尝试调用方法 sendCommand 或 sendChat 或我的客户端程序中的任何其他方法(除了连接)时,它会给我一个空指针执行。

我已经将问题范围缩小到当我声明套接字时套接字为空,我该如何解决这个问题?因为我无法初始化公共(public)类下的套接字!

我希望您明白我的意思,如果不明白,请回复,我会详细介绍!

提前谢谢您!

最佳答案

“sendCommandO”创建自己的 printwriter 而不是使用 全局一。以后有两个打印撰稿人喂食肯定是自找麻烦 同一条流。 “连接”立即发送“已连接...”似乎也很奇怪

一般来说,这个程序的这种结构是行不通的,因为你的写作将被阻塞,并且在读者读完某些内容之前无法解除阻塞。对于少量数据来说,它似乎工作得很好。读取和写入必须在单独的线程中完成。

这些都不是您眼前的问题,但我建议完全重写。

关于java - socket 未连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12717362/

相关文章:

c++ - 通过代理服务器提升 asio 套接字连接

java - In & Out 无法解析为类型 (Java Socket)

c++ - 如何在不创建服务器的情况下创建具有联系人和实时通信的多人游戏?

java - try-with-ressources 在创建并关闭之前无法创建新的 BufferedWriter

java - 如何将 JRE 从 1.8.0.241 更改为最新的 13

node.js - Web 应用程序中实时摄像头服务的最佳解决方案是什么?

客户端unix套接字编程中的connect调用

networking - 同一 VNET 中 VM 之间的连接

java - JDBC sql查询(批处理)

java - 在 Cucumber 测试场景中比较整数值的最佳实践