0

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

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 或任何其他方法时,它会给我一个空指针执行。

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

我希望你明白我的意思,如果没有,请回复,我会详细说明!

先感谢您!

4

3 回答 3

1

“sendCommandO”创建自己的打印器而不是使用全局打印器似乎很奇怪。让两台印刷机输入同一个数据流肯定是在自找麻烦。“connect”立即发送“connected...”似乎也很奇怪

一般来说,这个程序的这种结构是行不通的,因为你的写作会阻塞并且在读者读完之前无法解除阻塞。它似乎适用于少量数据。读取和写入必须在单独的线程中完成。

这些都不是你的直接问题,但我建议完全重写。

于 2012-10-03T22:40:34.137 回答
1

在套接字的生命周期内,您必须使用相同的流、扫描仪等。不要在每次想要进行 I/O 时都创建新的。否则,您将丢失缓冲区中的数据。

于 2012-10-03T23:42:26.640 回答
0

首先感谢你们所有的帮助和回复,这就是我的代码工作的原因:

  1. 清理代码以确保每个只创建 1 个,并且这些是在客户端连接到服务器时创建的

  2. 为了确保我可以在所有方法中同时使用 PrintWriter 和 Scanner,我必须将字段设为静态

感谢您的所有帮助,我的客户端现在连接到服务器并能够来回发送消息!:)

完整的代码在这里:

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


public class Client {

    public final static int portNumber = 6040;
    public static Socket socket;
    private static PrintWriter pw;
    private static Scanner input;
    /**
     * @param args
     * @throws IOException 
     */
    public static 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);
        input=new Scanner(socket.getInputStream());
        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{
        pw.print(id);
        pw.flush();
        /*
         * 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) {
                System.out.println("returned k" + k);
                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{
        Integer i = input.nextInt();
        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();
        String x = input.nextLine();
        return x;

    }


}
于 2012-10-04T00:14:42.417 回答