所以我正在Android Studio中构建移动应用程序。该应用程序允许不同设备上的多个用户登录其帐户。我想要实现的功能之一是在线用户之间的实时聊天。
我在eclipse中使用本地网络上的客户端 - 服务器模型完成了这项工作。服务器设置一个套接字,该套接字是有线程的,因此多个客户端可以连接和聊天。这在使用Java swing的eclipse中工作,这很容易与服务器通信并且更新UI都在一个类中完成。
然而,我对Android开发相对较新,并且很难让这个工作在客户端和服务器之间的通信可以通过Activity在UI上显示。
有人可以解释您如何在活动和我的客户端类之间建立链接吗?下面我已经把我的Server,ServerThreads和Client类,(通过终端)可以正确地进行通信。
服务器:
package com.degree.abbylaura.demothree.Server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String args[]) throws IOException {
//create a server and client sockets
ServerSocket serverSocket = null;
Socket clientSocket = null;
int portNumber = 8888; //TO BE CHANGED
//setup server socket
try{
serverSocket = new ServerSocket(portNumber);
System.out.println("Socket set up");
//in true loop listen forever for incoming connections
//allows multiple clients to be handled
while(true) {
//accept connection with clients socket
//open reader and writer for communication
try {
//connection accepted
clientSocket = serverSocket.accept();
System.out.println("client accepted");
//create a new multi-server-thread object to handle new client
//pass it the socket returned from the accept and start the thread
new ServerThreads(clientSocket).start();
//carry on listening for new connections forever
} catch (IOException e) {
System.err.println("IO error " + e.getMessage());
}
}
} catch (IOException e) {
System.out.println(e);
} finally {
try{
serverSocket.close();
} catch (IOException e){
System.out.println(e);
}
}
}
}
服务器主题:
package com.degree.abbylaura.demothree.Server;
import com.degree.abbylaura.demothree.Server.ServerRequests;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ServerThreads extends Thread{
private Socket clientSocket;
public ServerThreads(Socket clientSocket){
super();
this.clientSocket = clientSocket;
}
public void run(){
try {
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
PrintWriter outToClient =
new PrintWriter(clientSocket.getOutputStream(), true);
Boolean communicating = true;
String response = null;
String myresponse = "FIRST TO CLIENT"; //debugging
while(communicating){
outToClient.println("FIRST TO CLIENT");
response = inFromClient.readLine();
System.out.println(myresponse + " : " + response);
if(response.equals("FIRST TO SERVER")){
myresponse = "SECOND TO CLIENT";
outToClient.println("SECOND TO CLIENT");
}else if(response.equals("SECOND TO SERVER")){
myresponse = "SERVER ENDING COMMUNICATION";
outToClient.println("SERVER ENDING COMMUNICATION");
}else if(response.equals("CLIENT END")){
communicating = false;
}
}
clientSocket.close();
return;
} catch (IOException e) {
e.printStackTrace();
} finally {
try{
clientSocket.close();
}
catch (IOException e){
e.printStackTrace();
}
}
}
}
客户端:
package com.degree.abbylaura.demothree.Client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String args[]){
String hostName = "localhost";
int portNumber = 8888; //TO CHANGE
ClientHandler handler = new ClientHandler();
//create client socket
Socket socket = null;
BufferedReader inFromServer = null;
PrintWriter outToServer = null;
try{
socket = new Socket(hostName, portNumber);
System.out.println("socket created");
inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
outToServer = new PrintWriter(socket.getOutputStream(), true);
Boolean communicating = true;
String response = null;
String myresponse = null;
while(communicating){
response = inFromServer.readLine();
System.out.println(myresponse + " : " + response);
if(response.equals("FIRST TO CLIENT")){
myresponse = "FIRST TO SERVER";
outToServer.println("FIRST TO SERVER");
}else if(response.equals("SECOND TO CLIENT")){
myresponse = "SECOND TO SERVER";
outToServer.println("SECOND TO SERVER");
}else if(response.equals("SERVER ENDING COMMUNICATION")){
myresponse = "CLIENT END";
outToServer.println("CLIENT END");
communicating = false; //may or may not need this line in
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:0)
1)你可以从研究这个例子开始
https://github.com/schwiz/android-websocket-example
https://github.com/schwiz/websocket-server
(服务器端与语言无关 - 因为我们在这里使用套接字,所以可能是c或java实现或任何其他)
2)基本上你需要熟悉" android api"方面如:
a. Service class
(to maintain communication with server/ do job outside main aka ui thread)
+ Binder class :) functionality
(allowing Activity and Service to communicate with each other)
+ ServiceConnection class
(to bind from Activity to Service to use Binder*)
b. wakelocks
(for above to hold / persist connection)
c. Socket class
(to create connection with server and exchange data)
* binder是一个类似于你想要实现的套接字的android IPC机制(支持linux内核)并且出于安全原因是Google的首选 - 如果你的服务是本地的,你可以跳过部分关于活页夹交易:)并更多地关注基于服务的方法的其他方面。