@@ -67,37 +67,54 @@ private void Connect()
67
67
_soc = new Socket ( AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) ;
68
68
_soc . Connect ( _remoteEp ) ;
69
69
Connected = true ;
70
- _soc . ReceiveTimeout = 5 ;
71
70
}
72
71
73
72
public string GetInfo ( ) => $ "{ _targetAddr . HostIP } :{ _targetAddr . Port } ";
74
73
75
- public string ReceiveMessage ( Encoding encoding = null )
74
+ public string ReceiveString ( Encoding encoding = null )
76
75
{
77
76
if ( ! Connected )
78
77
{
79
78
Connect ( ) ;
80
79
}
81
80
82
- var receivedBytes = new byte [ 256 ] ;
83
- System . IO . MemoryStream ms = new System . IO . MemoryStream ( ) ;
84
- for ( ; ; )
85
- {
86
- try
87
- {
88
- int receivedLength = _soc . Receive ( receivedBytes , receivedBytes . Length , 0 ) ;
89
- if ( receivedLength == 0 )
90
- break ;
91
- ms . Write ( receivedBytes , 0 , receivedLength ) ;
92
- }
93
- catch
81
+ var myencoding = encoding ?? Encoding . UTF8 ;
82
+
83
+ try
84
+ {
85
+ //build length of string into a string
86
+ byte [ ] oneByte = new byte [ 1 ] ;
87
+ StringBuilder sb = new StringBuilder ( ) ;
88
+ for ( ; ; )
89
+ {
90
+ int recvd = _soc . Receive ( oneByte , 1 , 0 ) ;
91
+ if ( oneByte [ 0 ] == ( byte ) ' ' )
92
+ break ;
93
+ sb . Append ( ( char ) oneByte [ 0 ] ) ;
94
+ }
95
+
96
+ //receive string of indicated length
97
+ int lenStringBytes = int . Parse ( sb . ToString ( ) ) ;
98
+ byte [ ] buf = new byte [ lenStringBytes ] ;
99
+ int todo = lenStringBytes ;
100
+ int at = 0 ;
101
+ for ( ; ; )
94
102
{
95
- ms . SetLength ( 0 ) ;
96
- break ;
103
+ int recvd = _soc . Receive ( buf , at , todo , SocketFlags . None ) ;
104
+ if ( recvd == 0 )
105
+ throw new InvalidOperationException ( "ReceiveString terminated early" ) ;
106
+ todo -= recvd ;
107
+ at += recvd ;
108
+ if ( todo == 0 )
109
+ break ;
97
110
}
111
+ return myencoding . GetString ( buf , 0 , lenStringBytes ) ;
112
+ }
113
+ catch
114
+ {
115
+ //not sure I like this, but that's how it was
116
+ return "" ;
98
117
}
99
- var myencoding = encoding ?? Encoding . UTF8 ;
100
- return myencoding . GetString ( ms . ToArray ( ) , 0 , ( int ) ms . Length ) ;
101
118
}
102
119
103
120
public int SendBytes ( byte [ ] sendBytes )
@@ -140,13 +157,23 @@ public string SendScreenshot(int waitingTime = 0)
140
157
{
141
158
return Successful ? "Screenshot was sent" : "Screenshot could not be sent" ;
142
159
}
143
- var resp = ReceiveMessage ( ) ;
160
+ var resp = ReceiveString ( ) ;
144
161
return resp == "" ? "Failed to get a response" : resp ;
145
162
}
146
163
147
164
public int SendString ( string sendString , Encoding encoding = null )
148
165
{
149
- var sentBytes = SendBytes ( ( encoding ?? Encoding . UTF8 ) . GetBytes ( sendString ) ) ;
166
+ var payloadBytes = ( encoding ?? Encoding . UTF8 ) . GetBytes ( sendString ) ;
167
+ var strLenOfPayloadBytes = payloadBytes . Length . ToString ( ) ;
168
+ var strLenOfPayloadBytesAsBytes = Encoding . ASCII . GetBytes ( strLenOfPayloadBytes ) ;
169
+
170
+ System . IO . MemoryStream ms = new System . IO . MemoryStream ( ) ;
171
+ ms . Write ( strLenOfPayloadBytesAsBytes , 0 , strLenOfPayloadBytesAsBytes . Length ) ;
172
+ ms . WriteByte ( ( byte ) ' ' ) ;
173
+ ms . Write ( payloadBytes , 0 , payloadBytes . Length ) ;
174
+
175
+ int sentBytes = SendBytes ( ms . ToArray ( ) ) ;
176
+
150
177
Successful = sentBytes > 0 ;
151
178
return sentBytes ;
152
179
}
0 commit comments