Java初学者在这里,将我的客户端类分成多个方法?

时间:2014-06-04 01:21:31

标签: java sockets client

好吧基本上我正在使用套接字创建一个在线多人tic tac toe游戏,但我在我的客户端类中遇到了一些逻辑问题。我需要让客户端运行,然后能够在一个单独的方法中编写和读取套接字,流等。我确信这是一个非常简单的解决方案,我可能已经过度思考了,我已经用Google搜索了,我似乎无法将我的大脑包裹起来。 基本上我的最终结构应该是

public Client
{
Instantiate sockets/streams, etc here.
}
public void writeAndRead
{
Write and read here.
}

我目前的问题是我无法用其他方法调用我的作家。 我的代码如下

import java.net.*;
import java.io.*;
public class Client
{
private String host;
private int port;
public Client(String host2, int port2) throws IOException
{
host = host2;
port = port2;
    //Instantiate a stringbuffer to store the reply from the server.
    StringBuffer instr = new StringBuffer();

    //Stores the host string into a usable IP.
    InetAddress address = InetAddress.getByName(host);

    //Instantiate a socket with the given IP and port name.
    Socket socket = new Socket(address, port);

    //Instantiate a output stream to the socket.
    BufferedOutputStream stream = new BufferedOutputStream(socket.getOutputStream());

    //Instantiate a writer for the output stream using standard encoding.
    OutputStreamWriter writer = new OutputStreamWriter(stream, "US-ASCII");

    //Instantiate an input stream to read in reply from server.
    BufferedInputStream inputStream = new BufferedInputStream(socket.getInputStream());

    //Instantiate a reader to read and convert the stream, once again using standard encoding.
    InputStreamReader reader = new InputStreamReader(inputStream, "US-ASCII"); 

        //Create the string to send to server with a char to let the stream reader know when     the stream is completed.
        String process = "x" + (char) 0;

        //Write the string to the output stream.
        writer.write(process);

        //Clear the output stream.
        writer.flush();

        //Int c reads the data from the input stream.
        int c;

        //Receive the message and print.
        while ( (c = reader.read()) != 0)
        instr.append( (char) c);

            System.out.println("Message received from server: " + instr);
        }
      }

3 个答案:

答案 0 :(得分:0)

您所做的是在单独的线程上从套接字读取输入流,这样您就可以同时读写,而无需等待首先传递信息包。对于写入套接字,您可以将其放入主线程。

答案 1 :(得分:0)

在类中,将一些变量作为私有字段通常是个好主意,并且构造函数负责这些变量的初始收集和初始化。

所以你有

public Client(String host, int port) throws IOException
{
    this.host = host;
    this.port = port;
}

然后其他方法将完成一项工作

e.g。

public void connect () {

   //Stores the host string into a usable IP.
   InetAddress address = InetAddress.getByName(host);

   //Instantiate a socket with the given IP and port name.
   this.socket = new Socket(address, port);  // yes another field

}

然后其他方法将进行读写。

另外,您可能会发现需要使用单独的线程,因此请尝试按照聊天示例进行操作,您可以在互联网上轻松找到它。

答案 2 :(得分:0)

如果我理解你的问题,变量writer应该是一个类变量,而不是在Client的构造函数范围内:

public class Client {
    private OutputStreamWriter writer;
    private String host;
    private int port;

    public Client(String host2, int port2) throws IOException
    {
        // ...
    }

    public void readAndWrite() {
        // ...
        writer.write();
    }
}