App crashes when using Socket.BeginConnect()
Post your questions regarding programming in C# in here.
9 posts
Page 1 of 1
Why does the app crash? It works fine with the ordinary Connect() method, but it blocks so I want to use BeginConnect() and EndConnect(). The async callback that i specify in BeginConnect() is never called. I dont know why. Any help? Tell me if you need the source.
The socket is a streamed TCP socket using IPv4. There is no server listening at the remote computer. Will it work if i set a socket to listen there?
The socket is a streamed TCP socket using IPv4. There is no server listening at the remote computer. Will it work if i set a socket to listen there?
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

What code are you using to call the method?
If there is no TCP server running on the remote endpoint then it will not be able to connect.
If there is no TCP server running on the remote endpoint then it will not be able to connect.
Did you try using this to show the exception?
Code: Select all
Try
Catch ex As Exception
msgbox(ex.message)
end try
Yes i tried both. I changed the code from using BeginConnect to using ThreadPool and the blocking Connect method. It works now, but when my server is in a receive operation and the client disconnects, the server crashes. Any help?
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

If the server is coded properly then it should not crash.
I don't know what the problem is, because try/catch statements wont help. The debugger shows nothing. The client connects, the server accepts and begins a read operation. The client disconnects without sending anything. Crash.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

There is no way to troubleshoot it unless you provide the code or some specific information on the method usage.
I found another way. Works fine now.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!
![Image]()
![Image]()
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;
Over 30 projects with source code!
Please give reputation to helpful members!

An example ASYNC Wrapper I use is:
Code: Select all
An example implementation would be:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace ConquerSource.Sockets
{
public class Wrapper
{
public byte[] buffer;
public Socket _socket;
public object connector;
}
public class ConquerSocket
{
private Dictionary<string, byte> Connections;
public event Action<Wrapper> AnnounceNewConnection;
public event Action<Wrapper> AnnounceDisconnection;
public event Action<byte[], Wrapper, byte[]> AnnounceReceive;
private Socket _socket;
public ConquerSocket(ushort port)
{
try
{
Connections = new Dictionary<string, byte>();
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_socket.Bind(new IPEndPoint(IPAddress.Any, port));
_socket.Listen(500);
_socket.BeginAccept(AcceptConnections, new Wrapper());
}
catch (Exception e)
{
Program.WriteLine(e);
}
}
private void AcceptConnections(IAsyncResult result)
{
try
{
Wrapper wr = result.AsyncState as Wrapper;
wr._socket = _socket.EndAccept(result);
#region Invisible
string IP = wr._socket.RemoteEndPoint.ToString().Split(':')[0].ToString();
if (!Connections.ContainsKey(IP))
Connections.Add(IP, 1);
else
if (Connections[IP] <= 12)
{
byte connections = Connections[IP];
Connections.Remove(IP);
Connections.Add(IP, (byte)(connections + 1));
}
else
{
wr._socket.Disconnect(false);
_socket.BeginAccept(AcceptConnections, new Wrapper());
return;
}
#endregion
wr.buffer = new byte[65535];
wr._socket.BeginReceive(wr.buffer, 0, 65535, SocketFlags.None, ReceiveData, wr);
AnnounceNewConnection.Invoke(wr);
_socket.BeginAccept(AcceptConnections, new Wrapper());
}
catch (Exception e)
{
Program.WriteLine(e);
}
}
private void ReceiveData(IAsyncResult result)
{
try
{
Wrapper wr = result.AsyncState as Wrapper;
string IP = wr._socket.RemoteEndPoint.ToString().Split(':')[0].ToString();
if (Connections.ContainsKey(IP))
{
SocketError error = SocketError.Disconnecting;
int size = wr._socket.EndReceive(result, out error);
if (error == SocketError.Success && size != 0)
{
byte[] buffer = new byte[size];
Buffer.BlockCopy(wr.buffer, 0, buffer, 0, size);
byte[] question = new byte[] { 1 };
AnnounceReceive.Invoke(buffer, wr, question);
wr._socket.BeginReceive(wr.buffer, 0, 65535, SocketFlags.None, ReceiveData, wr);
}
else
{
if (wr._socket.Connected)
{
wr._socket.Disconnect(true);
}
byte connections = Connections[IP];
Connections.Remove(IP);
Connections.Add(IP, (byte)(connections - 1));
try
{
AnnounceDisconnection.Invoke(wr);
}
catch { }
}
}
}
catch (Exception e)
{
Program.WriteLine(e);
}
}
}
}
Code: Select all
You can then use the wrapper.connection object to assign it to class's to carry the socket for example
ConquerSocket AuthServer = new ConquerSocket(AuthPort);
ConquerSocket GameServer = new ConquerSocket(GamePort);
ConquerSocket CtrlPanel = new ConquerSocket(5551);
AuthServer.AnnounceNewConnection += new Action<Wrapper>(Auth_NewConnection);
AuthServer.AnnounceReceive += new Action<byte[], Wrapper, byte[]>(Auth_ReceivedData);
GameServer.AnnounceNewConnection += new Action<Wrapper>(Game_NewConnection);
GameServer.AnnounceReceive += new Action<byte[], Wrapper, byte[]>(Game_ReceivedData);
GameServer.AnnounceDisconnection += new Action<Wrapper>(Game_Disconnection);
CtrlPanel.AnnounceReceive += new Action<byte[], Wrapper, byte[]>(CtrlPanel_AnnounceReceive);
CtrlPanel.AnnounceNewConnection += new Action<Wrapper>(CtrlPanel_AnnounceNewConnection);
CtrlPanel.AnnounceDisconnection += new Action<Wrapper>(CtrlPanel_AnnounceDisconnection);
Code: Select all
Hope that helps. that above is an implementation I use for a gameserver emu. static void Auth_NewConnection(Wrapper obj)
{
obj.connector = new AuthClient(obj._socket);
AuthClient client = obj.connector as AuthClient;
Auth.Packet.AuthSeed.Send(obj);
}
Bound and boom tech,
The Future Of Coding
The Future Of Coding
9 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023