Skip to content

Commit 0e473eb

Browse files
committed
Reset TCP/IP connection with a '+' prefix
When running scrcpy with --tcpip=xx.xx.xx.xx, to make sure a new working connection is established, it was first disconnected by a call to: adb disconnect <addr> However, this caused all running instances connected to that address to be killed. Running several instances of scrcpy on the same device is now useful with virtual displays, so change the default behavior to NOT disconnect. To force a reconnection, a '+' prefix can be added: scrcpy --tcpip=+192.168.0.x Fixes #5562 <#5562>
1 parent b26b4fb commit 0e473eb

File tree

5 files changed

+33
-14
lines changed

5 files changed

+33
-14
lines changed

app/scrcpy.1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,13 +518,15 @@ Enable "show touches" on start, restore the initial value on exit.
518518
It only shows physical touches (not clicks from scrcpy).
519519

520520
.TP
521-
.BI "\-\-tcpip\fR[=\fIip\fR[:\fIport\fR]]
522-
Configure and reconnect the device over TCP/IP.
521+
.BI "\-\-tcpip\fR[=[+]\fIip\fR[:\fIport\fR]]
522+
Configure and connect the device over TCP/IP.
523523

524524
If a destination address is provided, then scrcpy connects to this address before starting. The device must listen on the given TCP port (default is 5555).
525525

526526
If no destination address is provided, then scrcpy attempts to find the IP address and adb port of the current device (typically connected over USB), enables TCP/IP mode if necessary, then connects to this address before starting.
527527

528+
Prefix the address with a '+' to force a reconnection.
529+
528530
.TP
529531
.BI "\-\-time\-limit " seconds
530532
Set the maximum mirroring time, in seconds.

app/src/adb/adb.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ sc_adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
412412

413413
// "adb connect" always returns successfully (with exit code 0), even in
414414
// case of failure. As a workaround, check if its output starts with
415-
// "connected".
415+
// "connected" or "already connected".
416416
char buf[128];
417417
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf) - 1);
418418
sc_pipe_close(pout);
@@ -429,7 +429,8 @@ sc_adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
429429
assert((size_t) r < sizeof(buf));
430430
buf[r] = '\0';
431431

432-
ok = !strncmp("connected", buf, sizeof("connected") - 1);
432+
ok = !strncmp("connected", buf, sizeof("connected") - 1)
433+
|| !strncmp("already connected", buf, sizeof("already connected") - 1);
433434
if (!ok && !(flags & SC_ADB_NO_STDERR)) {
434435
// "adb connect" also prints errors to stdout. Since we capture it,
435436
// re-print the error to stderr.

app/src/cli.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -860,16 +860,17 @@ static const struct sc_option options[] = {
860860
{
861861
.longopt_id = OPT_TCPIP,
862862
.longopt = "tcpip",
863-
.argdesc = "ip[:port]",
863+
.argdesc = "[+]ip[:port]",
864864
.optional_arg = true,
865-
.text = "Configure and reconnect the device over TCP/IP.\n"
865+
.text = "Configure and connect the device over TCP/IP.\n"
866866
"If a destination address is provided, then scrcpy connects to "
867867
"this address before starting. The device must listen on the "
868868
"given TCP port (default is 5555).\n"
869869
"If no destination address is provided, then scrcpy attempts "
870870
"to find the IP address of the current device (typically "
871871
"connected over USB), enables TCP/IP mode, then connects to "
872-
"this address before starting.",
872+
"this address before starting.\n"
873+
"Prefix the address with a '+' to force a reconnection.",
873874
},
874875
{
875876
.longopt_id = OPT_TIME_LIMIT,

app/src/server.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -829,11 +829,14 @@ sc_server_switch_to_tcpip(struct sc_server *server, const char *serial) {
829829
}
830830

831831
static bool
832-
sc_server_connect_to_tcpip(struct sc_server *server, const char *ip_port) {
832+
sc_server_connect_to_tcpip(struct sc_server *server, const char *ip_port,
833+
bool disconnect) {
833834
struct sc_intr *intr = &server->intr;
834835

835-
// Error expected if not connected, do not report any error
836-
sc_adb_disconnect(intr, ip_port, SC_ADB_SILENT);
836+
if (disconnect) {
837+
// Error expected if not connected, do not report any error
838+
sc_adb_disconnect(intr, ip_port, SC_ADB_SILENT);
839+
}
837840

838841
LOGI("Connecting to %s...", ip_port);
839842

@@ -849,7 +852,7 @@ sc_server_connect_to_tcpip(struct sc_server *server, const char *ip_port) {
849852

850853
static bool
851854
sc_server_configure_tcpip_known_address(struct sc_server *server,
852-
const char *addr) {
855+
const char *addr, bool disconnect) {
853856
// Append ":5555" if no port is present
854857
bool contains_port = strchr(addr, ':');
855858
char *ip_port = contains_port ? strdup(addr)
@@ -860,7 +863,7 @@ sc_server_configure_tcpip_known_address(struct sc_server *server,
860863
}
861864

862865
server->serial = ip_port;
863-
return sc_server_connect_to_tcpip(server, ip_port);
866+
return sc_server_connect_to_tcpip(server, ip_port, disconnect);
864867
}
865868

866869
static bool
@@ -885,7 +888,7 @@ sc_server_configure_tcpip_unknown_address(struct sc_server *server,
885888
}
886889

887890
server->serial = ip_port;
888-
return sc_server_connect_to_tcpip(server, ip_port);
891+
return sc_server_connect_to_tcpip(server, ip_port, false);
889892
}
890893

891894
static void
@@ -972,7 +975,13 @@ run_server(void *data) {
972975
sc_adb_device_destroy(&device);
973976
}
974977
} else {
975-
ok = sc_server_configure_tcpip_known_address(server, params->tcpip_dst);
978+
// If the user passed a '+' (--tcpip=+ip), then disconnect first
979+
const char *tcpip_dst = params->tcpip_dst;
980+
bool plus = tcpip_dst[0] == '+';
981+
if (plus) {
982+
++tcpip_dst;
983+
}
984+
ok = sc_server_configure_tcpip_known_address(server, tcpip_dst, plus);
976985
if (!ok) {
977986
goto error_connection_failed;
978987
}

doc/connection.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ scrcpy --tcpip=192.168.1.1 # default port is 5555
8585
scrcpy --tcpip=192.168.1.1:5555
8686
```
8787

88+
Prefix the address with a '+' to force a reconnection:
89+
90+
```bash
91+
scrcpy --tcpip=+192.168.1.1
92+
```
93+
8894

8995
### Manual
9096

0 commit comments

Comments
 (0)