昨天我试图创建一个android应用程序和一个Windows应用程序。C#客户端 - Java服务器数据读取速度慢? - 已解决
Android应用 - 发送的炭[]使用WIFI的窗口应用程序的一对。
的Windows应用程序 - C#服务器应用程序,监听客户端的数据
不过这两个程序运作良好,但数据从服务器读取是有点很慢。
我发送的是这样的信息。我有一个手柄通过otg连接到我的手机,当我按下按钮A数据变成这样。我甚至尝试在我的android应用程序中放置一个textview,它没有任何延迟地工作,当我按下按钮时,我可以看到我的textview中更改的数据集。
所以我认为问题是在服务器和客户端之间。这是不可能解决的,因为它们是2个不同的平台?我已经尝试过这种东西,Xamarin,它运行良好。但由于某些具体原因,我不想在这一点上使用Xamarin。
比方说,我按下按钮A,B,C,d这样的数据是这样的,我清楚地可以看到它在Android应用TextView的,当然,我会在我的服务器程序看到这个数据,但只有在大约10-30秒的延迟之后。
我的手机是Nexus 5.这是使用两种不同平台的结果吗?
服务器代码: -
private void startBtn_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(StartServer));
thread.Start();
}
void StartServer()
{
Log("Running");
TcpClient client = new TcpClient();
TcpListener server = new TcpListener(IPAddress.Any, 8888);
server.Start();
client = server.AcceptTcpClient();
Log("Client connected");
byte[] byteArray = new byte[8];
NetworkStream inOpt = client.GetStream();
while (true)
{
inOpt.Read(byteArray, 0, 8);
Log(ASCIIEncoding.);
}
}
void Log(string msg)
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(Log), new object[] {msg});
return;
}
logBox.Text = "";
logBox.Text += msg + Environment.NewLine;
}
Android客户端代码: -
char[] keys = {'0', '0', '0', '0', '0', '0', '0', '0'};
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList gameControllerDeviceIds = new ArrayList();
int[] deviceIds = InputDevice.getDeviceIds();
for (int deviceId : deviceIds) {
InputDevice dev = InputDevice.getDevice(deviceId);
int sources = dev.getSources();
// Verify that the device has gamepad buttons, control sticks, or both.
if (((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD)
|| ((sources & InputDevice.SOURCE_JOYSTICK)
== InputDevice.SOURCE_JOYSTICK)) {
// This device is a game controller. Store its device ID.
if (!gameControllerDeviceIds.contains(deviceId)) {
gameControllerDeviceIds.add(deviceId);
Log.d("yolol", String.valueOf(deviceId));
}
}
}
//Log.d("yolol", InputDevice.getDevice(7).getName());
new CheckUsageStartup().execute();
Log.d("sss", Arrays.toString(keys));
textView = (TextView) findViewById(R.id.textView);
}
byte[] toByteArray(String[] strong){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
for(int i = 0; i < strong.length; i++)
{
try{
dos.writeUTF(strong[i]);
}
catch (IOException e){
}
}
return baos.toByteArray();
}
class CheckUsageStartup extends AsyncTask<Void, Integer, String> {
String TAG = getClass().getSimpleName();
protected void onPreExecute() {
Log.d(TAG + " PreExceute", "On pre Exceute......");
}
protected String doInBackground(Void... arg0) {
Log.d(TAG + " DoINBackGround", "On doInBackground...");
try {
Socket socket = new Socket("192.168.1.8", 8888);
OutputStream out = socket.getOutputStream();
Writer writer = new OutputStreamWriter(out, "UTF-8");
while(true){
writer.write(keys, 0, 8);
writer.flush();
}
} catch (IOException e) {
System.out.println(e.getMessage());
return "Instant";
}
}
protected void onProgressUpdate(Integer... a) {
Log.d(TAG + " onProgressUpdate", "You are in progress update ... " + a[0]);
}
protected void onPostExecute(String result) {
Log.d(TAG + " onPostExecute", "" + result);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
int deviceId = event.getDeviceId();
if (deviceId != -1) {
switch(keyCode){
case KeyEvent.KEYCODE_BUTTON_1:
keys[0] = '1';
Log.d("u", "po");
break;
case KeyEvent.KEYCODE_BUTTON_2:
keys[1] = '1';
break;
case KeyEvent.KEYCODE_BUTTON_3:
keys[2] = '1';
break;
case KeyEvent.KEYCODE_BUTTON_4:
keys[3] = '1';
break;
case KeyEvent.KEYCODE_DPAD_UP:
keys[4] = '1';
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
keys[5] = '1';
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
keys[6] = '1';
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
keys[7] = '1';
break;
}
textView.setText(Arrays.toString(keys));
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
int deviceId = event.getDeviceId();
if (deviceId != -1) {
switch(keyCode){
case KeyEvent.KEYCODE_BUTTON_1:
keys[0] = '0';
break;
case KeyEvent.KEYCODE_BUTTON_2:
keys[1] = '0';
break;
case KeyEvent.KEYCODE_BUTTON_3:
keys[2] = '0';
break;
case KeyEvent.KEYCODE_BUTTON_4:
keys[3] = '0';
break;
case KeyEvent.KEYCODE_DPAD_UP:
keys[4] = '0';
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
keys[5] = '0';
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
keys[6] = '0';
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
keys[7] = '0';
break;
}
textView.setText(Arrays.toString(keys));
return true;
}
return super.onKeyUp(keyCode, event);
}
下面的代码。请告诉我可能的解决方案。它编码是否做了这么多的延迟,也许呢?