Android 的简单 TCP 侦听器线程..我的无法连接

发布于 2024-12-18 10:08:21 字数 1048 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒的傷心 2024-12-25 10:08:21

您显示的代码用于创建客户端套接字,而不是服务器套接字。请参阅下面的 TCP 服务器套接字示例,取自 SystemBash

class TCPServer
{
   public static void main(String argv[]) throws Exception
      {
         String clientSentence;
         String capitalizedSentence;
         ServerSocket welcomeSocket = new ServerSocket(6789);

         while(true)
         {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient =
               new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            System.out.println("Received: " + clientSentence);
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            outToClient.writeBytes(capitalizedSentence);
         }
      }
}

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:

class TCPServer
{
   public static void main(String argv[]) throws Exception
      {
         String clientSentence;
         String capitalizedSentence;
         ServerSocket welcomeSocket = new ServerSocket(6789);

         while(true)
         {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient =
               new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            clientSentence = inFromClient.readLine();
            System.out.println("Received: " + clientSentence);
            capitalizedSentence = clientSentence.toUpperCase() + '\n';
            outToClient.writeBytes(capitalizedSentence);
         }
      }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文