-1
我有一個服務器端客戶端程序,其中使用JLabel
,我試圖與特定的客戶端進行通信。當我接受任何客戶端時,我將其socket
添加到ArrayList<Socket>
,然後我將socket = socketList.get(1);
分配給我的第一個JLabel
,以便套接字將包含連接到服務器的第一個客戶端。但沒有任何交流發生。我無法確定問題。Socket的ArrayList無法發送消息到客戶端 - Java
//服務器
private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {
PrintWriter out;
try {
socket = socketList.get(2);
out = new PrintWriter(socket.getOutputStream(), true);
out.println("pc2");
} catch (IOException ex) {
Logger.getLogger(third_frame.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void postListen()
{
new Thread(new Runnable()
{
public void run()
{
connect_clients();
}
}).start();
}
//checking clients connected
void connect_clients()
{
try {
ServerSocket listener = new ServerSocket(7700);
jButton1.setText("Server Running!");
jButton1.setEnabled(false);
try {
while (true) {
//socket = listener.accept();
socketList.add(listener.accept());
try {
clientIP = socket.getLocalAddress().getHostName();
}
finally
{
}
}
}
finally
{
}
}
catch(IOException ex)
{
}
}
//客戶
void connect_server()
{
try {
// TODO code application logic here
String serverAddress = JOptionPane.showInputDialog(
"Enter IP Address of a machine that is\n" +
"running the date service on port 9090:");
s = new Socket(serverAddress, 7700);
while(true){
BufferedReader input =
new BufferedReader(new InputStreamReader(s.getInputStream()));
String answer = input.readLine();
System.out.println(answer);
}
}
catch (IOException ex) {
Logger.getLogger(client_form.class.getName()).log(Level.SEVERE, null, ex);
}
}
你有什麼異常? –
將這些catch塊留空意味着您不會看到您的代碼中可能發生的任何異常。你沒有做任何事情來更新'JLabel'。你只是給'clientIP'分配一個值。 – Kayaman
我已經抓住了這些塊。我得到一個空指針異常。但是,當我打印該套接字時,我正在獲取客戶端的地址。 – question