Skip to content

Commit

Permalink
Add post
Browse files Browse the repository at this point in the history
  • Loading branch information
mike_chen committed Jun 4, 2021
1 parent c893d89 commit 1ef716d
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ end
# Performance-booster for watching directories on Windows
gem "wdm", "~> 0.1.1", :install_if => Gem.win_platform?


gem "webrick", "~> 1.7"
47 changes: 28 additions & 19 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ GEM
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
colorator (1.1.0)
concurrent-ruby (1.1.5)
em-websocket (0.5.1)
concurrent-ruby (1.1.8)
em-websocket (0.5.2)
eventmachine (>= 0.12.9)
http_parser.rb (~> 0.6.0)
eventmachine (1.2.7)
eventmachine (1.2.7-x64-mingw32)
ffi (1.12.1-x64-mingw32)
ffi (1.15.1)
ffi (1.15.1-x64-mingw32)
forwardable-extended (2.6.0)
http_parser.rb (0.6.0)
i18n (1.8.2)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
jekyll (4.0.0)
jekyll (4.0.1)
addressable (~> 2.4)
colorator (~> 1.0)
em-websocket (~> 0.5)
Expand All @@ -29,19 +31,20 @@ GEM
rouge (~> 3.0)
safe_yaml (~> 1.0)
terminal-table (~> 1.8)
jekyll-feed (0.13.0)
jekyll-feed (0.15.1)
jekyll (>= 3.7, < 5.0)
jekyll-sass-converter (2.0.1)
jekyll-sass-converter (2.1.0)
sassc (> 2.0.1, < 3.0)
jekyll-seo-tag (2.6.1)
jekyll (>= 3.3, < 5.0)
jekyll-seo-tag (2.7.1)
jekyll (>= 3.8, < 5.0)
jekyll-watch (2.2.1)
listen (~> 3.0)
kramdown (2.1.0)
kramdown (2.3.1)
rexml
kramdown-parser-gfm (1.1.0)
kramdown (~> 2.0)
liquid (4.0.3)
listen (3.2.1)
listen (3.5.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
mercenary (0.3.6)
Expand All @@ -51,26 +54,31 @@ GEM
jekyll-seo-tag (~> 2.1)
pathutil (0.16.2)
forwardable-extended (~> 2.6)
public_suffix (4.0.3)
rb-fsevent (0.10.3)
public_suffix (4.0.6)
rb-fsevent (0.11.0)
rb-inotify (0.10.1)
ffi (~> 1.0)
rouge (3.15.0)
rexml (3.2.5)
rouge (3.26.0)
safe_yaml (1.0.5)
sassc (2.2.1-x64-mingw32)
sassc (2.4.0)
ffi (~> 1.9)
sassc (2.4.0-x64-mingw32)
ffi (~> 1.9)
terminal-table (1.8.0)
unicode-display_width (~> 1.1, >= 1.1.1)
thread_safe (0.3.6)
tzinfo (1.2.6)
tzinfo (1.2.9)
thread_safe (~> 0.1)
tzinfo-data (1.2019.3)
tzinfo-data (1.2021.1)
tzinfo (>= 1.0.0)
unicode-display_width (1.6.1)
unicode-display_width (1.7.0)
wdm (0.1.1)
webrick (1.7.0)

PLATFORMS
x64-mingw32
x86_64-darwin-20

DEPENDENCIES
jekyll (~> 4.0.0)
Expand All @@ -79,6 +87,7 @@ DEPENDENCIES
tzinfo (~> 1.2)
tzinfo-data
wdm (~> 0.1.1)
webrick (~> 1.7)

BUNDLED WITH
2.1.4
2.2.19
32 changes: 31 additions & 1 deletion _posts/2020-08-29-python-tkinter.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ entry = Entry(window, textvariable=value)
entry.insert(END, ' World')
entry.pack()
```
## ScrolledText
沒有textvariable屬性,只能用insert()修改文字

## Combobox
下拉式選單
```python
Expand All @@ -63,10 +66,37 @@ cb.current(0) # 指定選取索引0
cb.pack()
```

## TreeView
可捲動的表格
```python
vbar = Scrollbar(window)
vbar.pack(side=RIGHT, fill=Y)

Style().configure('user.Treeview', rowheight=110) # 自訂風格,調整欄高為110
tv = Treeview(window, yscrollcommand=vbar.set, show='headings', style='user.Treeview')
tv.pack(side=LEFT, fill=BOTH, expand=True)
tv['columns'] = 'id', 'name', 'password', 'auth_code'
tv.heading('#1', text='id')
tv.heading('#2', text='name')
tv.heading('#3', text='password')
tv.heading('#4', text='auth_code')
vbar.config(command=tv.yview)
```

## Grid
自動佈局,每三個項目為一列
```python
def auto_grid(parent, widget):
index = len(parent.winfo_children()) - 1
widget.config(width=10)
# print(f'index={index}, row={index // 3}, column={index % 3}')
widget.grid(row=index // 3, column=index % 3, ipady=12)
```

# Bind
- &lt;Button-1&gt; 左鍵點擊
- &lt;Button-2&gt; windows和unix是中鍵點擊,mac是右鍵點擊,可用`platform.system() == 'Darwin'`判斷
- &lt;Button-3&gt; 右鍵點擊
- &lt;Double-1&gt; 左鍵連擊

參考連結:[http://tcl.tk/man/tcl8.7/TkCmd/bind.htm](http://tcl.tk/man/tcl8.7/TkCmd/bind.htm)
參考連結:[http://tcl.tk/man/tcl8.7/TkCmd/bind.htm]()
153 changes: 153 additions & 0 deletions _posts/2021-06-04-android-adb-forward.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
---
layout: post
title: '[Android] adb forward'
date: 2021-06-04 17:34
categories:
---
# adb forward
轉發PC端4000端口到手機的9999端口
```
adb forward tcp:4000 tcp:9999
```
查看是否轉發成功
```
adb forward --list
```

## 範例
Android建立server socket
```java
private static class SocketThread extends Thread {
ServerSocket serverSocket;

@Override
public void run() {
byte[] buffer = new byte[1024];
try {
serverSocket = new ServerSocket(9999);
Log.d("Mike", "socket open");
Socket clientSocket = serverSocket.accept(); // 此處會阻塞等待
Log.d("Mike", "socket accept");

InputStream is = clientSocket.getInputStream();
OutputStream os = clientSocket.getOutputStream();
while (!isInterrupted()) {
int len = is.read(buffer);
if (len > 0) {
os.write(("echo " + new String(buffer, 0, len)).getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
}
Log.d("Mike", "socket end");
}

public void close() {
if (serverSocket != null) {
try {
interrupt();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```

PC端用python建立client socket
```python
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((socket.gethostname(), 4000))
client.send(b'Hello')
print(client.recv(1024))
```

# adb通道
adb server啟動以後,在本地的5037端口監聽。adb client通過本地的隨機端口與5037端口建立連接。
在這個通道上,client向server發送的命令都遵循如下格式:
1. 命令的長度(Length),由四位的十六進製表示
2. 實際的命令(Payload),通過ASCII編碼

譬如,查看adb當前的版本,client會發起如下命令:
```
000Chost:version
```
000C表示”host:version”這條命令的長度為12個字節。命令中使用了host前綴,目的是為了區分其他類型的命令(後面還會看到shell前綴的命令),host前綴的命令可以理解為只在client與server這個數據通道上存在。

server收到client的請求後,返回的數據遵循如下格式:
1. 如果成功,則返回四個字節的字符串”OKAY”
2. 如果失敗,則返回四個字節的字符串”FAIL”和出錯原因
3. 如果異常,則返回錯誤碼

當Client收到Server返回的”OKAY”後,就可以發繼續發起其他操作命令了。

當與server建立第一個通道的連接後,需要向server發送transport命令,表示接下來,要與adbd進行數據傳輸。
```
host:transport:<serial-number>
```
當server返回“OKAY”後,client後續發送的數據,就直接傳輸到adbd了。

## 範例
Android建立server socket
```java
private static class SocketThread extends Thread {
LocalServerSocket serverSocket;

@Override
public void run() {
byte[] buffer = new byte[1024];
try {
serverSocket = new LocalServerSocket("test");
Log.d("Mike", "socket open");
LocalSocket clientSocket = serverSocket.accept(); // 此處會阻塞等待
Log.d("Mike", "socket accept");

InputStream is = clientSocket.getInputStream();
OutputStream os = clientSocket.getOutputStream();
while (!isInterrupted()) {
int len = is.read(buffer);
if (len > 0) {
os.write(("echo " + new String(buffer, 0, len)).getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
}
Log.d("Mike", "socket end");
}

public void close() {
if (serverSocket != null) {
try {
interrupt();
// 與ServerSocket不同,close不會中斷accept,需用假的連上來跳過
LocalSocket mockSocket = new LocalSocket();
mockSocket.connect(serverSocket.getLocalSocketAddress());
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```

PC端用python建立client socket
```python
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 當socket關閉後,本地端用於該socket的端口就可以立刻被重用
client.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
client.connect((socket.gethostname(), 5037))

transport = 'host:transport:192.168.2.131:5555'
client.send(bytes('%0.4X' % len(transport) + transport, 'ascii'))
print(client.recv(4))
client.send(b'0012localabstract:test')
print(client.recv(4))
client.send(b'Hello')
print(client.recv(1024))
```

參考連結:[https://duanqz.github.io/2015-05-21-Intro-adb]()

0 comments on commit 1ef716d

Please sign in to comment.