通过Socket进行Android Studio客户端和Eclipse服务器进行数据传输

服务器代码


```java
package Serversocket;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Serversocket_Android {

    private static final int PORT = 9999;  //设置端口号
    private List<Socket> mList = new ArrayList<Socket>(); //用ArrayList实现多个用户
    private ServerSocket server = null;//服务器
    private ExecutorService mExecutorService = null;
    private String receiveMsg;//接受的信息
    private String sendMsg;//发送的信息

    public static void main(String[] args) {
        new Serversocket_Android();
    }

    public Serversocket_Android() {
        try {
            server = new ServerSocket(PORT);
            mExecutorService = Executors.newCachedThreadPool();
            System.out.println("服务器已启动...");
            Socket client = null;
            while (true) {
                client = server.accept();
                mList.add(client);//客户端加入ArrayList
                mExecutorService.execute(new Service(client));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class Service implements Runnable {
        private Socket socket;
        private BufferedReader in = null;
        private PrintWriter printWriter=null;

        public Service(Socket socket) {
            this.socket = socket;
            try {
                printWriter = new PrintWriter(new BufferedWriter(new OutputStreamWriter( socket.getOutputStream(), "UTF-8")), true);
                in = new BufferedReader(new InputStreamReader(
                        socket.getInputStream(),"UTF-8"));
                printWriter.println("成功连接服务器"+"(服务器发送)");
                System.out.println("成功连接服务器");
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void run() {
            try {
                while (true) {
                    if ((receiveMsg = in.readLine())!=null) {
                        System.out.println("receiveMsg:"+receiveMsg);
                        if (receiveMsg.equals("0")) {
                            System.out.println("客户端请求断开连接");
                            printWriter.println("服务端断开连接"+"(服务器发送)");
                            mList.remove(socket);
                            in.close();
                            socket.close();
                            break;
                        } else {
                            sendMsg = "我已接收:" + receiveMsg + "(服务器发送)";
                            printWriter.println(sendMsg);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

服务器启动状态

客户端代码实现

界面布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.socket.MainActivity"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="客户端"
            android:textColor="@color/deepskyblue"
            android:textSize="20sp" />
    </LinearLayout>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/red"
        android:hint="服务器IP:"
        android:id="@+id/ServerIP" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/red"
        android:hint="通信端口:"
        android:id="@+id/ServerPort" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal"
        android:gravity="center">


        <Button
            android:id="@+id/connect"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="24sp"
            android:text="自动连接"
            android:textColor="@color/royalblue"/>

        <Button
            android:id="@+id/disconnect"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="24sp"
            android:text="连接服务器"
            android:textColor="@color/royalblue"/>
    </LinearLayout>

    <TextView
        android:id="@+id/test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:layout_marginTop="20dp"
        android:text="请在服务器开启的情况下操作"
        />



    <!--临时测试专用区域-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height
  • 5
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Android中自带的Socket类,与Python服务器进行TCP或UDP通信。具体步骤如下: 1. 在Android Studio中创建一个Socket类,并设置服务器的IP地址和端口号。 ```java Socket socket = new Socket("192.168.1.100", 8888); ``` 2. 获取输入流和输出流,以便于向服务器发送数据和接收数据。 ```java OutputStream outputStream = socket.getOutputStream(); InputStream inputStream = socket.getInputStream(); ``` 3. 将采集的语音数据转换成字节数组,然后使用输出流将数据发送给服务器。 ```java byte[] voiceData = getVoiceData(); // 采集语音数据并转换成字节数组 outputStream.write(voiceData); outputStream.flush(); ``` 4. 使用输入流从服务器接收数据。 ```java byte[] receiveData = new byte[1024]; int len = inputStream.read(receiveData); String response = new String(receiveData, 0, len); ``` 5. 关闭Socket连接。 ```java socket.close(); ``` 在Python服务器端,可以使用socket库来监听客户端的连接请求,并接收客户端发送的数据。具体步骤如下: 1. 创建一个socket对象,并指定监听的IP地址和端口号。 ```python import socket server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind(('192.168.1.100', 8888)) server_socket.listen(1) ``` 2. 等待客户端连接,并接收客户端发送的数据。 ```python client_socket, client_address = server_socket.accept() data = client_socket.recv(1024) ``` 3. 处理接收到的数据,然后向客户端发送响应数据。 ```python response = process(data) # 处理接收到的数据 client_socket.send(response) ``` 4. 关闭socket连接。 ```python client_socket.close() server_socket.close() ``` 需要注意的是,Android客户端与Python服务器的通信过程中,需要保证数据的格式和编码方式一致。同时,需要注意安全性问题,例如数据加密、身份验证等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值