LaVOZs

The World’s Largest Online Community for Developers

'; c# - Sending to a copied Socket - LavOzs.Com

Consider the following:

using System.Net;
using System.Net.Sockets;

// ...

private static void AcceptCallback(IAsyncResult AR) {

    Socket socket;
    try {
        socket = serverSocket.EndAccept(AR);
    } catch (ObjectDisposedException) {
        return;
    }

    socketManager.addSocket(socket);
    // ...

}

// ...

Where socketManager() is an object of a class I made to manage the sockets (clients).SocketManager also has the function getSocket() that basically returns a socket based on an ID. So, if I later do something like this:

byte[] data = Encoding.ASCII.GetBytes("Some text to send");
socketManager.getSocket(2).Send(data);

This will never send "Some text to send" to the client. Is this because I copy socket when I do socketManager.addSocket(socket);? How can I copy Socket objects and still be able to use them properly and send messages?

Make sure socket object is pointing to an end point by printing socket.RemoteEndPoint.ToString() If this is fine, then continue by checking socketManager.getSocket(2).RemoteEndPoint.ToString() If this doesn't return the same value, it means that you have to review your socket manager. You should pass it by reference.

Related
What is the difference between a port and a socket?
C# Socket Server Send Question
SocketException when initiating a webRequest in c#
php key() function not working inside object
Connect to socket on command line in Windows and send data
How to handle socket disconnects in Dart?
The underlying connection was closed — API endpoint call fails
Why can't I send data twice on socket connection?
python socket client does not send anything