使用Android中的套接字从服务器向所有客户端发送消息

时间:2017-01-02 14:13:26

标签: android sockets server client

我已成功从客户端向服务器发送消息,我的代码已将消息发送到上次连接的客户端,而不是所有连接的客户端。我需要向所有连接的客户端实现发送消息。我的代码吼叫。

客户

 public class Client extends Activity {

private Socket socket;
Button connectBtn;
EditText ip;
private static final int SERVERPORT = 8080;
private String SERVER_IP;
private Socket clientSocket;
String response = "";
private TextView text;
Handler updateConversationHandler;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ip = (EditText)findViewById(R.id.EditTextIP);
    text = (TextView)findViewById(R.id.msg);
    updateConversationHandler = new Handler();
    connectBtn = (Button)findViewById(R.id.ipButton);
    connectBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SERVER_IP = ip.getText().toString();
            Toast.makeText(Client.this,"ip set",Toast.LENGTH_SHORT).show();
            new Thread(new ClientThread()).start();
        }
    });
}

public void onClick(View view) {
    try {
        EditText et = (EditText) findViewById(R.id.EditText01);
        String str = et.getText().toString();
        PrintWriter out = new PrintWriter(new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream())),
                true);
        out.println(str);
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

class ClientThread implements Runnable {

    @Override
    public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
            socket = new Socket(serverAddr, SERVERPORT);
            CommunicationThread commThread = new CommunicationThread(socket);
            new Thread(commThread).start();
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }


    }
}

class CommunicationThread implements Runnable {
    // private Socket clientSocket;
    private BufferedReader input;
    public CommunicationThread(Socket aClientSocket)
    {
        clientSocket = aClientSocket;
        try {
            this.input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void run()
    {
        while (!Thread.currentThread().isInterrupted())
        {
            try
            {
                String read = input.readLine();
                Log.d("msg","readString"+read);
                updateConversationHandler.post(new updateUIThread(read));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

class updateUIThread implements Runnable {
    private String msg;
    public updateUIThread(String str) {
        this.msg = str;
    }

    @Override
    public void run() {
        //if(msg!=null)
        text.setText("Server Says: "+ msg + "\n");
    }
}

}

服务器

public class MainActivity extends Activity {

private ServerSocket serverSocket;
Handler updateConversationHandler;
Thread serverThread = null;
private TextView text,iptext;
public static final int SERVERPORT = 8080;
String message;
Button btn;
private Socket socket;
Socket  clientSocket;
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text = (TextView) findViewById(R.id.text2);
    iptext = (TextView) findViewById(R.id.ip);
    //btn = (Button)findViewById(R.id.send);
    iptext.setText(getIpAddress());
    updateConversationHandler = new Handler();
    this.serverThread = new Thread(new ServerThread());
    this.serverThread.start();


}

public void onClick(View view) {
    try {
        if(clientSocket==null)
            Toast.makeText(MainActivity.this, "null socket", Toast.LENGTH_SHORT).show();
        else
        {
            Toast.makeText(MainActivity.this, "Msg send to socket", Toast.LENGTH_SHORT).show();
            PrintWriter out = new PrintWriter(new BufferedWriter(
                    new OutputStreamWriter(clientSocket.getOutputStream())),
                    true);
            out.println("viky");
        }
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
protected void onStop() {
    super.onStop();
    try {
        serverSocket.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

class ServerThread implements Runnable {

    public void run() {
        Socket socket = null;
        try {
            serverSocket = new ServerSocket(SERVERPORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (!Thread.currentThread().isInterrupted()) {
            try {
                Log.e("ServerThread","CommunicationThread started");
                socket = serverSocket.accept();
                CommunicationThread commThread = new CommunicationThread(socket);
                new Thread(commThread).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

public String getIpAddress() {
    String ip = "";
    try {
        Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
                .getNetworkInterfaces();
        while (enumNetworkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = enumNetworkInterfaces
                    .nextElement();
            Enumeration<InetAddress> enumInetAddress = networkInterface
                    .getInetAddresses();
            while (enumInetAddress.hasMoreElements()) {
                InetAddress inetAddress = enumInetAddress
                        .nextElement();

                if (inetAddress.isSiteLocalAddress()) {
                    ip += "Server running at : "
                            + inetAddress.getHostAddress();
                }
            }
        }

    } catch (SocketException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        ip += "Something Wrong! " + e.toString() + "\n";
    }
    return ip;
}

class CommunicationThread implements Runnable {

    // private Socket clientSocket;
    private BufferedReader input;
    public CommunicationThread(Socket aClientSocket) {
        clientSocket = aClientSocket;
        try {
            this.input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        while (!Thread.currentThread().isInterrupted()) {

            try {
                String read = input.readLine();
                if(read!=null && !read.equals(""))
                updateConversationHandler.post(new updateUIThread(read));

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

class updateUIThread implements Runnable {
    private String msg;

    public updateUIThread(String str) {
        this.msg = str;
    }

    @Override
    public void run() {
        text.setText(text.getText().toString()+"Client Says: "+ msg + "\n");
    }
}
}

我的代码或错过的内容是否有任何错误?

1 个答案:

答案 0 :(得分:0)

查看您的代码,我认为您在服务器和客户端之间使用TCP连接。不幸的是,在TCP中,没有广播设施(一次向所有客户发送消息)。

或者你可以在这里使用UDP连接mentioned in the docs. 您可以逐个向所有客户端发送消息,但我认为这样做效率不高,所以选择UDP。

UDP的一个服务器客户端示例是:here..

我为UDP实现的代码如下所示:

服务器代码:

import android.os.AsyncTask;
import android.util.Log;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;



public class udpFirst extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... voids) {


    try {
        String messageStr="Hello Android!";
        int server_port = 6668;
        // TODO: fill in UDP Client IP, 255 is for broadcast. You can use 255.255.255.255 also.
        InetAddress local = InetAddress.getByName("192.168.43.255");
        DatagramPacket p = new DatagramPacket(messageStr.getBytes(),
                messageStr.length(),
                local,server_port);
        Log.i("UDP server about to: ", "send");
        DatagramSocket s = new DatagramSocket();
        s.send(p);
        s.close();
        Log.i("***** UDP server: ", "Done sending");
    } catch ( SocketException e) {
        Log.i("***** UDP server has: ", "Socket Exception");
    } catch ( UnknownHostException e ) {
        Log.i("***** UDP server has: ", "UnknownHostException");
    } catch (IOException e){
        Log.i("UDP server has Exc", "e: " + e);
    }

    return null;
}
}

客户端:

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;



public class udpSecond extends AsyncTask<Void,Void,Void> {

Context context;

udpSecond(Context c){
    context = c;
}



@Override
protected Void doInBackground(Void... voids) {

    try {
        Log.i("***** UDP client: ", "starting");
        final String text;
        int server_port = 6668;
        byte[] message = new byte[2048];
        DatagramPacket p = new DatagramPacket(message, message.length);
        DatagramSocket s = new DatagramSocket(server_port);
        Log.i("***** UDP client: ", "about to wait to receive");
        s.receive(p); // blocks until something is received
        Log.i("***** UDP client: ", "received");
        text = new String(message, 0, p.getLength());
        Log.i("*UDP client message: ", text);

        Handler handler =  new Handler(context.getMainLooper());
        handler.post( new Runnable(){
            public void run(){
                Toast.makeText(context, text,Toast.LENGTH_LONG).show();
            }
        });

        Log.d("Udp tutorial","message:" + text);
        s.close();
    } catch ( SocketException e) {
        Log.i("***** UDP client has: ", "Socket Exception");
    } catch (IOException e){
        Log.i("UDPclient has Exception", "e: " + e);
    }

    return null;
}
}

希望这会有所帮助:)