欧洲变态另类zozo,欧美xxxx做受欧美gaybdsm,欧洲熟妇色xxxx欧美老妇软件,免费人成视频xvideos入口 ,欧美.日韩.国产.中文字幕

歡迎光臨
我們一直在努力

android基于socket的局域網(wǎng)內(nèi)服務(wù)器與客戶端加密通信

  實現(xiàn)了基本的socket通信(即兩臺設(shè)備,一臺用作服務(wù)器,一臺用作客戶端),服務(wù)器進行監(jiān)聽,客戶端發(fā)送加密數(shù)據(jù)到服務(wù)器,服務(wù)器進行解密得到明文。

  注意:本項目中使用了ButterKnife及EventBus作為輔助工具,通信建立時默認網(wǎng)絡(luò)正常(未做局域網(wǎng)網(wǎng)絡(luò)環(huán)境檢測),加密方式為AES加密

  1.效果圖:

  (1)客戶端

  (2)服務(wù)器端

  2.界面布局部分

  (1)服務(wù)器端布局 function_socket_server.xml

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:orientation="vertical">

 <RelativeLayout style="@style/ToolBar">

  <TextView

   style="@style/ToolBar_tv_Title"

   android:text="網(wǎng)絡(luò)加密-服務(wù)器端" />

 </RelativeLayout>

 <LinearLayout

  android:layout_width="match_parent"

  android:layout_height="wrap_content"

  android:orientation="horizontal">

  <Button

   android:id="@+id/btn_startListener"

   android:layout_width="0dp"

   android:layout_height="wrap_content"

   android:layout_weight="1"

   android:text="啟動監(jiān)聽" />

  <Button

   android:id="@+id/btn_stopListener"

   android:layout_width="0dp"

   android:layout_height="wrap_content"

   android:layout_weight="1"

   android:text="停止監(jiān)聽" />

  <Button

   android:id="@+id/btn_getUser"

   android:layout_width="0dp"

   android:layout_height="wrap_content"

   android:layout_weight="1"

   android:text="刷新用戶" />

 </LinearLayout>

 <LinearLayout

  android:layout_width="match_parent"

  android:layout_height="wrap_content"

  android:orientation="horizontal"

  android:padding="10dp">

  <TextView

   android:layout_width="wrap_content"

   android:layout_height="wrap_content"

   android:text="本機地址:" />

  <TextView

   android:id="@+id/tv_localAddress"

   android:layout_width="match_parent"

   android:layout_height="wrap_content"

   android:singleLine="true" />

 </LinearLayout>

 <ScrollView

  android:layout_width="match_parent"

  android:layout_height="match_parent">

  <LinearLayout

   android:layout_width="match_parent"

   android:layout_height="wrap_content"

   android:orientation="vertical">

   <TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="接收到的明文:"

    android:textColor="@color/black" />

   <TextView

    android:id="@+id/tv_receivedContent"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="10dp" />

   <TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="解密后的明文:"

    android:textColor="@color/black" />

   <TextView

    android:id="@+id/tv_decryptContent"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:padding="10dp" />

  </LinearLayout>

 </ScrollView>

</LinearLayout>

  (2)客戶端布局 function_socket_client.xml

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 android:orientation="vertical">

 <RelativeLayout style="@style/ToolBar">

  <TextView

   style="@style/ToolBar_tv_Title"

   android:text="網(wǎng)絡(luò)加密-客戶端" />

 </RelativeLayout>

 <LinearLayout

  android:layout_width="match_parent"

  android:layout_height="wrap_content"

  android:orientation="horizontal"

  android:padding="10dp">

  <TextView

   android:layout_width="wrap_content"

   android:layout_height="wrap_content"

   android:text="服務(wù)器地址:" />

  <EditText

   android:id="@+id/edtTxt_serverAddress"

   android:layout_width="match_parent"

   android:text="192.168.43.1"

   android:layout_height="wrap_content"

   android:singleLine="true" />

 </LinearLayout>

 <ScrollView

  android:layout_width="match_parent"

  android:layout_height="0dp"

  android:layout_weight="1">

  <LinearLayout

   android:layout_width="match_parent"

   android:layout_height="wrap_content"

   android:orientation="vertical">

   <TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="文本內(nèi)容:"

    android:textColor="@color/black" />

   <EditText

    android:id="@+id/edtTxt_Content"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="@color/main_background"

    android:padding="10dp"

    android:text="123木頭人" />

  </LinearLayout>

 </ScrollView>

 <Button

  android:id="@+id/btn_encryptAndSend"

  android:layout_width="match_parent"

  android:layout_height="wrap_content"

  android:layout_gravity="bottom"

  android:text="加密并發(fā)送" />

</LinearLayout>

  (3)用到的style

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<!--通用Title的右側(cè)按鈕-->

<style name="ToolBar_iv_Right">

 <item name="android:layout_width">@dimen/toolbar_icon_dimen</item>

 <item name="android:layout_height">@dimen/toolbar_icon_dimen</item>

 <item name="android:layout_alignParentRight">true</item>

 <item name="android:layout_gravity">end</item>

 <item name="android:clickable">true</item>

 <item name="android:background">?android:actionBarItemBackground</item>

 <item name="android:padding">15dp</item>

</style>

<!--通用Title的TextView-->

<style name="ToolBar_tv_Title">

 <item name="android:layout_width">wrap_content</item>

 <item name="android:layout_height">wrap_content</item>

 <item name="android:layout_centerVertical">true</item>

 <item name="android:layout_marginLeft">@dimen/toolbar_title_haveBack_marginStart</item>

 <item name="android:layout_marginRight">@dimen/toolbar_title_haveBack_marginEnd</item>

 <item name="android:gravity">center</item>

 <item name="android:singleLine">true</item>

 <item name="android:textColor">@color/white</item>

 <item name="android:textSize">20sp</item>

</style>

  3.功能代碼

  (1)基類 BaseEventActivity.Java

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;

public abstract class BaseEventActivity extends AppCompatActivity {

 @Override

 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  getIntentData();

  setContentView(getLayoutResId());

  ButterKnife.bind(this);

  EventBus.getDefault().register(this);

  init();

 }

 protected void getIntentData() {

 }

 @Override

 protected void onDestroy() {

  super.onDestroy();

  EventBus.getDefault().unregister(this);

 }

 protected abstract void init();

 protected abstract int getLayoutResId();

}

  (2)服務(wù)器主界面 Function_Socket.java

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

import android.content.ComponentName;

import android.content.Intent;

import android.content.ServiceConnection;

import android.os.IBinder;

import android.view.View;

import android.widget.TextView;

import org.greenrobot.eventbus.Subscribe;

import org.greenrobot.eventbus.ThreadMode;

import java.io.BufferedReader;

import java.io.FileReader;

import java.util.ArrayList;

import butterknife.BindView;

import butterknife.OnClick;

/**

 * 服務(wù)器界面

 */

public class Function_Socket_Server extends BaseEventActivity {

 @BindView(R.id.tv_localAddress)

 TextView tv_localAddress;

 @BindView(R.id.tv_receivedContent)

 TextView tv_receivedContent;

 @BindView(R.id.tv_decryptContent)

 TextView tv_decryptContent;

 private LocalService localService;//用于啟動監(jiān)聽的服務(wù)

 private ServiceConnection sc;//服務(wù)連接

 @Override

 protected void init() {

  tv_localAddress.setText(ToolUtil.getHostIP());

  sc = new ServiceConnection() {

   @Override

   public void onServiceConnected(ComponentName name, IBinder service) {

    LocalService.LocalBinder localBinder = (LocalService.LocalBinder) service;

    localService = localBinder.getService();

    localService.startWaitDataThread();

    ToastUtil.showToast(Function_Socket_Server.this, "監(jiān)聽已啟動");

   }

   @Override

   public void onServiceDisconnected(ComponentName name) {

   }

  };

  connection();

 }

 @Subscribe(threadMode = ThreadMode.MAIN)

 public void getData(String data) {

  tv_receivedContent.setText(data);

  tv_decryptContent.setText(AESUtil.decrypt(ConstantUtil.password, data));

 }

 /**

  * 綁定service

  */

 private void connection() {

  Intent intent = new Intent(this, LocalService.class);

  bindService(intent, sc, BIND_AUTO_CREATE);

 }

 @Override

 protected int getLayoutResId() {

  return R.layout.function_socket_server;

 }

 /**

  * 獲取連接到本機熱點上的手機ip

  */

 private ArrayList<String> getConnectedIP() {

  ArrayList<String> connectedIP = new ArrayList<>();

  try {

   //通過讀取配置文件實現(xiàn)

   BufferedReader br = new BufferedReader(new FileReader(

     "/proc/net/arp"));

   String line;

   while ((line = br.readLine()) != null) {

    String[] splitted = line.split(" +");

    if (splitted.length >= 4) {

     String ip = splitted[0];

     connectedIP.add(ip);

    }

   }

  } catch (Exception e) {

   e.printStackTrace();

  }

  return connectedIP;

 }

 @OnClick({R.id.btn_startListener, R.id.btn_stopListener, R.id.btn_getUser})

 public void onClick(View v) {

  switch (v.getId()) {

   case R.id.btn_startListener://啟動監(jiān)聽

    connection();

    break;

   case R.id.btn_stopListener://停止監(jiān)聽

    if (sc != null)

     unbindService(sc);

    break;

   case R.id.btn_getUser://刷新連接到此設(shè)備的IP并清空之前接收到的數(shù)據(jù)

    ArrayList<String> connectedIP = getConnectedIP();

    StringBuilder resultList = new StringBuilder();

    for (String ip : connectedIP) {

     resultList.append(ip);

     resultList.append("\n");

    }

    ToastUtil.showToast(this, "連接到手機上的Ip是:" + resultList.toString());

    tv_decryptContent.setText("");

    tv_receivedContent.setText("");

    break;

  }

 }

 public void onDestroy() {

  super.onDestroy();

  if (sc != null)

   unbindService(sc);

 }

}

贊(0) 打賞
未經(jīng)允許不得轉(zhuǎn)載:路由網(wǎng) » android基于socket的局域網(wǎng)內(nèi)服務(wù)器與客戶端加密通信

更好的WordPress主題

支持快訊、專題、百度收錄推送、人機驗證、多級分類篩選器,適用于垂直站點、科技博客、個人站,扁平化設(shè)計、簡潔白色、超多功能配置、會員中心、直達鏈接、文章圖片彈窗、自動縮略圖等...

聯(lián)系我們聯(lián)系我們

覺得文章有用就打賞一下文章作者

非常感謝你的打賞,我們將繼續(xù)提供更多優(yōu)質(zhì)內(nèi)容,讓我們一起創(chuàng)建更加美好的網(wǎng)絡(luò)世界!

支付寶掃一掃

微信掃一掃

登錄

找回密碼

注冊