最近在做桌面客户端与Android的通信,记录一下吧,最简单的C#和Java的Socket通信:

C#做服务端:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Android通信
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Thread thread;

        private delegate void SocketDelegate(string s);//代理

        int i;
        int port;
        TcpListener listener;
        int BufferSize;

        private void SocketListener()
        {
            if (this.InvokeRequired)
            {
                while (true)
                {
                    SocketDelegate socketDelegate = new SocketDelegate(RefreshUI);

                    this.Invoke(socketDelegate, new object[] { "等待接收数据" });

                    TcpClient remoteClient = listener.AcceptTcpClient();
                    remoteClient.ReceiveBufferSize = BufferSize;
                    NetworkStream streamToClient = remoteClient.GetStream();

                    byte[] buffer = new byte[BufferSize];  // BufferSize为缓存的大小
                    int bytesRead;
                    try
                    {
                        lock (streamToClient)//为了保证数据的完整性以及安全性  锁定数据流
                        {
                            bytesRead = streamToClient.Read(buffer, 0, BufferSize);
                        }
                        string recvStr = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                        this.Invoke(socketDelegate, new object[] { "接收到消息:" + recvStr });


                        streamToClient.Flush();
                        string response = "已收到!";
                        buffer = System.Text.Encoding.UTF8.GetBytes(response);
                        lock (streamToClient)
                        {
                            streamToClient.Write(buffer, 0, buffer.Length);
                        }
                        this.Invoke(socketDelegate, new object[] { "已发送回执" });
                    }
                    catch (Exception ex)
                    {
                        this.Invoke(socketDelegate, new object[] { "错误:" + ex.ToString() });
                    }
                    finally
                    {
                        streamToClient.Dispose();//释放数据流中的数据
                        remoteClient.Close();//释放TcpClient实例
                    }
                }
            }
        }

        private void RefreshUI(string text)
        {
            if (!this.InvokeRequired)
            {
                textBox1.AppendText(text + "\r\n");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            i = 0;
            port = 8088;
            BufferSize = 64 * 1024;
            if (listener == null)
            {
                listener = new TcpListener(new IPEndPoint(IPAddress.Any, port));//ip为服务器IP地址,port为监听的端口
            }
            listener.Start();//开启监听

            textBox1.AppendText("等待客户端连接\r\n");

            thread = new Thread(new ThreadStart(SocketListener));
            thread.Start();
            button1.Enabled = false;
            button2.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(thread != null)
            {
                thread.Abort();
            }
            if(listener != null)
            {
                listener.Stop();
            }
            textBox1.AppendText("已关闭监听\r\n");

            button1.Enabled = true;
            button2.Enabled = false;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(thread != null)
            {
                thread.Abort();
            }
            if (listener != null)
            {
                listener.Stop();
            }
        }
    }
}

Android作为客户端:

private String SendSocket(String text){
    Socket socket;
    String response = "";
    try {// 创建一个Socket对象,并指定服务端的IP及端口号
        socket = new Socket("192.168.1.103", 8088);

        //向服务器端发送数据
        OutputStream out = socket.getOutputStream();
        byte b[] = text.getBytes();
        out.write(b);
        out.flush();
        socket.shutdownOutput();

        //读取服务器端数据
        InputStream in = socket.getInputStream();
        b = new byte[1024];
        in.read(b);

        response = new String(b);

        out.close();
        in.close();
        /************************************************/
    } catch (SocketException e) {
        e.printStackTrace();
        response = "Error1";
    } catch (UnknownHostException e) {
        e.printStackTrace();
        response = "Error2";
    } catch (IOException e) {
        e.printStackTrace();
        response = "Error3";
    }
    finally{
        return response;
    }
}

完成,手工

小广告:关于获取西普实验吧所有Writeup请点击这里查看索引

查看所有Flag需要付费,需要获取所有Flag的童鞋请访问这里成为付费用户,可以自助把自己的注册邮箱加入网站白名单,即可免回复看到本站所有Flag

Flag大全地址:所有Flag

PS:本站不是实验吧的官方站点,纯粹是个人博客,收取Flag费用仅是维持服务器费用,做站不易,且行窃珍惜,如果喜欢我的博客,愿意捐赠的,可以扫描下面的二维码

微信二维码:
支付宝二维码: