![](/img/trans.png)
[英]How do I send a GraphicsContext from a client to a server and then to all of the other clients?
[英]How to send an string from a server (or a client) to all clients in JavaRMI?
我正在使用RMI,我的服務器(或客戶端)中有一個字符串obj,我想將其發送給所有客戶端,我該怎么做?
public class Myclass extends UnicastRemoteObject implements Server {
Connection c=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306","root","123");
String str2="";
Myclass()throws Exception{
LocateRegistry.createRegistry(1099);
Naming.rebind("My Class!!",this);
Class.forName("com.mysql.jdbc.Driver");
}
//the override is cause of the interface 'Server'
@Override
public String Reciever(String str1) throws Exception {
str2="Say Hello to "+str1;
return str2;
}
public static void main(String[] args) throws Exception {
Myclass mc=new Myclass();
}
}
在此類中, str2
將發送給正在運行Reciever()
客戶端,如何將其發送給所有客戶端?
從客戶端代碼中,找到服務,然后請求服務器對象,然后可以在此類上調用遠程方法。
http://docs.oracle.com/javase/tutorial/rmi/client.html
另外,為什么Reciever()大寫? 方法的標准慣例是使用駝峰式(小寫字母開頭,新單詞的首字母大寫)。
不知道服務器是否正確,應該看一下本教程以確保您了解RMI的工作方式和原因。 這不是一個冗長的教程。 http://docs.oracle.com/javase/tutorial/rmi/overview.html
RMI服務器不會發送給客戶端。 他們響應客戶,當他們發送一個請求 ,通過調用一個遠程方法。 服務器響應作為返回值發送。 您將必須讓客戶端輪詢服務器以獲取這些消息,否則也必須使客戶端進入服務器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.