我有一个谷歌电视盒。而且我有两部手机。
在 GoogleTV Box 上,我运行一个服务器。服务器在端口 6001 上有一个 ServerSocket。此外,服务器有两个用于两个客户端的套接字
第一个设备连接到服务器并使用套接字号一个第二个另一个....
我可以同时将来自 2 个设备的不同消息发布到谷歌电视盒插座并在电视上显示。
我使用以下解决方案:
对于移动客户端(2 台设备)
创建一个带有空白活动的新android项目并将此代码复制到。为包含编辑文本和按钮的客户端创建布局。确保在 ANDROIDMANIFEST.XML 中设置互联网和访问网络权限!!!并将此文件中的 serverIpAddress 编辑为您的服务器可访问的 IP。
package de.android.googletv.gameclient;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Random;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
*
*
*
*/
public class FullscreenActivity extends Activity {
private Socket socket;
private String serverIpAddress = "192.168.22.253"; // GOOGLE TV IP
private static final int PLAYER_1_SERVERPORT = 6001;
private Button bt;
private TextView tv;
String DEVICE_NAME;
private class ConnectToServer extends AsyncTask {
@Override
protected Object doInBackground(Object... params) {
System.out.println("connecting to server...");
try {
InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
socket = new Socket(serverAddr, PLAYER_1_SERVERPORT);
} catch (UnknownHostException e1) {
System.out.println("ERROR REACHING SERVER! 1");
e1.printStackTrace();
} catch (IOException e1) {
System.out.println("ERROR REACHING SERVER! 2");
e1.printStackTrace();
}
System.out.println("Done!");
return params;
}
protected void onPostExecute() {
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
DEVICE_NAME = android.os.Build.MODEL;
Button exit = (Button) findViewById(R.id.dummy_button);
exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.exit(1);
}
});
new ConnectToServer().execute("");
tv = (TextView) findViewById(R.id.editText1);
bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
Random rnd = new Random();
EditText et = (EditText) findViewById(R.id.editText1);
String str = DEVICE_NAME + " ID" + rnd.nextInt() + " says: " + et.getText().toString();
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())),true
);
out.println(str);
Log.d("Client", "Client sent message");
}
catch (UnknownHostException e) {
tv.setText("UnknownHostException");
e.printStackTrace();
}
catch (IOException e) {
tv.setText("IOException");
e.printStackTrace();
}
catch (Exception e) {
tv.setText("Exception");
e.printStackTrace();
}
}
});
}
}
对于服务器(谷歌电视盒)
使用空白活动创建一个新的 android 项目并将此代码复制到。创建一个只有文本字段的布局确保在 ANDROIDMANIFEST.XML 中设置互联网和访问网络权限!!!
package de.android.googletv.gameserver;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
*
*
*
*/
public class FullscreenActivity extends Activity {
// server socket
ServerSocket ss_plr1 = null;
public static final int SERVERPORT_1 = 6001;
int nr_connections = 0;
// socket for player1
Socket s1;
// socket for player2
Socket s2;
Thread myCommsThread = null;
protected static final int MSG_ID = 0x1337;
String mClientMsg = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
Button exit = (Button) findViewById(R.id.dummy_button);
exit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.exit(1);
}
});
TextView tv = (TextView) findViewById(R.id.fullscreen_content);
tv.setText("Nothing from client yet");
myCommsThread = new Thread(new CommsThread());
myCommsThread.start();
}
@Override
protected void onStop() {
super.onStop();
try {
// make sure you close the socket upon exiting
ss_plr1.close();
}
catch (IOException e) {e.printStackTrace(); }
}
Handler myUpdateHandler = new Handler() {
public void handleMessage(Message msg) {
System.out.println("handleMessage("+msg+")");
switch (msg.what) {
case MSG_ID:
TextView tv = (TextView) findViewById(R.id.fullscreen_content);
tv.setText((String)msg.obj);
break;
default:
break;
}
super.handleMessage(msg);
}
};
class CommsThread implements Runnable {
public void run() {
System.out.println("creating new sockets...");
try {
ss_plr1 = new ServerSocket(SERVERPORT_1 );
if (s1 == null)
s1 = ss_plr1.accept();
if (s2 == null)
s2 = ss_plr1.accept();
}
catch (IOException e) {e.printStackTrace();}
new Thread(new ConnectionHandler(s1, myUpdateHandler)).start();
new Thread(new ConnectionHandler(s2, myUpdateHandler)).start();
}
}
}
...服务器需要的是线程消息传递的连接处理程序...
在服务器项目中创建一个名为“ConnectionHandler.java”的附加类并将此代码复制到其中。它处理异步连接。
package de.android.googletv.gameserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import android.os.Handler;
import android.os.Message;
public class ConnectionHandler implements Runnable {
Socket m_socket;
Handler m_updateHandler;
public ConnectionHandler(Socket socket, Handler updateHandler) {
m_socket = socket;
m_updateHandler = updateHandler;
}
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
BufferedReader input = new BufferedReader(new InputStreamReader(m_socket.getInputStream()));
String st = null;
st = input.readLine();
Message m = new Message();
m.what = 0x1337;
m.obj = st;
m_updateHandler.sendMessage(m);
}
catch (IOException e) { e.printStackTrace();}
}
}
}
这不是最好的解决方案。多个“不好”的编码。例如 System.exit(1)。而且它只有两个设备支持。但它适用于不止一种设备,我很确定你会为你的目的修改它。它基于三个网络资源和我自己的其他尝试来使其工作。它只是我的原型....
我无法链接到他们 :( ... 声誉降低。
如果您构建并运行everthing,它应该如下所示:
https://plus.google.com/u/0/109268217221135029753/posts/3iz6SF1hiJa