实现Android基于socket的C/S聊天通信功能,可以分成如下几个步骤:
1. 建立Server端
- 使用Java的ServerSocket类创建Server端。
- 在Server端运行一个死循环,等待Client发来连接请求(使用Socket类进行连接)。
- 接收Client发送的消息,处理并返回数据给Client。
下面是一个Java Server端的示例代码:
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(7777);
System.out.println("Server started.");
// 等待客户端连接
Socket clientSocket = serverSocket.accept();
System.out.println("Server connected to Client: " + clientSocket.getInetAddress());
// 读取客户端发来的消息
InputStream inputStream = clientSocket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
System.out.println("Client says: " + line);
}
// 写入数据到客户端
OutputStream outputStream = clientSocket.getOutputStream();
PrintWriter out = new PrintWriter(new OutputStreamWriter(outputStream));
out.println("Hello Client!");
out.flush();
clientSocket.close();
serverSocket.close();
}
}
2. 建立Client端
- 使用Java的Socket类创建Client端。
- 连接到Server端。
- 发送消息到Server端,等待Server端返回的消息。
下面是一个Java Client端的示例代码:
public class Client {
public static void main(String[] args) throws IOException {
Socket socket = new Socket("localhost", 7777);
System.out.println("Client connected to Server.");
// 向Server发送消息
OutputStream outputStream = socket.getOutputStream();
PrintWriter out = new PrintWriter(new OutputStreamWriter(outputStream));
out.println("Hello Server!");
out.flush();
// 读取Server返回的消息
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
System.out.println("Server says: " + line);
}
socket.close();
}
}
3. Android端的实现
- 在Android项目中添加AndroidManifest.xml文件中添加网络权限
<uses-permission android:name="android.permission.INTERNET"/>
。 - 使用Android的线程池处理Server端向Client传递数据的任务。
- 通过Socket连接到Server端。
- 使用BufferedReader和PrintWriter进行数据读写操作。
下面是一个Android端的实现示例代码:
public class MainActivity extends AppCompatActivity {
private EditText mEditText;
private Button mButton;
private TextView mTextView;
private ExecutorService mThreadPool;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditText = findViewById(R.id.et_text);
mButton = findViewById(R.id.btn_send);
mTextView = findViewById(R.id.tv_text);
mThreadPool = Executors.newCachedThreadPool();
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = mEditText.getText().toString().trim();
mThreadPool.execute(new ServerTask(text));
mEditText.setText("");
}
});
}
private class ServerTask implements Runnable {
private String mMessage;
public ServerTask(String message) {
mMessage = message;
}
@Override
public void run() {
try {
Socket socket = new Socket("localhost", 7777);
PrintWriter writer = new PrintWriter(socket.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer.println(mMessage);
writer.flush();
String result = reader.readLine();
runOnUiThread(new Runnable() {
@Override
public void run() {
mTextView.setText(result);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Android基于socket实现的简单C/S聊天通信功能 - Python技术站