Android Socket实现简单聊天功能以及文件传输的步骤如下:
1. 创建服务端
首先,需要创建一个服务端,用于接收客户端请求。在服务端创建Socket实例,并指定端口号,即可监听客户端的请求。以下是一个简单的服务端代码示例,用于接受客户端的连接请求并接受消息:
public class ServerSocketThread extends Thread {
@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(8080);
Socket socket = serverSocket.accept();
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
String message = inputStream.readUTF();
System.out.println("收到客户端发来的消息:" + message);
inputStream.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. 创建客户端
接下来需要创建一个客户端,用于向服务端发送消息。在客户端中,同样需要创建Socket实例,并指定服务端的IP地址和端口号。这样就可以连接到服务端,并向其发送消息。以下是一个简单的客户端代码示例,用于向服务端发送消息:
public class SocketThread extends Thread {
@Override
public void run() {
try {
Socket socket = new Socket("localhost", 8080);
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeUTF("Hello, Server!");
outputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3. 实现简单聊天功能
有了服务端和客户端,就可以实现简单的聊天功能了。可以使用一个循环,让客户端和服务端轮流发送消息。以下是一个简单的示例聊天程序,其中使用了异步任务来运行服务端和客户端。
public class ChatActivity extends AppCompatActivity {
private TextView mMsgTextView;
private EditText mMsgEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
mMsgTextView = findViewById(R.id.msg_text_view);
mMsgEditText = findViewById(R.id.msg_edit_text);
findViewById(R.id.send_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendMessage();
}
});
new ServerSocketThread().start();
new SocketThread().start();
}
private void sendMessage() {
String message = mMsgEditText.getText().toString();
if (TextUtils.isEmpty(message)) {
return;
}
mMsgEditText.getText().clear();
mMsgTextView.append("I: " + message + "\n");
new AsyncTask<String, Void, Void>() {
@Override
protected Void doInBackground(String... strings) {
try {
Socket socket = new Socket("localhost", 8080);
DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
outputStream.writeUTF(strings[0]);
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
String receivedMessage = inputStream.readUTF();
mMsgTextView.append("Server: " + receivedMessage + "\n");
outputStream.close();
inputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute(message);
}
}
4. 实现文件传输
要实现文件传输,只需要使用Java IO的File、FileInputStream、FileOutputStream、BufferedInputStream和BufferedOutputStream等类来读取和写入文件数据。只需要在客户端发送文件的连接后,从本地读取文件的字节数组,然后写入到Socket的输出流中;在服务端接收到文件的连接后,从输入流中读取字节数组,然后写入到本地文件中。
以下是一个简单的文件传输示例程序。在客户端中,使用异步任务读取要发送的文件,并将其写入Socket的输出流中:
public class FileClientThread extends Thread {
private String mFilePath;
public FileClientThread(String filePath) {
mFilePath = filePath;
}
@Override
public void run() {
try {
Socket socket = new Socket("localhost", 8080);
File file = new File(mFilePath);
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
OutputStream outputStream = socket.getOutputStream();
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer, 0, bytesRead);
bufferedOutputStream.flush();
}
bufferedInputStream.close();
bufferedOutputStream.close();
outputStream.close();
fileInputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在服务端中,新建一个线程来接收文件,读取Socket输入流中的数据,并将其写入到文件中。
public class FileServerThread extends Thread {
private String mSavePath;
public FileServerThread(String savePath) {
mSavePath = savePath;
}
@Override
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(8080);
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
File file = new File(mSavePath);
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer, 0, bytesRead);
bufferedOutputStream.flush();
}
bufferedInputStream.close();
bufferedOutputStream.close();
fileOutputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
至此,Android Socket实现简单聊天功能以及文件传输的攻略完成。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:android Socket实现简单聊天功能以及文件传输 - Python技术站