Cihaz bağlantısı için TCP kullanan bir Android uygulaması geliştiriyorum. Sorun, soket programlamasında yeniyim. Bir sunucu ve istemci kodu başarıyla yaptım. Her istemci sunucuya bağlanabilir ve sunucu istemciye cevap verebilir. Ancak sunucunun tüm bağlı istemcilere aynı anda mesaj göndermesini sağlayamıyorum. Sunucunun istemciye bir mesaj yayınlamasını sağlayan adımlar nelerdir?Android TCP sunucu yayını
ServerSocket server = null;
try {
server = new ServerSocket(9092); // start listening on the port
} catch (IOException e) {
Log.d("btnCreate onClick", "Could not listen on port: 9092");
}
Socket client = null;
while(true) {
try {
client = server.accept();
} catch (IOException e) {
Log.d("btnCreate onClick", "Accept Failed");
}
//start a new thread to handle this client
Thread t = new Thread(new ClientConn(client));
t.start();
}
Ve sunucu iplik:
class ClientConn implements Runnable {
private Socket client;
ClientConn(Socket client) {
this.client = client;
}
public void run() {
BufferedReader in = null;
PrintWriter out = null;
try {
/* obtain an input stream to this client ... */
in = new BufferedReader(new InputStreamReader(
client.getInputStream()));
/* ... and an output stream to the same client */
out = new PrintWriter(client.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
return;
}
String msg;
try {
while ((msg = in.readLine()) != null) {
Log.d("ClientConn", "Client says: " + msg);
out.println(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Bağlamınızda "yayın" ile ne demek istediğini açıklayabilir misiniz? TCP yayını yok. –
Sorumu düzenledim. Sunucunun tüm bağlı istemcilere bir mesaj göndermesini istiyorum. Buna yayın mı denir? Soket programlamasında yeniyim. –