-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActivity1.cs
More file actions
89 lines (65 loc) · 2.35 KB
/
Activity1.cs
File metadata and controls
89 lines (65 loc) · 2.35 KB
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
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Net.Sockets;
namespace TestTCP
{
[Activity (Label = "TestTCP", MainLauncher = true)]
public class Activity1 : Activity
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
byte[] readBuffer = new byte[100];
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
Button button = FindViewById<Button> (Resource.Id.myButton);
button.Click += HandleButtonClick;
System.Timers.Timer t = new System.Timers.Timer(3000);
t.AutoReset = true;
t.Elapsed += (sender, e) => {
MakeTCP();
};
t.Start();
}
void HandleButtonClick (object sender, EventArgs e)
{
GC.Collect();
}
void MakeTCP ()
{
Android.Util.Log.Info("-------------", "---------------------------------------------");
Android.Util.Log.Info("-------------", "---------------------------------------------");
Android.Util.Log.Info("-------------", "Begin Connect");
TcpClient tcpClient = new TcpClient();
tcpClient.BeginConnect("google.com", 80, ConnectCompleted, tcpClient);
}
void ConnectCompleted (IAsyncResult res)
{
Android.Util.Log.Info("-------------", "ConnectCompleted");
TcpClient tcpClient = (TcpClient)res.AsyncState;
tcpClient.EndConnect(res);
byte[] buf = encoding.GetBytes( "GET\n\n" );
Android.Util.Log.Info("-------------", "BeginWrite");
tcpClient.GetStream().BeginWrite(buf, 0, buf.Length, WriteCompleted, tcpClient);
}
void WriteCompleted (IAsyncResult res){
Android.Util.Log.Info("-------------", "WriteCompleted");
TcpClient tcpClient = (TcpClient)res.AsyncState;
tcpClient.GetStream().EndWrite(res);
Android.Util.Log.Info( "tcpClient.Connected", tcpClient.Connected.ToString());
Android.Util.Log.Info("-------------", "BeginRead");
tcpClient.GetStream().BeginRead(readBuffer, 0, readBuffer.Length, ReadCompleted, tcpClient);
}
void ReadCompleted (IAsyncResult res){
Android.Util.Log.Info("-------------", "ReadCompleted");
TcpClient tcpClient = (TcpClient)res.AsyncState;
tcpClient.GetStream().EndRead(res);
Android.Util.Log.Info("Server response", encoding.GetString( readBuffer ) );
}
}
}