我想打开一个TCP连接,以将数据发送到android应用程序中的ip地址。我发现的每个套接字编程文章/线程都同时显示客户端和服务器端代码(通常是聊天程序)。是否可以仅在Android设备上运行客户端代码,然后将任意数据发送到例如Google的IP地址?现在我正在使用此线程(最高支持的答案)中的代码(Android客户端套接字),如何读取数据?在扩展AsynchTask的类中,如下所示:
public class InternetTask extends AsyncTask<Void, Integer, Void> {
public static final int BUFFER_SIZE = 2048;
private Socket socket = null;
private PrintWriter out = null;
private BufferedReader in = null;
private int port = 80;
private String host = null;
private static final String TAG="sure2015test";
public InternetTask(String host,int port) {
this.host=host;
this.port=port;
}
@Override
protected Void doInBackground(Void... args) {
connectWithServer();
Log.i(TAG, "Connected");
sendDataWithString("hello");
Log.i(TAG, "Sent data");
String response=receiveDataFromServer();
Log.i(TAG,response);
disConnectWithServer();
return null;
}
private void connectWithServer() {
try {
if (socket == null) {
socket = new Socket(this.host, this.port);
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
} catch (IOException e) {
Log.i(TAG,"IO Exeception");
e.printStackTrace();
}
}
private void disConnectWithServer() {
if (socket != null) {
if (socket.isConnected()) {
try {
in.close();
out.close();
socket.close();
} catch (IOException e) {
Log.i(TAG,"IO exception disconnecting");
e.printStackTrace();
}
}
}
}
public void sendDataWithString(String message) {
if (message != null) {
connectWithServer();
out.write(message);
out.flush();
}
}
public String receiveDataFromServer() {
try {
String message = "";
int charsRead = 0;
char[] buffer = new char[BUFFER_SIZE];
Log.i(TAG,"Message before: "+message);
while ((charsRead = in.read(buffer)) != -1) {
message += new String(buffer).substring(0, charsRead);
Log.i(TAG,message);
}
Log.i(TAG,"Message after: "+message);
disConnectWithServer(); // disconnect server
return message;
} catch (IOException e) {
Log.i(TAG,"IO Error in receiving message");
return "Error receiving response: " + e.getMessage();
}
}
}
还有我在MainActivity.java中的onCreate方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InternetTask task=new InternetTask("74.125.226.159",80);
task.execute();
}
当我运行它时,我在日志中得到了这个:
05-16 00:01:04.368 622-637/? I/sure2015test﹕ Connected
05-16 00:01:04.369 622-637/? I/sure2015test﹕ Sent data
05-16 00:01:04.369 622-637/? I/sure2015test﹕ Message before:
05-16 00:03:04.405 622-637/com.example.connorstein.sockethelloworld I/sure2015test﹕ Message after:
因此,使用Google的IP在端口80上打开套接字时也没有例外,发送或接收数据也没有例外。看来我没有回应。这是因为我发送的数据(“ hello”)毫无意义吗?我认为至少我会得到一个响应,说无效请求或类似的内容。我也尝试发送“ GET / HTTP / 1.0”,但也没有响应。
Google回应为空白,因为您没有向其发送正确的HTTP请求。模仿普通Web浏览器的请求,您将得到响应。HTTP是建立在TCP之上的协议。您需要遵循该协议,以从服务器中获取所有有用的信息。
获得响应的最小浏览器标头示例:
GET / HTTP/1.1\r\nUser-Agent: curl/7.37.1\r\nHost: www.google.com\r\nAccept: */*\r\n\r\n
不要使用Web服务器,而应考虑使用SSH服务器。SSH服务器将发送类似...
SSH-2.0-OpenSSH_5.3\r\n
...当您连接时。始终响应的服务器对客户端代码进行故障排除要容易得多。请注意,您可能会使系统管理员疯狂地不断连接到他们的ssh服务器。您可能想建立自己的测试对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句