DimChtz
DimChtz

Reputation: 4323

Sending to a copied Socket

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?

Upvotes: 0

Views: 247

Answers (1)

Indrit Kello
Indrit Kello

Reputation: 1313

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.

Upvotes: 1

Related Questions