本文整理汇总了C#中Socket.EndConnect方法的典型用法代码示例。如果您正苦于以下问题:C# Socket.EndConnect方法的具体用法?C# Socket.EndConnect怎么用?C# Socket.EndConnect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket.EndConnect方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Connected
private void Connected(IAsyncResult iar)
{
client = (Socket)iar.AsyncState;
try
{
client.EndConnect(iar);
sendAuthLogonChallenge();
client.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), client);
}
catch (SocketException)
{
Console.WriteLine("Connection failed");
// Error connecting
}
}
示例2: DualModeBeginConnect_DnsEndPointToHost_Helper
public void DualModeBeginConnect_DnsEndPointToHost_Helper(IPAddress listenOn, bool dualModeServer)
{
int port;
Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out port))
{
IAsyncResult async = socket.BeginConnect(new DnsEndPoint("localhost", port), null, null);
socket.EndConnect(async);
Assert.True(socket.Connected);
}
}
示例3: DualModeBeginConnect_IPAddressListToHost_Helper
private void DualModeBeginConnect_IPAddressListToHost_Helper(IPAddress[] connectTo, IPAddress listenOn, bool dualModeServer)
{
int port;
Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, out port))
{
IAsyncResult async = socket.BeginConnect(connectTo, port, null, null);
socket.EndConnect(async);
Assert.True(socket.Connected);
}
}
示例4: Start
//public static string SERVER = "127.0.0.1";
void Start()
{
inst = this;
Security.PrefetchSocketPolicy(SERVER,SocketPolicyServer.PORT,2000);
PlayerInfo._id = Math.Abs(((int)DateTime.Now.Ticks))%10000000 * -1;
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(SERVER);
System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, Shoot3KillServer.PORT);
socket.BeginConnect(remoteEndPoint,(IAsyncResult res)=>{
_socket = (Socket) res.AsyncState;
_socket.EndConnect(res);
bool send_ok = true;
_request_thread = new Thread(new ThreadStart(() => {
AsyncReadState state = new AsyncReadState();
while (true) {
int read = _socket.Receive(state._buffer);
if (read > 0) {
int start = 0;
int i = 0;
for (; i < read; i++) {
if (state._buffer[i] == (byte)Shoot3KillServer.MSG_TERMINATOR) {
state._msg.Append(Encoding.ASCII.GetString(state._buffer,start,i));
msg_recieved(state._msg.ToString());
state._msg.Remove(0,state._msg.Length);
start = i + 1;
send_ok = true;
}
}
state._msg.Append(Encoding.ASCII.GetString(state._buffer,start,read-start));
} else {
Debug.Log ("ERROR::request thread end, server down");
break;
}
}
}));
_request_thread.Start();
_send_thread = new Thread(new ThreadStart(() => {
while (true) {
if ((_socket == null || !_socket.Connected) || !send_ok) {
Thread.Sleep(40);
continue;
}
string msg_text = null;
bool cont = false;
lock (_msg_send_queue) {
cont = _msg_send_queue.Count > 0;
if (cont) msg_text = _msg_send_queue.Dequeue();
}
if (!cont) {
Thread.Sleep(40);
continue;
}
byte[] msg_bytes = Encoding.ASCII.GetBytes(msg_text+Shoot3KillServer.MSG_TERMINATOR);
_socket.Send(msg_bytes);
send_ok = false;
}
}));
_send_thread.Start();
},socket);
}
示例5: MyConnect
/// -----------------------------------------------------------------------------
/// <summary>
/// Callback for connection
/// </summary>
/// <param name="ar"></param>
/// <remarks>
/// </remarks>
/// <history>
/// [Caleb] 6/18/2005 Created
/// </history>
/// -----------------------------------------------------------------------------
private void MyConnect(IAsyncResult ar)
{
try
{
//RaiseEvent OnSomething("Found Connection")
m_tmpSocket = ((Socket) ar.AsyncState);
m_tmpSocket.EndConnect(ar);
if (m_tmpSocket.Connected == true)
{
Socket obj_Socket = m_tmpSocket;
DataObject obj_SocketState = new DataObject();
obj_SocketState.WorkSocket = obj_Socket;
obj_Socket.BeginReceive(obj_SocketState.Buffer, 0, obj_SocketState.BufferSize, 0, new AsyncCallback( MyDataIn), obj_SocketState);
if (OnConnectEvent != null)
OnConnectEvent("");
// SocketCheck = New Threading.Thread(AddressOf AmConnected)
//SocketCheck.Start()
obj_SocketState = null;
obj_Socket = null;
ar = null;
// GC.Collect()
}
else
{
SendLogMessage("IRC", "MyConnect", BlackLight.Services.Error.Errors.ERROR, "Recieved call back, but not connected", "", "", "");
return;
}
}
catch (Exception ex)
{
SendLogMessage("IRC", "MyConnect", BlackLight.Services.Error.Errors.ERROR, "Problem with connection callback", "", ex.Message, ex.StackTrace);
return;
}
}
示例6: DualModeBeginConnect_LoopbackDnsToHost_Helper
private void DualModeBeginConnect_LoopbackDnsToHost_Helper(IPAddress listenOn, bool dualModeServer, int port)
{
Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
using (SocketServer server = new SocketServer(_log, listenOn, dualModeServer, port))
{
IAsyncResult async = socket.BeginConnect("localhost", port, null, null);
socket.EndConnect(async);
Assert.True(socket.Connected);
}
}
示例7: Socket_BeginConnectDnsEndPoint_Failure
public void Socket_BeginConnectDnsEndPoint_Failure()
{
using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
SocketException ex = Assert.ThrowsAny<SocketException>(() =>
{
IAsyncResult result = sock.BeginConnect(new DnsEndPoint("notahostname.invalid.corp.microsoft.com", UnusedPort), null, null);
sock.EndConnect(result);
});
SocketError errorCode = ex.SocketErrorCode;
Assert.True((errorCode == SocketError.HostNotFound) || (errorCode == SocketError.NoData),
"SocketErrorCode: {0}" + errorCode);
ex = Assert.ThrowsAny<SocketException>(() =>
{
IAsyncResult result = sock.BeginConnect(new DnsEndPoint("localhost", UnusedPort), null, null);
sock.EndConnect(result);
});
Assert.Equal(SocketError.ConnectionRefused, ex.SocketErrorCode);
}
}
示例8: Socket_BeginConnectDnsEndPoint_Success
public void Socket_BeginConnectDnsEndPoint_Success(SocketImplementationType type)
{
int port;
SocketTestServer server = SocketTestServer.SocketTestServerFactory(type, IPAddress.Loopback, out port);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IAsyncResult result = sock.BeginConnect(new DnsEndPoint("localhost", port), null, null);
sock.EndConnect(result);
sock.Dispose();
server.Dispose();
}
示例9: isReachable0
public static bool isReachable0(object thisInet6AddressImpl, byte[] addr, int scope, int timeout, byte[] inf, int ttl, int if_scope)
{
if (addr.Length == 4)
{
return Java_java_net_Inet4AddressImpl.isReachable0(null, addr, timeout, inf, ttl);
}
// like the JDK, we don't use Ping, but we try a TCP connection to the echo port
// (.NET 2.0 has a System.Net.NetworkInformation.Ping class, but that doesn't provide the option of binding to a specific interface)
try
{
using (Socket sock = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
{
if (inf != null)
{
sock.Bind(new IPEndPoint(new IPAddress(inf, (uint)if_scope), 0));
}
if (ttl > 0)
{
sock.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.HopLimit, ttl);
}
IPEndPoint ep = new IPEndPoint(new IPAddress(addr, (uint)scope), 7);
IAsyncResult res = sock.BeginConnect(ep, null, null);
if (res.AsyncWaitHandle.WaitOne(timeout, false))
{
try
{
sock.EndConnect(res);
return true;
}
catch (SocketException x)
{
const int WSAECONNREFUSED = 10061;
if (x.ErrorCode == WSAECONNREFUSED)
{
// we got back an explicit "connection refused", that means the host was reachable.
return true;
}
}
}
}
}
catch (ArgumentException)
{
}
catch (SocketException)
{
}
return false;
}
示例10: Socket_BeginConnectDnsEndPoint_Failure
public void Socket_BeginConnectDnsEndPoint_Failure()
{
using (Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
// TODO: Behavior difference from .Net Desktop. This will actually throw InternalSocketException.
SocketException ex = Assert.ThrowsAny<SocketException>(() =>
{
IAsyncResult result = sock.BeginConnect(new DnsEndPoint("notahostname.invalid.corp.microsoft.com", TestPortBase + 6), null, null);
sock.EndConnect(result);
});
SocketError errorCode = ex.SocketErrorCode;
Assert.True((errorCode == SocketError.HostNotFound) || (errorCode == SocketError.NoData),
"SocketErrorCode: {0}" + errorCode);
// TODO: Behavior difference from .Net Desktop. This will actually throw InternalSocketException.
ex = Assert.ThrowsAny<SocketException>(() =>
{
IAsyncResult result = sock.BeginConnect(new DnsEndPoint("localhost", TestPortBase + 6), null, null);
sock.EndConnect(result);
});
Assert.Equal(SocketError.ConnectionRefused, ex.SocketErrorCode);
}
}
示例11: Socket_BeginConnectDnsEndPoint_Success
public void Socket_BeginConnectDnsEndPoint_Success()
{
SocketTestServer server = SocketTestServer.SocketTestServerFactory(new IPEndPoint(IPAddress.Loopback, TestPortBase + 5));
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IAsyncResult result = sock.BeginConnect(new DnsEndPoint("localhost", TestPortBase + 5), null, null);
sock.EndConnect(result);
sock.Dispose();
server.Dispose();
}