Remote program using socket problem
I'm writing a remote program which passes mouse position from android touchTextView to server, then moving mouse on remoted computer. The problem is x, y aren't output to server console. I think there is something wrong with "in.hasNextInt()" or InputStream. I debugged for hours but I can't find the reason. :(
Any help will be appreciated.
Server
public class Server {
private InputStream inStream;
private OutputStream outS开发者_Go百科tream;
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();
}
Client
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();
}
}
}
Because you are using PrintWriter, you need to flush the output stream to actually send the data. This can be accomplished in 2 ways. In the client, change out = new PrintWriter(outStream)
to out = new PrintWriter(outStream, true)
to turn on automatic flushing whenever a println is called, or, add out.flush();
as the last line in your sendXY method.
精彩评论