远程程序使用socket问题

时间:2011-04-26 19:24:21

标签: java android sockets

我正在写一个远程程序,它将鼠标位置从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();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

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