|
Cet article a fait l'objet d'une traduction automatique. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. Informations supplémentaires.
|
Traduction
Source
|
Socket, classe
Espace de noms : System.Net.Sockets
Assembly : System (dans System.dll)
Le type Socket expose les membres suivants.
| Nom | Description | |
|---|---|---|
![]() | Socket(SocketInformation) | |
![]() | Socket(SocketType, ProtocolType) | |
![]() | Socket(AddressFamily, SocketType, ProtocolType) |
| Nom | Description | |
|---|---|---|
![]() | AddressFamily | |
![]() | Available | |
![]() | Blocking | |
![]() | Connected | |
![]() | DontFragment | |
![]() | DualMode | |
![]() | EnableBroadcast | |
![]() | ExclusiveAddressUse | |
![]() | Handle | |
![]() | IsBound | |
![]() | LingerState | |
![]() | LocalEndPoint | |
![]() | MulticastLoopback | |
![]() | NoDelay | |
![]() ![]() | OSSupportsIPv4 | |
![]() ![]() | OSSupportsIPv6 | |
![]() | ProtocolType | |
![]() | ReceiveBufferSize | |
![]() | ReceiveTimeout | |
![]() | RemoteEndPoint | |
![]() | SendBufferSize | |
![]() | SendTimeout | |
![]() | SocketType | |
![]() ![]() | SupportsIPv4 | Obsolète. |
![]() ![]() | SupportsIPv6 | Obsolète. |
![]() | Ttl | |
![]() | UseOnlyOverlappedIO |
| Nom | Description | |
|---|---|---|
![]() | Accept | |
![]() | AcceptAsync | |
![]() | BeginAccept(AsyncCallback, Object) | |
![]() | BeginAccept(Int32, AsyncCallback, Object) | |
![]() | BeginAccept(Socket, Int32, AsyncCallback, Object) | |
![]() | BeginConnect(EndPoint, AsyncCallback, Object) | |
![]() | BeginConnect(IPAddress, Int32, AsyncCallback, Object) | |
![]() | BeginConnect(IPAddress[], Int32, AsyncCallback, Object) | |
![]() | BeginConnect(String, Int32, AsyncCallback, Object) | |
![]() | BeginDisconnect | |
![]() | BeginReceive(IList<ArraySegment<Byte>>, SocketFlags, AsyncCallback, Object) | |
![]() | BeginReceive(IList<ArraySegment<Byte>>, SocketFlags, SocketError, AsyncCallback, Object) | |
![]() | BeginReceive(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) | |
![]() | BeginReceive(Byte[], Int32, Int32, SocketFlags, SocketError, AsyncCallback, Object) | |
![]() | BeginReceiveFrom | |
![]() | BeginReceiveMessageFrom | |
![]() | BeginSend(IList<ArraySegment<Byte>>, SocketFlags, AsyncCallback, Object) | |
![]() | BeginSend(IList<ArraySegment<Byte>>, SocketFlags, SocketError, AsyncCallback, Object) | |
![]() | BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) | |
![]() | BeginSend(Byte[], Int32, Int32, SocketFlags, SocketError, AsyncCallback, Object) | |
![]() | BeginSendFile(String, AsyncCallback, Object) | |
![]() | BeginSendFile(String, Byte[], Byte[], TransmitFileOptions, AsyncCallback, Object) | |
![]() | BeginSendTo | |
![]() | Bind | |
![]() ![]() | CancelConnectAsync | |
![]() | Close() | |
![]() | Close(Int32) | |
![]() | Connect(EndPoint) | |
![]() | Connect(IPAddress, Int32) | |
![]() | Connect(IPAddress[], Int32) | |
![]() | Connect(String, Int32) | |
![]() | ConnectAsync(SocketAsyncEventArgs) | |
![]() ![]() | ConnectAsync(SocketType, ProtocolType, SocketAsyncEventArgs) | |
![]() | Disconnect | |
![]() | DisconnectAsync | |
![]() | Dispose() | |
![]() | Dispose(Boolean) | |
![]() | DuplicateAndClose | |
![]() | EndAccept(IAsyncResult) | |
![]() | EndAccept(Byte[], IAsyncResult) | |
![]() | EndAccept(Byte[], Int32, IAsyncResult) | |
![]() | EndConnect | |
![]() | EndDisconnect | |
![]() | EndReceive(IAsyncResult) | |
![]() | EndReceive(IAsyncResult, SocketError) | |
![]() | EndReceiveFrom | |
![]() | EndReceiveMessageFrom | |
![]() | EndSend(IAsyncResult) | |
![]() | EndSend(IAsyncResult, SocketError) | |
![]() | EndSendFile | |
![]() | EndSendTo | |
![]() | Equals(Object) | |
![]() | Finalize | |
![]() | GetHashCode | |
![]() | GetSocketOption(SocketOptionLevel, SocketOptionName) | |
![]() | GetSocketOption(SocketOptionLevel, SocketOptionName, Byte[]) | |
![]() | GetSocketOption(SocketOptionLevel, SocketOptionName, Int32) | |
![]() | GetType | |
![]() | IOControl(Int32, Byte[], Byte[]) | |
![]() | IOControl(IOControlCode, Byte[], Byte[]) | |
![]() | Listen | |
![]() | MemberwiseClone | |
![]() | Poll | |
![]() | Receive(IList<ArraySegment<Byte>>) | |
![]() | Receive(Byte[]) | |
![]() | Receive(IList<ArraySegment<Byte>>, SocketFlags) | |
![]() | Receive(Byte[], SocketFlags) | |
![]() | Receive(IList<ArraySegment<Byte>>, SocketFlags, SocketError) | |
![]() | Receive(Byte[], Int32, SocketFlags) | |
![]() | Receive(Byte[], Int32, Int32, SocketFlags) | |
![]() | Receive(Byte[], Int32, Int32, SocketFlags, SocketError) | |
![]() | ReceiveAsync | |
![]() | ReceiveFrom(Byte[], EndPoint) | |
![]() | ReceiveFrom(Byte[], SocketFlags, EndPoint) | |
![]() | ReceiveFrom(Byte[], Int32, SocketFlags, EndPoint) | |
![]() | ReceiveFrom(Byte[], Int32, Int32, SocketFlags, EndPoint) | |
![]() | ReceiveFromAsync | |
![]() | ReceiveMessageFrom | |
![]() | ReceiveMessageFromAsync | |
![]() ![]() | Select | |
![]() | Send(IList<ArraySegment<Byte>>) | |
![]() | Send(Byte[]) | |
![]() | Send(IList<ArraySegment<Byte>>, SocketFlags) | |
![]() | Send(Byte[], SocketFlags) | |
![]() | Send(IList<ArraySegment<Byte>>, SocketFlags, SocketError) | |
![]() | Send(Byte[], Int32, SocketFlags) | |
![]() | Send(Byte[], Int32, Int32, SocketFlags) | |
![]() | Send(Byte[], Int32, Int32, SocketFlags, SocketError) | |
![]() | SendAsync | |
![]() | SendFile(String) | |
![]() | SendFile(String, Byte[], Byte[], TransmitFileOptions) | |
![]() | SendPacketsAsync | |
![]() | SendTo(Byte[], EndPoint) | |
![]() | SendTo(Byte[], SocketFlags, EndPoint) | |
![]() | SendTo(Byte[], Int32, SocketFlags, EndPoint) | |
![]() | SendTo(Byte[], Int32, Int32, SocketFlags, EndPoint) | |
![]() | SendToAsync | |
![]() | SetIPProtectionLevel | |
![]() | SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean) | |
![]() | SetSocketOption(SocketOptionLevel, SocketOptionName, Byte[]) | |
![]() | SetSocketOption(SocketOptionLevel, SocketOptionName, Int32) | |
![]() | SetSocketOption(SocketOptionLevel, SocketOptionName, Object) | |
![]() | Shutdown | |
![]() | ToString |
Si vous utilisez un protocole orienté connexion tel que TCP, votre serveur peut écouter les connexions à l'aide de la méthode Listen. La méthode Accept traite toutes les demandes de connexion entrantes et retourne un Socket que vous pouvez utiliser pour la communication de données avec l'hôte distant. Utilisez ce Socket retourné pour appeler la méthode Send ou Receive. Appelez la méthode Bind avant d'appeler la méthode Listen si vous voulez spécifier l'adresse IP locale et le numéro de port. Utilisez le numéro de port zéro si vous souhaitez que le fournisseur de services sous-jacent vous assigne un port libre. Si vous voulez vous connecter à un hôte à l'écoute, appelez la méthode Connect. Pour communiquer des données, appelez la méthode Send ou Receive. Si vous utilisez un protocole sans connexion, tel que UDP, vous n'avez pas du tout besoin d'écouter les connexions. Appelez la méthode ReceiveFrom pour accepter tous les datagrammes entrants. Utilisez la méthode SendTo pour envoyer des datagrammes à un hôte distant.
Si vous utilisez un protocole orienté connexion tel que TCP, utilisez Socket, la méthode BeginConnect et la méthode EndConnect pour vous connecter à un hôte à l'écoute. Utilisez les méthodes BeginSend et EndSend ou BeginReceive et EndReceive pour communiquer des données de manière asynchrone. Les demandes de connexion entrantes peuvent être traitées à l'aide des méthodes BeginAccept et EndAccept. Si vous utilisez un protocole sans connexion tel que UDP, vous pouvez utiliser les méthodes BeginSendTo et EndSendTo pour envoyer des datagrammes, ainsi que les méthodes BeginReceiveFrom et EndReceiveFrom pour recevoir des datagrammes.
Remarque |
|---|
using System; using System.Text; using System.IO; using System.Net; using System.Net.Sockets; public class GetSocket { private static Socket ConnectSocket(string server, int port) { Socket s = null; IPHostEntry hostEntry = null; // Get host related information. hostEntry = Dns.GetHostEntry(server); // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid // an exception that occurs when the host IP Address is not compatible with the address family // (typical in the IPv6 case). foreach(IPAddress address in hostEntry.AddressList) { IPEndPoint ipe = new IPEndPoint(address, port); Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp); tempSocket.Connect(ipe); if(tempSocket.Connected) { s = tempSocket; break; } else { continue; } } return s; } // This method requests the home page content for the specified server. private static string SocketSendReceive(string server, int port) { string request = "GET / HTTP/1.1\r\nHost: " + server + "\r\nConnection: Close\r\n\r\n"; Byte[] bytesSent = Encoding.ASCII.GetBytes(request); Byte[] bytesReceived = new Byte[256]; // Create a socket connection with the specified server and port. Socket s = ConnectSocket(server, port); if (s == null) return ("Connection failed"); // Send request to the server. s.Send(bytesSent, bytesSent.Length, 0); // Receive the server home page content. int bytes = 0; string page = "Default HTML page on " + server + ":\r\n"; // The following will block until te page is transmitted. do { bytes = s.Receive(bytesReceived, bytesReceived.Length, 0); page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes); } while (bytes > 0); return page; } public static void Main(string[] args) { string host; int port = 80; if (args.Length == 0) // If no server name is passed as argument to this program, // use the current host name as the default. host = Dns.GetHostName(); else host = args[0]; string result = SocketSendReceive(host, port); Console.WriteLine(result); } }
- SocketPermission
Établir une connexion sortante ou accepter une demande entrante.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (rôle principal du serveur non pris en charge), Windows Server 2008 R2 (rôle principal du serveur pris en charge avec SP1 ou version ultérieure ; Itanium non pris en charge)
Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.
