gpt4 book ai didi

java - 远程程序使用socket问题

转载 作者:行者123 更新时间:2023-11-29 22:26:28 25 4
gpt4 key购买 nike

我正在编写一个远程程序,它将鼠标位置从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 方法的最后一行。

关于java - 远程程序使用socket问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5795392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com