Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用套接字问题的远程程序_Java_Android_Sockets - Fatal编程技术网

Java 使用套接字问题的远程程序

Java 使用套接字问题的远程程序,java,android,sockets,Java,Android,Sockets,我正在编写一个远程程序,它将鼠标位置从android touchTextView传递到服务器,然后在远程计算机上移动鼠标问题是x、y没有输出到服务器控制台。我认为“in.hasNextInt()”或InputStream有问题。我调试了几个小时,但找不到原因( 任何帮助都将不胜感激 服务器 public class Server { private InputStream inStream; private OutputStream outStream; private

我正在编写一个远程程序,它将鼠标位置从android touchTextView传递到服务器,然后在远程计算机上移动鼠标问题是x、y没有输出到服务器控制台。我认为“in.hasNextInt()”或InputStream有问题。我调试了几个小时,但找不到原因(

任何帮助都将不胜感激

服务器

public class Server {
    private InputStream inStream;
    private OutputStream outStream; 
    private Scanner in;
    private PrintWriter out;
    private ServerSocket serverSocket;
    private Socket socket;

    public void run()
    {
        try {
            serverSocket = new ServerSocket(4444);
            System.out.println("Waiting for connection");
            socket = serverSocket.accept(); 

            outStream = socket.getOutputStream();
            inStream = socket.getInputStream(); 
            out = new PrintWriter(outStream, true);
            in = new Scanner(inStream);             

            while(true) {
            //======== PROBLEM HERE COULD BE HERE=======//
                if(in.hasNextInt()) {
                    int x = in.nextInt();
                    int y = in.nextInt());
                    System.out.println(x);
                    System.out.println(y);
                }
            }   
        } catch (IOException e) {
            e.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            // close everything closable
            try {
                in.close();
                out.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            } // end try/catch
        } // end finally
    }

    public static void main(String[] args) {
        Server server = new Server();
        server.run();   
    }
客户

public class Client extends Activity implements OnClickListener, OnTouchListener  {
    private static final String TAG = "Client";
    private Button connectButton;
    private EditText hostEditText;
    private TextView touchTextView;
    private Socket socket;
    InputStream inStream;
    OutputStream outStream;
    Scanner in;
    PrintWriter out;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        connectButton = (Button)findViewById(R.id.connect);
        connectButton.setOnClickListener(this);
        hostEditText = (EditText)findViewById(R.id.host);
        touchTextView= (TextView)findViewById(R.id.touch);
        touchTextView.setOnTouchListener(this);
    }    

    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.connect:
                Log.d(TAG, socket == null ? "null" : "not null");
                if(socket == null)
                    connect();      
                break;
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent e) {
        if(e.getAction() != MotionEvent.ACTION_DOWN || socket == null)
            return false;

        float x, y;
        switch(v.getId()) {

            case R.id.touch:
                x = e.getX();
                y = e.getY();

                sendXY((int)x, (int)y);
                break;
        }
        return true;
    }

    //===== OR THE PROBLEM HERE? ======//
    // send x, y to server
    private void sendXY(int x, int y)
    {
        out.print(x);
        out.print(y);
    }


    private void connect()
    {
        try {
            String hostName = hostEditText.getText().toString();
            Log.d(TAG, hostName);
            socket = new Socket(hostName, 4444);

            inStream = socket.getInputStream();
            outStream = socket.getOutputStream();
            in = new Scanner(inStream);
            out = new PrintWriter(outStream);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    protected void onDestroy() {
        super.onDestroy();
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

因为您使用的是PrintWriter,所以需要刷新输出流以实际发送数据。这可以通过两种方式完成。在客户端,将
out=new PrintWriter(outStream)
更改为
out=new PrintWriter(outStream,true)
以在调用println时启用自动刷新,或者添加
out.flush();
作为sendXY方法的最后一行。

服务器代码是否使其通过了accept?在accept()之后添加打印将有助于验证连接是否已建立。我刚刚进行了测试。连接已建立。请帮助我,我真的不知道Hast灭绝或nextInt有什么问题。