Android 的简单 TCP 侦听器线程..我的无法连接
在 Android 上,我尝试实现一个简单的 TCP 侦听器线程(或从任何地方复制它)。它应该简单地等待文本,然后执行某些操作。文本已发送,这部分有效,但此侦听器线程甚至没有创建用于正确侦听的套接字。
有谁对我有想法,有什么问题或其他简单的方法吗? 文本是我自己定义的,而不是 html。我只发现 http 处理程序太复杂了。
import java.lang.*;
import java.io.*;
import java.net.*;
public class Client implements Runnable {
public static void main(String args[]) {
System.out.print("Listening Thread started\n");
try {
Socket skt = new Socket("localhost", 2999);
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
System.out.print("Received string: '");
while (!in.ready()) {}
System.out.println(in.readLine()); // Read one line and output it
System.out.print("'\n");
in.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
System.err.println(e);
}
}
public Client () {
}
@Override
public void run() {
// TODO Auto-generated method stub
main(null);
}
}
On Android I tried to implement a simple TCP Listener Thread (or copied it from anywhere). It should simply wait for a Text and then do something. The Text is sent, this part works, but this listener-Thread doesn´t even create the Socket for listening correctly.
Has anyone an Idea, whats wrong or another simple approach for me?
The text is defined b myself and not html. I only found much too complicated http-handlers.
import java.lang.*;
import java.io.*;
import java.net.*;
public class Client implements Runnable {
public static void main(String args[]) {
System.out.print("Listening Thread started\n");
try {
Socket skt = new Socket("localhost", 2999);
BufferedReader in = new BufferedReader(new
InputStreamReader(skt.getInputStream()));
System.out.print("Received string: '");
while (!in.ready()) {}
System.out.println(in.readLine()); // Read one line and output it
System.out.print("'\n");
in.close();
}
catch(Exception e) {
System.out.print("Whoops! It didn't work!\n");
System.err.println(e);
}
}
public Client () {
}
@Override
public void run() {
// TODO Auto-generated method stub
main(null);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您显示的代码用于创建客户端套接字,而不是服务器套接字。请参阅下面的 TCP 服务器套接字示例,取自 SystemBash :
The code you showed is used to create a client socket, not a server socket. see below an example of TCP server socket, taken from SystemBash: