【发布时间】:2021-06-16 05:52:50
【问题描述】:
我想向其他班级发送活动连接,但我不知道我是否可以这样做。
我在客户端有方法和构造函数
public ChatClient(String serverName, int serverPort) {
this.serverName = serverName;
this.serverPort = serverPort;
}
public boolean connect() {
try {
this.socket = new Socket(serverName, serverPort);
System.out.println("Client port: " + socket.getLocalPort());
this.serverOut = socket.getOutputStream();
this.serverIn = socket.getInputStream();
this.bufferedIn = new BufferedReader(new InputStreamReader(serverIn));
return true;
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
在我的另一堂课中调用它
public LoginUserWindow(){
JFrame f= new JFrame("Panel Example");
this.client = new ChatClient("localhost", 2137);
client.connect();
}
现在我已经连接到我的服务器,我想用同样的连接调用另一个类
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
AnotherClass anotherClass = new AnotherClass(here must be active connection i think);
}
});
有人知道我应该怎么做或如何做得更好?
【问题讨论】:
-
只保存为属性?
-
你能举个例子吗?
标签: java multithreading server connection client