質問編集履歴

1

タグとソースコードの修正(PythonのコードC#のエラー処理部分、C#のリクエスト用のstrに開業を追加)

2021/12/08 02:21

投稿

syoshinsya-
syoshinsya-

スコア21

test CHANGED
File without changes
test CHANGED
@@ -14,6 +14,248 @@
14
14
 
15
15
  port = 1900
16
16
 
17
+ messages = "M-SEARCH * HTTP/1.1\r\n" +\
18
+
19
+ "HOST: 239.255.255.250:1900\r\n" +\
20
+
21
+ "MX: 5\r\n" +\
22
+
23
+ "MAN: \"ssdp:discover\"\r\n" +\
24
+
25
+ "ST: upnp:rootdevice\r\n\r\n\r\n"
26
+
27
+ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
28
+
29
+ sock.sendto(messages.encode(), (host, port))
30
+
31
+ while True:
32
+
33
+ try:
34
+
35
+ res = sock.recv(4096)
36
+
37
+ print(res.decode())
38
+
39
+ except socket.timeout:
40
+
41
+ break
42
+
43
+ sock.close()
44
+
45
+ ```
46
+
47
+
48
+
49
+ # 問題のC#のコード
50
+
51
+ ```C#
52
+
53
+ Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
54
+
55
+
56
+
57
+ try
58
+
59
+ {
60
+
61
+ IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900);
62
+
63
+ client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);
64
+
65
+
66
+
67
+ string str =
68
+
69
+ "M-SEARCH * HTTP/1.1\r\n" +
70
+
71
+ "HOST: 239.255.255.250:1900\r\n" +
72
+
73
+ "MX: 5\r\n" +
74
+
75
+ "MAN: \"ssdp:discover\"\r\n" +
76
+
77
+ "ST: upnp:rootdevice\r\n";
78
+
79
+ byte[] q = Encoding.UTF8.GetBytes(str);
80
+
81
+
82
+
83
+ client.SendTo(q, q.Length, SocketFlags.None, endPoint);
84
+
85
+ IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
86
+
87
+ EndPoint senderEP = sender;
88
+
89
+
90
+
91
+ List<string> strList = new List<string>();
92
+
93
+ while (true)
94
+
95
+ {
96
+
97
+ try
98
+
99
+ {
100
+
101
+ byte[] data = new byte[1024];
102
+
103
+ client.ReceiveFrom(data, ref senderEP);
104
+
105
+ Console.WriteLine(Encoding.UTF8.GetString(data));
106
+
107
+ }
108
+
109
+ catch(Exception)
110
+
111
+ {
112
+
113
+ break;
114
+
115
+ }
116
+
117
+ }
118
+
119
+ }
120
+
121
+ catch(Exception e)
122
+
123
+ {
124
+
125
+ Console.WriteLine(e.Message);
126
+
127
+ }
128
+
129
+ finally
130
+
131
+ {
132
+
133
+ if (client != null)
134
+
135
+ client.Close();
136
+
137
+ }
138
+
139
+ ```
140
+
141
+
142
+
143
+ #うまくいっていない部分の説明
144
+
145
+ こちらがPythonの実行結果です。デバイスが4つ見つかっています。
146
+
147
+ ```Python
148
+
149
+ HTTP/1.1 200 OK
150
+
151
+ CACHE-CONTROL: max-age=120
152
+
153
+ Location: http://192.168.0.1:2869/upnp/rootdevice.xml
154
+
155
+ SERVER: IGD-HTTP/1.1 UPnP/1.0 UPnP-Device-Host/1.0
156
+
157
+ ST: upnp:rootdevice
158
+
159
+ EXT:
160
+
161
+
162
+
163
+ HTTP/1.1 200 OK
164
+
165
+ Location: http://192.168.0.9:49154/MediaRenderer/desc.xml
166
+
167
+ Cache-Control: max-age=1800
168
+
169
+ Content-Length: 0
170
+
171
+ Server: Linux/3.2 UPnP/1.0 Network_Module/1.0 (RX-V485)
172
+
173
+ EXT:
174
+
175
+ ST: upnp:rootdevice
176
+
177
+
178
+
179
+ HTTP/1.1 200 OK
180
+
181
+ CACHE-CONTROL: max-age=1800
182
+
183
+ DATE: Tue, 07 Dec 2021 14:30:18 GMT
184
+
185
+ EXT:
186
+
187
+ LOCATION: http://192.168.0.4:60606/***/Server0/ddd
188
+
189
+ SERVER: Linux/4.0 UPnP/1.0 Panasonic-UPnP-MW/1.0
190
+
191
+ ST: upnp:rootdevice
192
+
193
+
194
+
195
+ HTTP/1.1 200 OK
196
+
197
+ CACHE-CONTROL: max-age=1800
198
+
199
+ DATE: Tue, 07 Dec 2021 14:30:00 GMT
200
+
201
+ EXT:
202
+
203
+ LOCATION: http://192.168.0.2:38400/***/description.xml
204
+
205
+ SERVER: NetBSD/1.6.1, UPnP/1.0, Portable SDK for UPnP devices/1.6.6, DLNADOC/1.50
206
+
207
+ ST: upnp:rootdevice
208
+
209
+
210
+
211
+ ```
212
+
213
+ こちらはC#の実行結果です。デバイスが2つしか見つけられていません...なぜ...
214
+
215
+ ```C#
216
+
217
+ HTTP/1.1 200 OK
218
+
219
+ CACHE-CONTROL: max-age=120
220
+
221
+ Location: http://192.168.0.1:2869/upnp/rootdevice.xml
222
+
223
+ SERVER: IGD-HTTP/1.1 UPnP/1.0 UPnP-Device-Host/1.0
224
+
225
+ ST: upnp:rootdevice
226
+
227
+ EXT:
228
+
229
+
230
+
231
+
232
+
233
+ HTTP/1.1 200 OK
234
+
235
+ Location: http://192.168.0.9:49154/MediaRenderer/desc.xml
236
+
237
+ Cache-Control: max-age=1800
238
+
239
+ Content-Length: 0
240
+
241
+ Server: Linux/3.2 UPnP/1.0 Network_Module/1.0 (RX-V485)
242
+
243
+ EXT:
244
+
245
+ ST: upnp:rootdevice
246
+
247
+ ```
248
+
249
+
250
+
251
+ #修正履歴
252
+
253
+ ##2021/12/08
254
+
255
+ 修正前
256
+
257
+ ```Python
258
+
17
259
  messages = "M-SEARCH * HTTP/1.1\r\n" +
18
260
 
19
261
  "HOST: 239.255.255.250:1900\r\n" +
@@ -24,47 +266,11 @@
24
266
 
25
267
  "ST: upnp:rootdevice\r\n";
26
268
 
27
- sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
28
-
29
- sock.sendto(message.encode(), (host, port))
30
-
31
- while True:
32
-
33
- try:
34
-
35
- res = sock.recv(4096)
36
-
37
- print(res.decode())
38
-
39
- except socket.timeout:
40
-
41
- break
42
-
43
- sock.close()
44
-
45
- ```
269
+ ```
46
-
47
-
48
-
49
- # 問題のC#のコード
50
270
 
51
271
  ```C#
52
272
 
53
- Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
54
-
55
-
56
-
57
- try
58
-
59
- {
60
-
61
- IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900);
62
-
63
- client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);
64
-
65
-
66
-
67
- string str =
273
+ string str =
68
274
 
69
275
  "M-SEARCH * HTTP/1.1\r\n" +
70
276
 
@@ -76,21 +282,9 @@
76
282
 
77
283
  "ST: upnp:rootdevice\r\n";
78
284
 
79
- byte[] q = Encoding.UTF8.GetBytes(str);
285
+
80
-
81
-
82
-
83
- client.SendTo(q, q.Length, SocketFlags.None, endPoint);
286
+
84
-
85
- IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
86
-
87
- EndPoint senderEP = sender;
88
-
89
-
90
-
91
- List<string> strList = new List<string>();
92
-
93
- while (true)
287
+ while (true)
94
288
 
95
289
  {
96
290
 
@@ -116,132 +310,70 @@
116
310
 
117
311
  }
118
312
 
119
- }
120
-
121
- catch(Exception e)
122
-
123
- {
124
-
125
- Console.WriteLine(e.Message);
126
-
127
- }
128
-
129
- finally
130
-
131
- {
132
-
133
- if (client != null)
134
-
135
- client.Close();
136
-
137
- }
138
-
139
- ```
313
+ ```
140
-
141
-
142
-
314
+
315
+
316
+
143
- #うまくいっていない部分の説明
317
+ 修正後
144
-
145
- こちらがPythonの実行結果です。デバイスが4つ見つかっています。
146
318
 
147
319
  ```Python
148
320
 
149
- HTTP/1.1 200 OK
150
-
151
- CACHE-CONTROL: max-age=120
152
-
153
- Location: http://192.168.0.1:2869/upnp/rootdevice.xml
154
-
155
- SERVER: IGD-HTTP/1.1 UPnP/1.0 UPnP-Device-Host/1.0
321
+ messages = "M-SEARCH * HTTP/1.1\r\n" +\
322
+
156
-
323
+ "HOST: 239.255.255.250:1900\r\n" +\
324
+
325
+ "MX: 5\r\n" +\
326
+
327
+ "MAN: \"ssdp:discover\"\r\n" +\
328
+
157
- ST: upnp:rootdevice
329
+ "ST: upnp:rootdevice\r\n\r\n\r\n"
158
-
159
- EXT:
330
+
160
-
161
-
162
-
163
- HTTP/1.1 200 OK
164
-
165
- Location: http://192.168.0.9:49154/MediaRenderer/desc.xml
166
-
167
- Cache-Control: max-age=1800
168
-
169
- Content-Length: 0
170
-
171
- Server: Linux/3.2 UPnP/1.0 Network_Module/1.0 (RX-V485)
172
-
173
- EXT:
174
-
175
- ST: upnp:rootdevice
176
-
177
-
178
-
179
- HTTP/1.1 200 OK
180
-
181
- CACHE-CONTROL: max-age=1800
182
-
183
- DATE: Tue, 07 Dec 2021 14:30:18 GMT
184
-
185
- EXT:
186
-
187
- LOCATION: http://192.168.0.4:60606/***/Server0/ddd
188
-
189
- SERVER: Linux/4.0 UPnP/1.0 Panasonic-UPnP-MW/1.0
190
-
191
- ST: upnp:rootdevice
192
-
193
-
194
-
195
- HTTP/1.1 200 OK
196
-
197
- CACHE-CONTROL: max-age=1800
198
-
199
- DATE: Tue, 07 Dec 2021 14:30:00 GMT
200
-
201
- EXT:
202
-
203
- LOCATION: http://192.168.0.2:38400/***/description.xml
204
-
205
- SERVER: NetBSD/1.6.1, UPnP/1.0, Portable SDK for UPnP devices/1.6.6, DLNADOC/1.50
206
-
207
- ST: upnp:rootdevice
208
-
209
-
210
-
211
- ```
331
+ ```
212
-
213
- こちらはC#の実行結果です。デバイスが2つしか見つけられていません...なぜ...
332
+
333
+
214
334
 
215
335
  ```C#
216
336
 
217
- HTTP/1.1 200 OK
337
+ string str =
218
-
219
- CACHE-CONTROL: max-age=120
338
+
220
-
221
- Location: http://192.168.0.1:2869/upnp/rootdevice.xml
222
-
223
- SERVER: IGD-HTTP/1.1 UPnP/1.0 UPnP-Device-Host/1.0
339
+ "M-SEARCH * HTTP/1.1\r\n" +
340
+
224
-
341
+ "HOST: 239.255.255.250:1900\r\n" +
342
+
343
+ "MX: 5\r\n" +
344
+
345
+ "MAN: \"ssdp:discover\"\r\n" +
346
+
225
- ST: upnp:rootdevice
347
+ "ST: upnp:rootdevice\r\n\r\n\r\n";
348
+
349
+
350
+
226
-
351
+ while (true)
352
+
353
+ {
354
+
227
- EXT:
355
+ try
228
-
229
-
230
-
231
-
232
-
356
+
233
- HTTP/1.1 200 OK
357
+ {
234
-
235
- Location: http://192.168.0.9:49154/MediaRenderer/desc.xml
358
+
236
-
237
- Cache-Control: max-age=1800
359
+ byte[] data = new byte[1024];
360
+
238
-
361
+ client.ReceiveFrom(data, ref senderEP);
362
+
363
+ strList.Add(Encoding.UTF8.GetString(data));
364
+
365
+ Console.WriteLine(Encoding.UTF8.GetString(data));
366
+
367
+ }
368
+
239
- Content-Length: 0
369
+ catch(SocketException)
240
-
370
+
241
- Server: Linux/3.2 UPnP/1.0 Network_Module/1.0 (RX-V485)
371
+ {
242
-
372
+
243
- EXT:
373
+ break;
244
-
374
+
245
- ST: upnp:rootdevice
375
+ }
376
+
246
-
377
+ }
378
+
247
- ```
379
+ ```