简体   繁体   English

Java Sockets - 实时传输

[英]Java Sockets - transmission in real time

i got this code form Android-er blogspot, big thanks for him to make me almost understand basic socket connections in java.我从 Android-er blogspot 得到了这个代码,非常感谢他让我几乎理解了 java 中的基本套接字连接。 So i got this client app on my android device, and computer with server running, but how could i make a loop in a client code, to make it send data from EditText in real time?所以我在我的 android 设备和运行服务器的计算机上安装了这个客户端应用程序,但是我如何在客户端代码中创建一个循环,使其实时从 EditText 发送数据? (whenever it changes) Please if someone could clear it out for a complete newbie? (无论何时发生变化)请如果有人可以为一个完整的新手清除它?

-----This is client code (Android-er Copyrights): -----这是客户端代码(Android-er 版权所有):

package com.exercise.AndroidClient;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidClient extends Activity {

EditText textOut;
TextView textIn;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 textOut = (EditText)findViewById(R.id.textout);
 Button buttonSend = (Button)findViewById(R.id.send);
 textIn = (TextView)findViewById(R.id.textin);
 buttonSend.setOnClickListener(buttonSendOnClickListener);
 }

 Button.OnClickListener buttonSendOnClickListener
 = new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
 // TODO Auto-generated method stub
 Socket socket = null;
 DataOutputStream dataOutputStream = null;
 DataInputStream dataInputStream = null;

 try {
  socket = new Socket("192.168.1.101", 8888);
  dataOutputStream = new DataOutputStream(socket.getOutputStream());
  dataInputStream = new DataInputStream(socket.getInputStream());
  dataOutputStream.writeUTF(textOut.getText().toString());
  textIn.setText(dataInputStream.readUTF());
 } catch (UnknownHostException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 }
 finally{
  if (socket != null){
   try {
    socket.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

  if (dataOutputStream != null){
   try {
    dataOutputStream.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

  if (dataInputStream != null){
   try {
    dataInputStream.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
}};
}

-----This is server code (Android-er Copyrights): -----这是服务器代码(Android-er 版权所有):

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;


public class MyServer {

 public static void main(String[] args){
  ServerSocket serverSocket = null;
  Socket socket = null;
  DataInputStream dataInputStream = null;
  DataOutputStream dataOutputStream = null;

  try {
   serverSocket = new ServerSocket(8888);
   System.out.println("Listening :8888");
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  while(true){
   try {
    socket = serverSocket.accept();
    dataInputStream = new DataInputStream(socket.getInputStream());
    dataOutputStream = new DataOutputStream(socket.getOutputStream());
    System.out.println("ip: " + socket.getInetAddress());
    System.out.println("message: " + dataInputStream.readUTF());
    dataOutputStream.writeUTF("Hello!");
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   finally{
    if( socket!= null){
     try {
      socket.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }

    if( dataInputStream!= null){
     try {
      dataInputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }

    if( dataOutputStream!= null){
     try {
      dataOutputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   }
  }
 }
}

There are a few things you will need to change.您需要更改一些内容。

First of all, if you want the data to be sent in real time, you will need to change from using a Button OnClickListener to using a TextWatcher (see addTextChangedListener in TextView )首先,如果您希望实时发送数据,则需要从使用 Button OnClickListener 更改为使用 TextWatcher(参见 TextView 中的addTextChangedListener

As this event will be fired every time the text changes, you will need to open your socket outside of the event (you don't want a new socket each time some text is typed), and then in your listener, you just want to send the new data down the socket.由于每次文本更改时都会触发此事件,因此您需要在事件之外打开套接字(您不希望每次键入一些文本时都需要一个新套接字),然后在您的侦听器中,您只想将新数据发送到套接字。

You can set a text changed listener on your EditText and do the sending from there.您可以在 EditText 上设置文本更改侦听器并从那里进行发送。

edittext.addTextChangedListener(new TextWatcher() {
    public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
 // ... do your sending here
}

But beware that if that sending is not asynchronous, it may block text entry for the user.但请注意,如果该发送不是异步的,它可能会阻止用户的文本输入。 Network latency can be relatively high on GSM networks, so the user may be irritated, when his freshly typed characters will not immediately appear on screen. GSM 网络上的网络延迟可能相对较高,因此用户可能会感到恼火,因为他新键入的字符不会立即出现在屏幕上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM