发送消息从基本服务器到特定客户端 [英] Send message from a basic server to a specific client

查看:176
本文介绍了发送消息从基本服务器到特定客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个无线模块M1和M2连接到我的接入点。我有一个android手机连接到同一个接入点。我有一个套接字服务器上我的Andr​​oid手机和两个模块连接到服务器的客户端。现在的问题是,是否有可能从我的手机发送一个字符串消息到模块M1无需送什么东西给M2。我想客户之间做出选择发送邮件到。它甚至有可能在Java中?

I have two wifi modules M1 and M2 that connect to my access point. I have an android phone that connects to the same access point. I have a socket server on my android phone and the two modules join to the server as clients. Now my question is, is it possible to send a string message from my phone to module M1 without having to send anything to M2. I want to choose between clients to send the message to. Is it even possible in Java?

好吧,这里不用。

//setting up server
ServerSocket serverSocket = new ServerSocket(8000, 0, IPaddress);

//creating a client socket to accept it
Socket clientSocket = serverSocket.accept();

现在,我接受了客户端在一个单独的线程,以便主线程不会冻结becauz 接受()函数阻塞。

Now, I accept the client in a seperate thread so that the main thread does not freeze becauz accept() function is blocking.

我不知道该怎么每一个新的客户端连接时创建一个新的线程。此外,我不知道如何限制客户端可以连接的数量。我需要至少5个客户端,并没有更多的。

I don't know how to create a new thread every time a new client connects. Also I dont know how to limit the number of clients that can connect. I need at most 5 clients and no more.

推荐答案

是的,这是可能的。你需要维护每个客户建立一个单独的连接。该的ServerSocket 类有一个接受()函数返回一个插槽对象。重新$ P $该对象psents两点,服务器和一个客户端之间的连接。在一个循环中可以调用的ServerSocket.accept()多次接受所有传入连接。返回每个插槽对象将是不同的客户端。

Yes, it is possible. You need to maintain a separate connection to each client. The ServerSocket class has an accept() function which returns a Socket object. That object represents a connection between two points, your server and one client. You can call ServerSocket.accept() multiple times in a loop to accept all incoming connections. Each Socket object returned will be for a different client.

为了使服务器将消息发送到特定客户端,它需要知道哪个插座属于哪个客户端,因此客户端必须发送一些消息给服务器标识自身,并且服务器将需要阅读和跨preT该消息。然后,它可与该特定的客户端的适当的响应做出反应。

In order to have the server send a message to a specific client, it will need to know which socket belongs to which client, so the clients will have to send some message to the server identifying themselves, and the server will need to read and interpret that message. Then it can respond with the appropriate response for that specific client.

发表您的code,如果你仍然有问题。

Post your code if you are still having trouble.

更新,因为你加code的问题:见的有关创建线程的Andr​​oid文档。这将是超过这个职位上的计算器了很多。

UPDATE because you added code to the question: See the Android Documentation about creating threads. That will be a lot of reading beyond this post on stackoverflow.

至于接受连接和启动线程,只是做在一个循环:

As to accepting connections and starting threads, just do it in a loop:

for(int i = 0; i<5; i++){
    clientSocket = serverSocket.accept();
    // start a new thread, passing it the clientSocket as an argument
}

其他可能有用的链接: https://developer.android.com/resources/articles/painless-threading.html https://developer.android.com/guide/topics/fundamentals/processes-and-threads.html

Other possibly useful links: https://developer.android.com/resources/articles/painless-threading.html https://developer.android.com/guide/topics/fundamentals/processes-and-threads.html

这篇关于发送消息从基本服务器到特定客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆