Socket 服务端和 Android 客户端 服务端:Java 实现的 Socket 服务器 客户端:Android Socket 客户端示例代码
时间: 2023-11-04 07:04:16 浏览: 115
好的,下面我会分别给您提供 Java 实现的 Socket 服务器和 Android Socket 客户端示例代码。
Java 实现的 Socket 服务器
```java
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SocketServer {
public static void main(String[] args) throws IOException {
// 创建一个 ServerSocket,监听指定端口
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Socket 服务器已启动,等待客户端连接...");
// 等待客户端连接
Socket socket = serverSocket.accept();
System.out.println("客户端已连接:" + socket.getInetAddress().getHostAddress());
// 获取输入流和输出流
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
// 读取客户端发送的数据
byte[] buffer = new byte[1024];
int len = inputStream.read(buffer);
String message = new String(buffer, 0, len);
System.out.println("收到客户端消息:" + message);
// 发送响应数据给客户端
String response = "Hello, Client!";
outputStream.write(response.getBytes());
// 关闭资源
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
System.out.println("Socket 服务器已关闭");
}
}
```
Android Socket 客户端示例代码
```java
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private EditText mEtServerIp;
private EditText mEtServerPort;
private EditText mEtMessage;
private Button mBtnSend;
private TextView mTvResponse;
private Socket mSocket;
private InputStream mInputStream;
private OutputStream mOutputStream;
private static final int MSG_RECEIVED = 1;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_RECEIVED:
String message = (String) msg.obj;
mTvResponse.setText(message);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEtServerIp = findViewById(R.id.et_server_ip);
mEtServerPort = findViewById(R.id.et_server_port);
mEtMessage = findViewById(R.id.et_message);
mBtnSend = findViewById(R.id.btn_send);
mTvResponse = findViewById(R.id.tv_response);
mBtnSend.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_send:
new Thread(new Runnable() {
@Override
public void run() {
try {
// 获取服务端 IP 和端口号
String serverIp = mEtServerIp.getText().toString().trim();
int serverPort = Integer.parseInt(mEtServerPort.getText().toString().trim());
// 创建 Socket 连接
mSocket = new Socket(serverIp, serverPort);
// 获取输入流和输出流
mInputStream = mSocket.getInputStream();
mOutputStream = mSocket.getOutputStream();
// 发送消息给服务端
String message = mEtMessage.getText().toString().trim();
mOutputStream.write(message.getBytes());
// 读取服务端响应消息
byte[] buffer = new byte[1024];
int len = mInputStream.read(buffer);
String response = new String(buffer, 0, len);
// 发送消息给主线程更新 UI
Message msg = mHandler.obtainMessage(MSG_RECEIVED, response);
mHandler.sendMessage(msg);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (mSocket != null) {
mSocket.close();
}
if (mOutputStream != null) {
mOutputStream.close();
}
if (mInputStream != null) {
mInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
break;
}
}
}
```
以上代码仅供参考,具体实现要根据您的需求和具体情况进行调整。
阅读全文