本文整理汇总了C#中System.Net.Connection.Connect方法的典型用法代码示例。如果您正苦于以下问题:C# Connection.Connect方法的具体用法?C# Connection.Connect怎么用?C# Connection.Connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.Connection
的用法示例。
在下文中一共展示了Connection.Connect方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Timeout
public void Timeout()
{
// Had to pick a routable IP, but one that won't return packets... don't know of a better choice than this
var connection = new Connection(new IPEndPoint(IPAddress.Parse("10.230.220.210"), 28015));
connection.ConnectTimeout = TimeSpan.FromSeconds(1);
connection.Connect();
}
示例2: ShouldNotConnectToWrongPort
public void ShouldNotConnectToWrongPort()
{
var connected = true;
var connection = new Connection(new IPEndPoint(IPAddress.Loopback, 7777));
connection.Connect((success) => { connected = success; _completion.Set(); });
_completion.WaitOne();
Assert.IsFalse(connected);
}
示例3: ShouldConnectToListener
public void ShouldConnectToListener()
{
var connected = false;
var connection = new Connection(new IPEndPoint(IPAddress.Loopback, 9999));
connection.Connect((success) => { connected = success; _completion.Set(); });
_completion.WaitOne();
Assert.IsTrue(connected);
}
示例4: Cancel
public void Cancel()
{
CancellationTokenSource cts = new CancellationTokenSource();
cts.Cancel();
try
{
var connection = new Connection(new IPEndPoint(IPAddress.Parse("10.230.220.210"), 28015));
connection.Connect(cts.Token);
Assert.Fail("Expected exception");
}
catch (AggregateException ex)
{
Assert.That(ex.InnerException is TaskCanceledException);
}
}
示例5: ShouldSendData
public void ShouldSendData()
{
var localConnectionWaiter = new ManualResetEvent(false);
var remoteConnectionWaiter = new ManualResetEvent(false);
Connection remoteConnection = null;
_remote.ConnectionRequested += (sender, args) => {
remoteConnection = new Connection(args.Socket);
remoteConnectionWaiter.Set();
};
var localConnection = new Connection(new IPEndPoint(IPAddress.Loopback, 9999));
localConnection.Connect(c => localConnectionWaiter.Set());
WaitHandle.WaitAll(new WaitHandle[] { localConnectionWaiter, remoteConnectionWaiter });
localConnectionWaiter.Reset();
remoteConnectionWaiter.Reset();
var remoteBuffer = new Buffer(new byte[1]);
remoteConnection.Receive(remoteBuffer, (i, b) => remoteConnectionWaiter.Set());
localConnection.Send(new Buffer(new byte[] { 0xf1 }), (i, b) => localConnectionWaiter.Set());
WaitHandle.WaitAll(new WaitHandle[] { localConnectionWaiter, remoteConnectionWaiter });
Assert.AreEqual(0xf1, remoteBuffer.Segment.Array[0]);
}
示例6: CreateConnection
// IRC COMMANDS
private void CreateConnection()
{
//The hostname of the IRC server
string server = serverBox.Text;
//The bot's nick on IRC
string nick = usernameBox.Text;
string password = OAuthBox.Text;
//Fire up the Ident server for those IRC networks
//silly enough to use it.
Identd.Start(nick);
//A ConnectionArgs contains all the info we need to establish
//our connection with the IRC server and register our bot.
//This line uses the simplfied contructor and the default values.
//With this constructor the Nick, Real Name, and User name are
//all set to the same value. It will use the default port of 6667 and no server
//password.
ConnectionArgs cargs = new ConnectionArgs(nick, server);
cargs.ServerPassword = password;
cargs.Port = 6667;
//When creating a Connection two additional protocols may be
//enabled: CTCP and DCC. In this example we will disable them
//both.
connection = new Connection(cargs, false, false);
//NOTE
//We could have created multiple Connections to different IRC servers
//and each would process messages simultaneously and independently.
//There is no fixed limit on how many Connection can be opened at one time but
//it is important to realize that each runs in its own Thread. Also,separate event
//handlers are required for each connection, i.e. the
//same OnRegistered () handler cannot be used for different connection
//instances.
try
{
//Calling Connect() will cause the Connection object to open a
//socket to the IRC server and to spawn its own thread. In this
//separate thread it will listen for messages and send them to the
//Listener for processing.
connection.Connect();
Console.WriteLine("Connected.");
this.Invoke(new Action(() => this.chatMessage.Text += "[SYSTEM] CONNECTED to " + server + ":6667 #" + channelBox.Text + " with " + nick +"\n"));
this.Invoke(new Action(() => this.chart1.Series["User(s) in chat"].Points.AddXY(DateTime.Now.ToString("H:mm"), 1)));
this.Invoke(new Action(() => {
this.connectButton.Enabled = false;
this.infoPanel.Enabled = false;
this.topPanel.Enabled = true;
this.chatText.Enabled = true;
this.chatButton.Enabled = true;
EnableTab(this.giveawayPage, true);
EnableTab(this.bettingPage, true);
EnableTab(this.challengePage, true);
EnableTab(this.graphPage, true);
EnableTab(this.databasePage, true);
currentGiveawayBox.Enabled = false;
currentBet.Enabled = false;
timer5min = new System.Timers.Timer(1000);
timer5min.Elapsed += new ElapsedEventHandler(timerTick);
timer5min.Enabled = true; // Enable it
nextSync = UnixTimeNow() + (60 * 5); // sync every 5 min
nextMin = UnixTimeNow() + 60;
}));
//The main thread ends here but the Connection's thread is still alive.
//We are now in a passive mode waiting for events to arrive.
connection.Sender.PublicMessage("#" + channelBox.Text, "HugBotty.com is in da house!");
}
catch (Exception e)
{
Console.WriteLine("Error during connection process.");
this.Invoke(new Action(() => this.chatMessage.Text += "[SYSTEM] Error during connection process.\n"));
Console.WriteLine(e);
Identd.Stop();
}
}
示例7: SendMessage
/// <summary>
/// Sends a message. Looks for the necessary connection. If none exists, a new one will be built
/// </summary>
/// <param name="msg"></param>
public static void SendMessage(ChannelMessage msg)
{
Connection conn = (Connection) ConnByReceiver[msg.To];
if (conn == null)
{
String host;
int port;
msg.SplitReceiver(out host, out port);
if (host == null)
{
// message is directed at a GUID but no connection found
// TODO: wait some seconds and retry. Maybe the client reconnects.
Console.WriteLine("Message is directed at a GUID but no connection found");
throw new Exception("Message is directed at a GUID but no connection found");
}
conn = new Connection(host,port);
conn.Connect();
ConnByReceiver[msg.To] = conn;
}
conn.SendMessage(msg);
}