通过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="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:text="数据接收:"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="内容"
            android:id="@+id/getContent" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:text="数据发送:"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="内容"
            android:id="@+id/sendContent" />
    </LinearLayout>

    <Button
        android:id="@+id/sendButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:text="发送数据"
        android:textColor="@color/royalblue"/>

</LinearLayout>

客户端代码实现

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.UnknownHostException;

public class MainActivity extends AppCompatActivity {

    //控件定义
    EditText Serverip, ServerPort;
    Button btnSearch, btnLogin;

    //消息机制
    private Handler messageHandler;

    //开辟一个socket
    Socket socket = null;

    OutputStream OutputStream = null;//定义数据输出流,用于发送数据
    BufferedReader bufferedReader;//声明输入流对象
    InputStream InputStream = null;//定义数据输入流,用于接收数据

    //定义一个逻辑变量,用于判断服务器连接状态
    boolean isConnected = false;

    //用于控制读数据线程是否执行
    boolean RD = false;



    Button sendButton;
    EditText sendContent, getContent;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sockct_android);

        //控件绑定
        Serverip = findViewById(R.id.ServerIP);
        ServerPort = findViewById(R.id.ServerPort);
        btnSearch = findViewById(R.id.connect);
        btnLogin = findViewById(R.id.disconnect);

        sendButton=findViewById(R.id.sendButton);
        sendContent=findViewById(R.id.sendContent);
        getContent=findViewById(R.id.getContent);


        //自动填写按钮事件
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //自动填写服务器固定的IP地址跟端口号
                Serverip.setText("192.168.0.108");
                ServerPort.setText("9999");
                String ip = Serverip.getText().toString();
                String sn = ServerPort.getText().toString();
                if (isConnected != true) {
                    //创建一条新的Socket连接
                    new ClientThread().start();
                    //按钮文字改变
                    btnLogin.setText("断开连接");
                    //页面消息
                    Toast.makeText(MainActivity.this, "服务器连接成功!", Toast.LENGTH_SHORT).show();


                } else {
                    //按钮按下的时候已在连接情况下,服务器断开连接
                    if (socket != null) {
                        try {
                            //退出服务器
                            socket.close();
                            //服务器状态改为空
                            socket = null;
                            //服务器连接转态改为空
                            isConnected = false;
                            //读数据线程不执行
                            RD = false;

                            btnLogin.setText("连接服务器");
                            //页面文字显示
                            Toast.makeText(MainActivity.this, "与服务器断开连接!", Toast.LENGTH_SHORT).show();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

        //连接服务器按钮事件
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //容错机制
                //取出文本框内容,用来判断输入框是否为空
                String ip = Serverip.getText().toString();
                String port = ServerPort.getText().toString();

                if((ip== null || ip.length() == 0 )&& (port== null || port.length() == 0))
                    Toast.makeText(MainActivity.this, "IP地址、端口号不能为空", Toast.LENGTH_SHORT).show();
                else if(ip== null || ip.length() == 0)
                    Toast.makeText(MainActivity.this, "IP地址不能为空", Toast.LENGTH_SHORT).show();
                else if(port== null || port.length() == 0)
                    Toast.makeText(MainActivity.this, "端口号不能为空", Toast.LENGTH_SHORT).show();

                else {
                    //判断服务器连接状态
                    if (isConnected != true) {
                        //创建一条新的Socket连接
                        new ClientThread().start();
                        //按钮文字改变
                        btnLogin.setText("断开连接");
                        //页面消息
                        Toast.makeText(MainActivity.this, "服务器连接成功!", Toast.LENGTH_SHORT).show();


                    } else {
                        //按钮按下的时候已在连接情况下,服务器断开连接
                        if (socket != null) {
                            try {
                                //退出服务器
                                socket.close();
                                //服务器状态改为空
                                socket = null;
                                //服务器连接转态改为空
                                isConnected = false;
                                //读数据线程不执行
                                RD = false;

                                btnLogin.setText("连接服务器");
                                //页面文字显示
                                Toast.makeText(MainActivity.this, "与服务器断开连接!", Toast.LENGTH_SHORT).show();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });

        //发送按钮按下状态
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //这里可以用线程也可以不用线程
                String zt = btnLogin.getText().toString();


                if(zt == "断开连接"){
                    new sj().start();
                    if(sendContent != null && sendContent.length() > 0){
                        //handlerMessage处理发送信息刷新UI界面
                        Message msgMessage =new Message();
                        msgMessage.obj=sendContent.getText().toString();
                        msgMessage.what=0;
                        messageHandler.sendMessage(msgMessage);
                    }
                    else //页面文字显示
                        Toast.makeText(MainActivity.this, "发送的数据不能为空!", Toast.LENGTH_SHORT).show();
                }
                else //页面文字显示
                    Toast.makeText(MainActivity.this, "未连接服务器,请先连接!", Toast.LENGTH_SHORT).show();
            }
        });


        //消息处理机制
        messageHandler = new Handler() { // 等待socket连接成功
            @Override
            public void handleMessage(android.os.Message msgMessage) {
                String sendString="";
                String receiveString="";

                switch (msgMessage.what) {
                    case 0:
                        
                        Toast.makeText(MainActivity.this, "用户端发送:"+msgMessage.obj.toString(), Toast.LENGTH_SHORT).show();
                        sendString="用户端发送:"+msgMessage.obj.toString()+"\n";
                       
                        break;

                    case 1:
                        Toast.makeText(MainActivity.this, "服务器端发送:"+msgMessage.obj.toString(), Toast.LENGTH_SHORT).show();
                        receiveString="服务器端发送:"+msgMessage.obj.toString()+"\n";
                       
                        break;
                    default:
                        break;
                }
            }
        };
    }

    //用线程创建Socket连接,线程不允许更新UI(用Handler实现)
    public class ClientThread extends Thread{
        public void run(){
            //定义两个变量用于储存ip跟端口号
            InetAddress Ip;
            int port;
            try {
                //判断socket的状态,防止重复执行
                if(socket == null){
                    //获取输入的IP地址
                    Ip = InetAddress.getByName(Serverip.getText().toString());
                    //获取输入的端口
                    port = Integer.valueOf(ServerPort.getText().toString());
                    //新建一个socket,连接
                    socket = new Socket(Ip,port);
                    ///获取socket的输出流,接收数据
                    InputStream = socket.getInputStream();


                    //取得输入流、输出流
                    bufferedReader=new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    OutputStream=socket.getOutputStream();

                    //接收数据可用子线程也可直接在此线程操作
                    char[] buffer=new char[256];//定义数组接收输入流数据
                    String bufferString="";//定义一个字符接收数组数据
                    int conut =0;//初始化buffer数组长度为0
                    int tag=0;//初识写入数组的位置

                    isConnected=true;

                    //死循环重复接收输入流数据并进行处理
                    while (true) {
                        //当输入流写入buffer数组的长度大于0时即接收到数据时
                        while ((conut=bufferedReader.read(buffer))>0) {
                            //将buffer数组的数据全部写入bufferString字符类型
                            while ( tag<buffer.length) {
                                bufferString=bufferString+buffer[tag];
                                tag++;
                            }
                            //将数据给messageHandler刷新UI界面
                            Message msgMessage =new Message();
                            msgMessage.obj=bufferString;
                            msgMessage.what=1;
                            messageHandler.sendMessage(msgMessage);
                            //初始化数据,以便处理下一条输入流信息
                            tag=0;
                            bufferString="";
                        }
                    }
                }
                //出错提示
                //UnknownHostExceptionDNS解析出错
                //IOException读写文件异常
            } catch (UnknownHostException e) {
                //在命令行打印异常信息在程序中出错的位置及原因
                e.printStackTrace();
            } catch (IOException e) {
                //在命令行打印异常信息在程序中出错的位置及原因
                e.printStackTrace();
            }
        }
    }


    //向服务器发送数据子程序
    public class sj extends Thread {
        public void run() {
            //判断连接状态
            if (socket != null) {
                try {
                    //判断输入框是否为空
                    if (sendContent != null && sendContent.length() > 0) {
                        //输入框内容转码后向服务器发送
                        OutputStream.write((sendContent.getText().toString()+"\n").getBytes("utf-8"));
                        //清空缓冲区
                        OutputStream.flush();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } // 连接输出流
            }
        }
    }



}

实现效果

连接服务器

有两种方式:第一种直接填入默认的端口号和服务器的IP地址与服务器进行连接。二自己输入IP地址和端口号和服务器匹对
在这里插入图片描述

向服务器发送数据

在这里插入图片描述

断开连接

在这里插入图片描述

总结

最近正在复习计算机网络和学习Android课程,学到socket编程的时候就在想试着将这两门课程结合起来,在这次实验中,对字节输入流输出流,以及socket编程和Android很好的结合在一起,一开始无法连接服务器,向百度寻找问题后,需要在Mainfest添加以下两行代码:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

链接

https://blog.csdn.net/y1204297001/article/details/106467188
作者:陈乃耀

  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 14
    评论
可以使用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服务器的通信过程,需要保证数据的格式和编码方式一致。同时,需要注意安全性问题,例如数据加密、身份验证等。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值