2016-12-01 9 views
0

これは私のサーバーです。私は途中でJFrameを使用しています。実際には、私は多くのPCを持っている私は、PC 1でサーバーを実行してから、PC 2とPC 3にあるクライアントを接続します.PC 3クライアントは接続しますが、サーバーはメッセージを受信できません。 PC 2クライアントが接続している間。あなたは、各ソケット(クライアント)のスレッドのために作る、およびそれらを処理する必要があります接続1台のサーバーで複数のクライアントを実行するにはどうすればよいですか?

try{ 
     providerSocket = new ServerSocket(9090); 
     msgArea.append("Waiting for connection...."); 
     connection = providerSocket.accept(); 

     out = new ObjectOutputStream(connection.getOutputStream()); 
     out.flush(); 

     in = new ObjectInputStream(connection.getInputStream()); 
     sendmessage("Connection is successful..."); 

     while(true){ 
      message = (String)in.readObject(); 

      if(!message.isEmpty()) 
       msgArea.append("\nClient: "+message); 
     } 

    } 
    catch(Exception e){ 

    } 

} 

public void sendmessage(String msg){ 

    try{ 
     out.writeObject(msg); 
     out.flush(); 
     msgArea.append("\nServer: "+msg); 
    }catch(Exception e){ 

    } 
} 

/** 
* Creates new form FrmServer 
*/ 
public FrmServer() { 
    initComponents(); 
} 

private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here: 
    sendmessage(txt.getText()); 
} 
public static void main(String args[]) { 
FrmServer s = new FrmServer(); 
    s.setVisible(true); 
    s.run(); 

} 

クライアント

package client; 

import java.net.*; 
import java.io.*; 

public class FrmClient extends javax.swing.JFrame { 

Socket requestSocket; 
ObjectInputStream in; 
ObjectOutputStream out; 
String message; 
/** 
* Creates new form FrmClient 
*/ 
public FrmClient() { 
    initComponents(); 
} 

public void run(){ 

    try{ 
     requestSocket = new Socket("10.99.225.12",9090); 
     msgArea.append("Connected to the server..."); 

     out = new ObjectOutputStream(requestSocket.getOutputStream()); 
     out.flush(); 

     in = new ObjectInputStream(requestSocket.getInputStream()); 

     while(true){ 
      message = (String)in.readObject(); 

      if(!message.isEmpty()); 
       msgArea.append("\nServer: "+message); 
     } 
    } 
    catch(Exception e){ 

    } 

} 

public void sendmessage(String msg){ 

    try{ 
     out.writeObject(msg); 
     out.flush(); 

     msgArea.append("\nClient: "+msg); 
    } 
    catch(Exception e){ 

    } 

} 

private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {           
    // TODO add your handling code here: 
    sendmessage(txt.getText()); 
} 
private void formWindowClosing(java.awt.event.WindowEvent evt) {         
    // TODO add your handling code here: 

    try{ 
     sendmessage("Got to go.. Goodbye!"); 
     in.close(); 
     out.close(); 
     requestSocket.close(); 
    } 
    catch(Exception e){ 

    } 

} 
public static void main(String args[]) { 
FrmClient c = new FrmClient(); 
    c.setVisible(true); 
    c.run(); 
} 
+2

あなたはq個のマルチスレッドをタグ付け。だから、あなたは明らかにその答えをすでに知っているでしょう... – Fildor

+0

このチュートリアルの最後(http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html)で、簡単な例を見つけることができますそれをやる。 – PeterMmm

答えて

0

ため

package server; 
import java.net.*; 
import java.io.*; 

public class FrmServer extends javax.swing.JFrame { 

ServerSocket providerSocket; 
Socket connection=null; 
ObjectOutputStream out; 
ObjectInputStream in; 
String message; 

//To run the connection 
public void run(){ 

。あなたは一度だけconnectionを初期化するものであれば、私は参照してください

は、作られただけ接続は、あります。

すべてのソケットを使用してリストを作成し、入出力ストリームに対して書き込み/読み取りを行うことができます。

0

接続を継続的にリスンするには専用のスレッドが必要です。

接続を受け入れたら、受け入れられた接続ごとに2つのスレッドを作成します.1つは着信データを読み取るスレッド、もう1つはソケットにデータを書き込むスレッドです。

これはコードの簡単な変更ですが、より良い方法で記述することができます。

  1. JFrameクラスがRunnableを実装するようにします。

    public class FrmServer extends javax.swing.JFrame implements Runnable 
    
  2. は、あなたのJFrameクラスの実行方法

    @Override 
    public void run() { 
        while (true) { 
         try { 
          msgArea.append("Waiting for connection...."); 
          connection = providerSocket.accept(); 
          new ConnectionWriter(connection, msgArea).start(); 
          new ConnectionReader(connection, msgArea).start(); 
         } catch (IOException ex) { 
          System.err.out(ex); 
         } 
        } 
    } 
    
  3. がソケットから読み込むためのスレッドを作成したソケットに

    public class ConnectionWriter extends Thread { 
    
        ObjectOutputStream out; 
        Socket connection; 
        JTextArea msgArea; 
    
        public ConnectionWriter(Socket connection, JTextArea msgArea) { 
         this.connection = connection; 
         this.msgArea = msgArea; 
        } 
    
        @Override 
        public void run() { 
         try { 
          out = new ObjectOutputStream(connection.getOutputStream()); 
          out.flush(); 
          sendmessage("Connection is successful..."); 
         } catch (IOException e) { 
          System.out.println(e); 
         } 
        } 
    
        public void sendmessage(String msg) { 
    
         try { 
          out.writeObject(msg); 
          out.flush(); 
          msgArea.append("\nServer: " + msg); 
         } catch (IOException e) { 
          System.err.println(e); 
         } 
        } 
    } 
    
  4. を書くためのスレッドを作成して実装しています。

    public class ConnectionReader extends Thread { 
    
        ObjectInputStream in; 
        Socket connection; 
        JTextArea msgArea; 
    
        public ConnectionReader(Socket connection, JTextArea msgArea) { 
         this.connection = connection; 
         this.msgArea = msgArea; 
        } 
    
        @Override 
        public void run() { 
         try { 
          in = new ObjectInputStream(connection.getInputStream()); 
          while (true) { 
           String message = (String) in.readObject(); 
    
           if (!message.isEmpty()) { 
            msgArea.append("\nClient: " + message); 
           } 
          } 
         } catch (IOException ex) { 
          System.err.out(e); 
         } 
        } 
    
    } 
    
関連する問題