在Java服务器插座(Android版) [英] Server socket in Java (Android)
问题描述
您好所有我写我的Android应用程序内的服务器套接字。它应该只允许一个客户端。有趣的是,我想我的服务器发送消息(字符串)到客户端,而他们都可用。我从我想我的服务器由一个尝试采取串并发送给客户端的字符串队列,队列是否空不送只要一些字符串被推入队列发送。
这里是我如何做到这一点:
公共类资源{\r
公共静态队列<串GT; Q =新的LinkedList<串GT;();\r
\r
\r
公共静态同步无效addString(字符串命令){//供读者\r
semaphore.add(命令);\r
}\r
\r
公共同步字符串的getString(){\r
如果(!semaphore.isEmpty()){\r
返回semaphore.remove();\r
}否则返回0;\r
\r
}\r
}
\r
有关,我有串的队列q其中我可以从另一个类添加字符串。这里是我的服务器code:
新主题(新的Runnable(){\r
@覆盖\r
公共无效的run(){\r
而(真){\r
尝试{\r
//监听连接将这个接口实现的。\r
Socket套接字= my_serverSocket.accept();\r
//发送消息到客户端\r
为PrintWriter了=的新PrintWriter(socket.getOutputStream(),TRUE);\r
通过out.println(1);\r
\r
在的BufferedReader =新的BufferedReader(新的InputStreamReader(插座\r
.getInputStream()));\r
弦乐味精= in.readLine();\r
的System.out.println(消息是:+味精);\r
\r
// 整理\r
//附寄();\r
//socket.close();\r
}赶上(IOException异常IOE){\r
ioe.printStackTrace();\r
}赶上(SecurityException异常SE){\r
se.printStackTrace();\r
}\r
}\r
}\r
})开始();
\r
- 您可能要使用的BlockingQueue 。
这是实施生产者comsumer模式的典型方式:生产者将把()
在队列中的字符串,服务器/消费者将取()
A从队列中的字符串。如果队列为空,服务器将阻塞并等待,任选与一个超时。
您可能不需要绑定的队列,所以你可以实例化的的LinkedBlockingQueue 其空构造。
所有实施的BlockingQueue
是线程安全的,所以你不必为他们的基本操作额外的同步。 1
1.如果你需要原子和/或其他记忆效应,你必须自己实现你的线程安全政策如常。
Hello all I am writing server socket inside my Android app. It should allow only one client. Interesting thing is that I want my server to send messages(strings) to client while they are available. I have queue of strings from which i want my server try taking strings on by one and send to the client, if queue empty do not send and as long as some string is pushed into queue send it. Here is how I do that:
public class Resource {
public static Queue<String> q = new LinkedList<String>();
public static synchronized void addString(String commands) {//for reader
semaphore.add(commands);
}
public synchronized String getString() {
if (!semaphore.isEmpty()) {
return semaphore.remove();
} else return "0";
}
}
For that I have a queue q of string where I can add strings from another class. Here is my server code:
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
// Listens for a connection to be made to this socket.
Socket socket = my_serverSocket.accept();
//send message to client
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("1");
BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
String msg = in.readLine();
System.out.println("message is: " + msg);
// tidy up
//in.close();
//socket.close();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SecurityException se) {
se.printStackTrace();
}
}
}
}).start();
Here I want my serve check if queue is empty, if not get one string and send to the client, and repeat this process infinetely. It is something like producer consumer problem. Also I am interested do i need to use synchronized. Is it possible that i will end up with race condition.
- You probably want to use a BlockingQueue.
This is the typical way to implement a producer-comsumer pattern: the producer will put()
a string in the queue, the server/consumer will take()
a string from the queue. If the queue is empty the server will block and wait, optionally with a timeout.
You probably don't need to bound the queue, so you could instantiate a LinkedBlockingQueue with its empty constructor.
All implementations of BlockingQueue
are thread-safe, so you don't need additional synchronisation for their basic operations.1
1. If you need atomicity and/or other memory effects, you must implement your thread safety policies yourself as usual.
这篇关于在Java服务器插座(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!