安卓手机socket通信(服务器和客户端)攻略
要实现安卓手机之间的socket通信,需要涉及到两个角色:服务器和客户端。服务器用于监听客户端的请求,客户端则向服务器发送请求并接收响应。以下是详细攻略。
创建服务器
1. 添加网络权限
在AndroidManifest.xml文件中添加网络权限:
<uses-permission android:name="android.permission.INTERNET" />
2. 创建ServerSocket
使用ServerSocket可以监听客户端请求。在服务器端创建一个ServerSocket,并指定一个端口号,等待客户端连接:
try {
ServerSocket serverSocket = new ServerSocket(8888);
Socket socket = serverSocket.accept();
} catch (IOException e) {
e.printStackTrace();
}
3. 接收客户端请求
当客户端连接成功时,可以通过Socket获取输入流和输出流,进行通信:
try {
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
创建客户端
1. 添加网络权限
在AndroidManifest.xml文件中添加网络权限:
<uses-permission android:name="android.permission.INTERNET" />
2. 连接服务器
客户端通过Socket连接服务器,需要指定服务器的IP地址和端口号:
try {
Socket socket = new Socket("192.168.0.100", 8888);
} catch (IOException e) {
e.printStackTrace();
}
3. 向服务器发送请求
客户端向服务器发送请求,可以通过输出流写入数据:
try {
OutputStream outputStream = socket.getOutputStream();
String message = "Hello Server!";
byte[] bytes = message.getBytes();
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
4. 接收服务器响应
客户端从服务器接收响应,可以通过输入流读取数据:
try {
InputStream inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
int len;
StringBuilder sb = new StringBuilder();
while ((len = inputStream.read(bytes)) != -1) {
String s = new String(bytes, 0, len);
sb.append(s);
}
String response = sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
完整示例
以下是一个完整的安卓手机socket通信示例,包含服务器和客户端两部分:
服务器部分
public class ServerActivity extends AppCompatActivity {
private static final String TAG = "ServerActivity";
private TextView mTvMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server);
mTvMessage = findViewById(R.id.tv_message);
new Thread(() -> {
try {
ServerSocket serverSocket = new ServerSocket(8888);
Log.d(TAG, "server started.");
Socket socket = serverSocket.accept();
Log.d(TAG, "client connected.");
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
byte[] buffer = new byte[1024];
int len;
StringBuilder sb = new StringBuilder();
while ((len = inputStream.read(buffer)) != -1) {
String s = new String(buffer, 0, len);
sb.append(s);
}
String request = sb.toString();
Log.d(TAG, "request: " + request);
String response = "Hello Client!";
outputStream.write(response.getBytes());
Log.d(TAG, "response: " + response);
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
}
客户端部分
public class ClientActivity extends AppCompatActivity {
private static final String TAG = "ClientActivity";
private EditText mEtMessage;
private TextView mTvResponse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
mEtMessage = findViewById(R.id.et_message);
mTvResponse = findViewById(R.id.tv_response);
new Thread(() -> {
try {
Socket socket = new Socket("localhost", 8888);
Log.d(TAG, "connected.");
OutputStream outputStream = socket.getOutputStream();
String message = mEtMessage.getText().toString();
byte[] bytes = message.getBytes();
outputStream.write(bytes);
InputStream inputStream = socket.getInputStream();
byte[] buffer = new byte[1024];
int len;
StringBuilder sb = new StringBuilder();
while ((len = inputStream.read(buffer)) != -1) {
String s = new String(buffer, 0, len);
sb.append(s);
}
String response = sb.toString();
Log.d(TAG, "response: " + response);
runOnUiThread(() -> mTvResponse.setText(response));
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
}
以上就是安卓手机socket通信的完整攻略及示例。注意,在实际应用中,客户端和服务器的代码应该分别放在不同的应用中,以实现不同应用之间的socket通信。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:安卓手机socket通信(服务器和客户端) - Python技术站