Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.
Traduzione
Inglese

Metodo Socket.ReceiveFrom (Byte[], Int32, Int32, SocketFlags, EndPoint)

 

Riceve il numero specificato di byte di dati nella posizione specificata del buffer di dati usando l'oggetto SocketFlags specificato e archivia l'endpoint.

Spazio dei nomi:   System.Net.Sockets
Assembly:  System (in System.dll)

public int ReceiveFrom(
	byte[] buffer,
	int offset,
	int size,
	SocketFlags socketFlags,
	ref EndPoint remoteEP
)

Parametri

buffer
Type: System.Byte[]

Matrice di tipo Byte che rappresenta la posizione di archiviazione dei dati ricevuti.

offset
Type: System.Int32

Posizione nel parametro buffer in cui archiviare i dati ricevuti.

size
Type: System.Int32

Numero di byte da ricevere.

socketFlags
Type: System.Net.Sockets.SocketFlags

Combinazione bit per bit dei valori di SocketFlags.

remoteEP
Type: System.Net.EndPoint

Oggetto EndPoint, passato per riferimento, che rappresenta il server remoto.

Valore restituito

Type: System.Int32

Numero di byte ricevuti.

Exception Condition
ArgumentNullException

buffer è null.

-oppure-

remoteEP è null.

ArgumentOutOfRangeException

offset è minore di 0.

-oppure-

offset è maggiore della lunghezza di buffer.

-oppure-

size è minore di 0.

-oppure-

size è maggiore della lunghezza di buffer meno il valore del parametro di offset.

SocketException

socketFlags non è una combinazione valida di valori.

-oppure-

La proprietà LocalEndPoint non è stata impostata.

-oppure-

Si è verificato un errore durante il tentativo di accesso al socket. Per altre informazioni, vedere la sezione Osservazioni.

ObjectDisposedException

L'oggetto Socket è stato chiuso.

The M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@) method reads data into the buffer parameter, returns the number of bytes successfully read, and captures the remote host endpoint from which the data was sent. This method is useful if you intend to receive connectionless datagrams from an unknown host or multiple hosts.

With connectionless protocols, M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@) will read the first enqueued datagram received into the local network buffer. If the datagram you receive is larger than the size of buffer, the M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@) method will fill buffer with as much of the message as is possible, and throw a T:System.Net.Sockets.SocketException. If you are using an unreliable protocol, the excess data will be lost. If you are using a reliable protocol, the excess data will be retained by the service provider and you can retrieve it by calling the M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@) method with a large enough buffer.

If no data is available for reading, the M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@) method will block until data is available. If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@) method will complete immediately and throw a T:System.Net.Sockets.SocketException. You can use the P:System.Net.Sockets.Socket.Available property to determine if data is available for reading. When P:System.Net.Sockets.Socket.Available is non-zero, retry the receive operation.

Although M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@) is intended for connectionless protocols, you can use a connection-oriented protocol as well. If you choose to do so, you must first either establish a remote host connection by calling the M:System.Net.Sockets.Socket.Connect(System.Net.EndPoint) method or accept an incoming remote host connection by calling the M:System.Net.Sockets.Socket.Accept method. If you do not establish or accept a connection before calling the M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@) method, you will get a T:System.Net.Sockets.SocketException. You can also establish a default remote host for a connectionless protocol prior to calling the M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@) method. In either of these cases, the M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@) method will ignore the remoteEP parameter and only receive data from the connected or default remote host.

With connection-oriented sockets, M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@) will read as much data as is available up to the amount of bytes specified by the size parameter. If the remote host shuts down the T:System.Net.Sockets.Socket connection with the M:System.Net.Sockets.Socket.Shutdown(System.Net.Sockets.SocketShutdown) method, and all available data has been Received, the M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@) method will complete immediately and return zero bytes.

System_CAPS_noteNota

Before calling M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@), you must explicitly bind the T:System.Net.Sockets.Socket to a local endpoint using the M:System.Net.Sockets.Socket.Bind(System.Net.EndPoint) method. If you do not, M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@) will throw a T:System.Net.Sockets.SocketException. If you receive a T:System.Net.Sockets.SocketException, use the P:System.Net.Sockets.SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation in the MSDN library for a detailed description of the error.

System_CAPS_noteNota

The T:System.Net.Sockets.AddressFamily of the T:System.Net.EndPoint used in Overload:System.Net.Sockets.Socket.ReceiveFrom needs to match theT:System.Net.Sockets.AddressFamily of the T:System.Net.EndPoint used in Overload:System.Net.Sockets.Socket.SendTo.

System_CAPS_noteNota

This member outputs trace information when you enable network tracing in your application. For more information, see [<topic://conUsingNetworkTracing>].

The following code example receives a connectionless datagram from a remote host. The offset, buffer size, and T:System.Net.Sockets.SocketFlags are passed to the M:System.Net.Sockets.Socket.ReceiveFrom(System.Byte[],System.Int32,System.Int32,System.Net.Sockets.SocketFlags,System.Net.EndPoint@) method.

public static void ReceiveFrom4()
{
    IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
    IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);

    Socket s = new Socket(endPoint.Address.AddressFamily,
        SocketType.Dgram,
        ProtocolType.Udp);

    // Creates an IpEndPoint to capture the identity of the sending host.
    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
    EndPoint senderRemote = (EndPoint)sender;

    // Binding is required with ReceiveFrom calls.
    s.Bind(endPoint);
    byte[] msg = new Byte[256];
    Console.WriteLine ("Waiting to receive datagrams from client...");
    // This call blocks.  
    s.ReceiveFrom(msg, 0, msg.Length, SocketFlags.None, ref senderRemote);
    s.Close();
}

SocketPermission

for accepting connections from the network. Associated enumeration: F:System.Net.NetworkAccess.Accept.

.NET Framework
Disponibile da 1.1
Torna all'inizio
Mostra: