socket
「socket」とは・「socket」の意味
「socket」は、日本語で「ソケット」あるいは「差込口」と訳されることが多い。電気製品におけるプラグを差し込む部分や、コンピューターのネットワーク通信におけるエンドポイントを指すことが多い。また、工具においては、ボルトやナットを回すための工具の一部を指すこともある。「socket」の発音・読み方
「socket」の発音は、IPA表記では /ˈsɒkɪt/ となる。IPAのカタカナ読みでは「サキット」となる。日本人が発音するカタカナ英語では「ソケット」と読む。発音によって意味や品詞が変わる単語ではない。「socket」の定義を英語で解説
A socket is a device or point in a system where an electrical device can be plugged in or a tool can be inserted. In the context of computer networking, a socket is an endpoint in a communication.「socket」の類語
「socket」の類語としては、「outlet」や「receptacle」がある。これらも電気製品のプラグを差し込む部分を指す言葉である。ただし、「outlet」は主にアメリカ英語で、「receptacle」は少し古風な表現である。「socket」に関連する用語・表現
「socket」に関連する用語としては、「plug」がある。これは「socket」に差し込む部分を指す。また、コンピューターのネットワーク通信における「socket」に関連しては、「IP address」や「port number」がある。これらは「socket」を特定するための情報である。「socket」の例文
以下に「socket」を用いた例文を10個示す。 1. English: The lamp is plugged into the socket.日本語訳: ランプはソケットに差し込まれている。 2. English: The socket wrench is used to tighten the bolts.
日本語訳: ソケットレンチはボルトを締めるために使われる。 3. English: The socket is designed for a two-pin plug.
日本語訳: そのソケットは2ピンのプラグ用に設計されている。 4. English: The server creates a socket and listens for incoming connections.
日本語訳: サーバーはソケットを作成し、着信接続を待ち受ける。 5. English: The socket is located behind the desk.
日本語訳: ソケットはデスクの後ろに位置している。 6. English: The socket is not working properly.
日本語訳: ソケットが正常に動作していない。 7. English: The socket provides a connection to the electrical grid.
日本語訳: ソケットは電力網への接続を提供する。 8. English: The socket is used to connect the device to the power supply.
日本語訳: ソケットはデバイスを電源に接続するために使用される。 9. English: The socket accepts a three-pronged plug.
日本語訳: そのソケットは3本ピンのプラグを受け入れる。 10. English: The socket is part of the computer's hardware.
日本語訳: ソケットはコンピュータのハードウェアの一部である。
ソケット【socket】
Socket クラス
アセンブリ: System (system.dll 内)

Public Class Socket Implements IDisposable
public class Socket : IDisposable
public ref class Socket : IDisposable
public class Socket implements IDisposable
public class Socket implements IDisposable

Socket クラスには、ネットワーク通信のためのメソッドとプロパティが豊富に用意されています。Socket クラスを使用すると、ProtocolType 列挙体で示されている各種の通信プロトコルを使い、同期または非同期でデータを転送できます。Socket クラスは、.NET Framework での非同期メソッドの名前付け規則に従っています。たとえば、同期の Receive メソッドは非同期の BeginReceive メソッドと EndReceive メソッドに対応します。
実行中に 1 つのスレッドしか必要ない場合に使用するメソッドを次に示します。これらのメソッドは同期操作モードでの使用を想定しています。
-
TCP などのコネクション指向のプロトコルを使用する場合、サーバーは Listen メソッドを使用して接続を待機します。Accept メソッドは受信接続要求を処理して、リモート ホストとのデータ通信に使用できる Socket を返します。この返された Socket を使用して、Send メソッドまたは Receive メソッドを呼び出します。ローカル IP アドレスとポート番号を指定する場合は、Listen メソッドの前に Bind メソッドを呼び出します。基になるサービス プロバイダによって空きポートが割り当てられるようにする場合は、ポート番号に 0 を使用します。待機中のホストに接続する必要がある場合は、Connect メソッドを呼び出します。データを送受信するには、Send メソッドまたは Receive メソッドを呼び出します。
-
UDP などのコネクションレスのプロトコルを使用している場合は、接続を待機する必要はありません。ReceiveFrom メソッドを呼び出して、受信データグラムを受け取ります。データグラムをリモート ホストに送信するには、SendTo メソッドを使用します。
実行中に個別のスレッドを使用して通信を処理する場合に使用するメソッドを次に示します。これらのメソッドは非同期操作モードでの使用を想定しています。
-
TCP などのコネクション指向のプロトコルを使用する場合は、Socket メソッド、BeginConnect メソッド、および EndConnect メソッドを使用して待機中のホストと通信します。データの非同期的な送受信には、BeginSend メソッドと EndSend メソッドまたは BeginReceive メソッドと EndReceive メソッドを使用します。BeginAccept および EndAccept を使用すると、受信接続要求を処理できます。
-
UDP などのコネクションレスのプロトコルを使用している場合は、BeginSendTo および EndSendTo を使用してデータグラムを送信します。データグラムの受信には、BeginReceiveFrom と EndReceiveFrom を使用します。
ソケット上で複数の非同期動作を実行する場合、各動作の実行は開始された順に完了するとは限りません。
データの送受信が完了したら、Shutdown メソッドを使用して Socket を無効にします。Shutdown を呼び出してから Close メソッドを呼び出して、Socket に関連付けられているすべてのリソースを解放します。
Socket クラスを使用すると、SetSocketOption メソッドを使用して Socket を設定できます。これらの設定を取得するには、GetSocketOption メソッドを使用します。
![]() |
---|
比較的単純なアプリケーションを記述しており、最高のパフォーマンスを必要としない場合は、TcpClient、TcpListener、および UdpClient を使用することを検討してください。これらのクラスには、Socket 通信を行うためのより単純でわかりやすいインターフェイスが用意されています。 |
Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows CE プラットフォームメモ : すべてのデバイスのオペレーティング システムで、すべてのソケット オプションがサポートされているわけではありません。

Socket クラスを使用して、データを HTTP サーバーに送信し、応答を受信する方法を次のコード例に示します。この例は、すべてのページを受信するまでブロックします。
Imports System Imports System.Text Imports System.IO Imports System.Net Imports System.Net.Sockets Imports Microsoft.VisualBasic Public Class GetSocket Private Shared Function ConnectSocket(server As String, port As Integer) As Socket Dim s As Socket = Nothing Dim hostEntry As IPHostEntry = Nothing ' 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 host IP Address is not compatible with the address family ' (typical in the IPv6 case). Dim address As IPAddress For Each address In hostEntry.AddressList Dim endPoint As New IPEndPoint(address, port) Dim tempSocket As New Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp) tempSocket.Connect(endPoint) If tempSocket.Connected Then s = tempSocket Exit For End If Next address Return s End Function ' This method requests the home page content for the specified server. Private Shared Function SocketSendReceive(server As String, port As Integer) As String 'Set up variables and String to write to the server. Dim ascii As Encoding = Encoding.ASCII Dim request As String = "GET / HTTP/1.1" + ControlChars.Cr + ControlChars.Lf + "Host: " + server + ControlChars.Cr + ControlChars.Lf + "Connection: Close" + ControlChars.Cr + ControlChars.Lf + ControlChars.Cr + ControlChars.Lf Dim bytesSent As [Byte]() = ascii.GetBytes(request) Dim bytesReceived(255) As [Byte] ' Create a socket connection with the specified server and port. Dim s As Socket = ConnectSocket(server, port) If s Is Nothing Then Return "Connection failed" End If ' Send request to the server. s.Send(bytesSent, bytesSent.Length, 0) ' Receive the server home page content. Dim bytes As Int32 ' Read the first 256 bytes. Dim page as [String] = "Default HTML page on " + server + ":" + ControlChars.Cr + ControlChars.Lf ' The following will block until the page is transmitted. Do bytes = s.Receive(bytesReceived, bytesReceived.Length, 0) page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes) Loop While bytes > 0 Return page End Function 'Entry point which delegates to C-style main Private Function Public Overloads Shared Sub Main() Main(System.Environment.GetCommandLineArgs()) End Sub Overloads Private Shared Sub Main(args() As String) Dim host As String Dim port As Integer = 80 If args.Length = 1 Then ' If no server name is passed as argument to this program, ' use the current host name as default. host = Dns.GetHostName() Else host = args(1) End If Dim result As String = SocketSendReceive(host, port) Console.WriteLine(result) End Sub 'Main End Class
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); } }
#using <System.dll> using namespace System; using namespace System::Text; using namespace System::IO; using namespace System::Net; using namespace System::Net::Sockets; using namespace System::Collections; Socket^ ConnectSocket( String^ server, int port ) { Socket^ s = nullptr; IPHostEntry^ hostEntry = nullptr; // Get host related information. hostEntry = Dns::Resolve( 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). IEnumerator^ myEnum = hostEntry->AddressList->GetEnumerator(); while ( myEnum->MoveNext() ) { IPAddress^ address = safe_cast<IPAddress^>(myEnum->Current); IPEndPoint^ endPoint = gcnew IPEndPoint( address,port ); Socket^ tmpS = gcnew Socket( endPoint->AddressFamily,SocketType::Stream,ProtocolType::Tcp ); tmpS->Connect( endPoint ); if ( tmpS->Connected ) { s = tmpS; break; } else { continue; } } return s; } // This method requests the home page content for the specified server. String^ SocketSendReceive( String^ server, int port ) { String^ request = String::Concat( "GET / HTTP/1.1\r\nHost: ", server, "\r\nConnection: Close\r\n\r\n" ); array<Byte>^bytesSent = Encoding::ASCII->GetBytes( request ); array<Byte>^bytesReceived = gcnew array<Byte>(256); // Create a socket connection with the specified server and port. Socket^ s = ConnectSocket( server, port ); if ( s == nullptr ) return ("Connection failed"); // Send request to the server. s->Send( bytesSent, bytesSent->Length, static_cast<SocketFlags>(0) ); // Receive the server home page content. int bytes = 0; String^ strRetPage = String::Concat( "Default HTML page on ", server, ":\r\n" ); do { bytes = s->Receive( bytesReceived, bytesReceived->Length, static_cast<SocketFlags>(0) ); strRetPage = String::Concat( strRetPage, Encoding::ASCII->GetString( bytesReceived, 0, bytes ) ); } while ( bytes > 0 ); return strRetPage; } int main() { array<String^>^args = Environment::GetCommandLineArgs(); String^ host; int port = 80; if ( args->Length == 1 ) // If no server name is passed as argument to this program, // use the current host name as default. host = Dns::GetHostName(); else host = args[ 1 ]; String^ result = SocketSendReceive( host, port ); Console::WriteLine( result ); }
import System.*; import System.Text.*; import System.IO.*; import System.Net.*; import 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.Resolve(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). for (int iCtr = 0; iCtr < hostEntry.get_AddressList().length; iCtr++) { IPAddress address = hostEntry.get_AddressList()[iCtr]; IPEndPoint ipe = new IPEndPoint(address, port); Socket tempSocket = new Socket(ipe.get_AddressFamily() , SocketType.Stream, ProtocolType.Tcp); tempSocket.Connect(ipe); if (tempSocket.get_Connected()) { s = tempSocket; break; } else { continue; } } return s; } //ConnectSocket // 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"; System.Byte bytesSent[] = (System.Byte[])Encoding.get_ASCII().GetBytes(request); System.Byte bytesReceived[] = new System.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((ubyte[])bytesSent, bytesSent.length, (SocketFlags)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((ubyte[])bytesReceived, bytesReceived.length, (SocketFlags)0); page = page + Encoding.get_ASCII().GetString( (ubyte[])bytesReceived, 0, bytes); } while (bytes > 0); return page; } //SocketSendReceive 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); } //main } //GetSocket


System.Net.Sockets.Socket


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Socket コンストラクタ (AddressFamily, SocketType, ProtocolType)
アセンブリ: System (system.dll 内)

Public Sub New ( _ addressFamily As AddressFamily, _ socketType As SocketType, _ protocolType As ProtocolType _ )
Dim addressFamily As AddressFamily Dim socketType As SocketType Dim protocolType As ProtocolType Dim instance As New Socket(addressFamily, socketType, protocolType)
public function Socket ( addressFamily : AddressFamily, socketType : SocketType, protocolType : ProtocolType )
- addressFamily
AddressFamily 値の 1 つ。
- socketType
SocketType 値の 1 つ。
- protocolType
ProtocolType 値の 1 つ。


addressFamily パラメータは Socket クラスが使用するアドレッシング スキームを指定し、socketType パラメータは Socket クラスの型を指定します。また、protocolType パラメータは Socket が使用するプロトコルを指定します。これらの 3 つのパラメータは独立していません。一部のアドレス ファミリは使用できるプロトコルを制限しており、多くの場合、プロトコルでは Socket 型が暗黙的です。アドレス ファミリ、Socket の型、およびプロトコルの種類の組み合わせが無効な Socket になった場合、このコンストラクタは SocketException をスローします。
![]() |
---|
このコンストラクタが SocketException をスローした場合は、SocketException.ErrorCode プロパティを使用して具体的なエラー コードを取得してください。このコードを取得したら、Windows Socket Version 2 API エラー コードのドキュメントでエラーの詳細情報を確認してください。これは MSDN ライブラリから入手できます。 |

Socket クラスのインスタンスを作成する方法を次のコード例に示します。
Imports System Imports System.Text Imports System.IO Imports System.Net Imports System.Net.Sockets _ Public Class Sample Public Shared Function DoSocketGet(server As String) As String 'Set up variables and String to write to the server. Dim ASCII As Encoding = Encoding.ASCII Dim [Get] As String = "GET / HTTP/1.1" + ControlChars.Lf + ControlChars.NewLine + "Host: " + server + ControlChars.Lf + ControlChars.NewLine + "Connection: Close" + ControlChars.Lf + ControlChars.NewLine + ControlChars.Lf + ControlChars.NewLine Dim ByteGet As [Byte]() = ASCII.GetBytes([Get]) Dim RecvBytes(256) As [Byte] Dim strRetPage As [String] = Nothing ' IPAddress and IPEndPoint represent the endpoint that will ' receive the request. ' Get first IPAddress in list return by DNS. Try ' Define those variables to be evaluated in the next for loop and ' then used to connect to the server. These variables are defined ' outside the for loop to make them accessible there after. Dim s As Socket = Nothing Dim hostEndPoint As IPEndPoint Dim hostAddress As IPAddress = Nothing Dim conPort As Integer = 80 ' Get DNS host information. Dim hostInfo As IPHostEntry = Dns.Resolve(server) ' Get the DNS IP addresses associated with the host. Dim IPaddresses As IPAddress() = hostInfo.AddressList ' Evaluate the socket and receiving host IPAddress and IPEndPoint. Dim index As Integer = 0 For index = 0 To IPaddresses.Length - 1 hostAddress = IPaddresses(index) hostEndPoint = New IPEndPoint(hostAddress, conPort) ' Creates the Socket to send data over a TCP connection. s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) ' Connect to the host using its IPEndPoint. s.Connect(hostEndPoint) If Not s.Connected Then ' Connection failed, try next IPaddress. strRetPage = "Unable to connect to host" s = Nothing GoTo ContinueFor1 End If ' Sent the GET request to the host. s.Send(ByteGet, ByteGet.Length, 0) ContinueFor1: Next index ' End of the for loop. ' Receive the host home page content and loop until all the data is received. 'Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0) Dim bytes As Int32 = s.Receive(RecvBytes, RecvBytes.Length, 0) strRetPage = "Default HTML page on " + server + ":\r\n" strRetPage = "Default HTML page on " + server + ":" + ControlChars.Lf + ControlChars.NewLine Dim i As Integer While bytes > 0 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0) strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes) End While ' End of the try block. Catch e As SocketException Console.WriteLine("SocketException caught!!!") Console.WriteLine(("Source : " + e.Source)) Console.WriteLine(("Message : " + e.Message)) Catch e As ArgumentNullException Console.WriteLine("ArgumentNullException caught!!!") Console.WriteLine(("Source : " + e.Source)) Console.WriteLine(("Message : " + e.Message)) Catch e As NullReferenceException Console.WriteLine("NullReferenceException caught!!!") Console.WriteLine(("Source : " + e.Source)) Console.WriteLine(("Message : " + e.Message)) Catch e As Exception Console.WriteLine("Exception caught!!!") Console.WriteLine(("Source : " + e.Source)) Console.WriteLine(("Message : " + e.Message)) End Try Return strRetPage End Function 'DoSocketGet Public Shared Sub Main() Console.WriteLine(DoSocketGet("localhost")) End Sub 'Main End Class 'Sample
using System; using System.Text; using System.IO; using System.Net; using System.Net.Sockets; public class Sample { public static string DoSocketGet(string server) { //Set up variables and String to write to the server. Encoding ASCII = Encoding.ASCII; string Get = "GET / HTTP/1.1\r\nHost: " + server + "\r\nConnection: Close\r\n\r\n"; Byte[] ByteGet = ASCII.GetBytes(Get); Byte[] RecvBytes = new Byte[256]; String strRetPage = null; // IPAddress and IPEndPoint represent the endpoint that will // receive the request. // Get first IPAddress in list return by DNS. try { // Define those variables to be evaluated in the next for loop and // then used to connect to the server. These variables are defined // outside the for loop to make them accessible there after. Socket s = null; IPEndPoint hostEndPoint; IPAddress hostAddress = null; int conPort = 80; // Get DNS host information. IPHostEntry hostInfo = Dns.GetHostEntry(server); // Get the DNS IP addresses associated with the host. IPAddress[] IPaddresses = hostInfo.AddressList; // Evaluate the socket and receiving host IPAddress and IPEndPoint. for (int index=0; index<IPaddresses.Length; index++) { hostAddress = IPaddresses[index]; hostEndPoint = new IPEndPoint(hostAddress, conPort); // Creates the Socket to send data over a TCP connection. s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp ); // Connect to the host using its IPEndPoint. s.Connect(hostEndPoint); if (!s.Connected) { // Connection failed, try next IPaddress. strRetPage = "Unable to connect to host"; s = null; continue; } // Sent the GET request to the host. s.Send(ByteGet, ByteGet.Length, 0); } // End of the for loop. // Receive the host home page content and loop until all the data is received. Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0); strRetPage = "Default HTML page on " + server + ":\r\n"; strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes); while (bytes > 0) { bytes = s.Receive(RecvBytes, RecvBytes.Length, 0); strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes); } } // End of the try block. catch(SocketException e) { Console.WriteLine("SocketException caught!!!"); Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } catch(ArgumentNullException e) { Console.WriteLine("ArgumentNullException caught!!!"); Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } catch(NullReferenceException e) { Console.WriteLine("NullReferenceException caught!!!"); Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } catch(Exception e) { Console.WriteLine("Exception caught!!!"); Console.WriteLine("Source : " + e.Source); Console.WriteLine("Message : " + e.Message); } return strRetPage; } public static void Main() { Console.WriteLine(DoSocketGet("localhost")); } }
#using <System.dll> using namespace System; using namespace System::Text; using namespace System::IO; using namespace System::Net; using namespace System::Net::Sockets; String^ DoSocketGet( String^ server ) { //Set up variables and String to write to the server. Encoding^ ASCII = Encoding::ASCII; String^ Get = "GET / HTTP/1.1\r\nHost: "; Get->Concat( server, "\r\nConnection: Close\r\n\r\n" ); array<Byte>^ByteGet = ASCII->GetBytes( Get ); array<Byte>^RecvBytes = gcnew array<Byte>(256); String^ strRetPage = nullptr; // IPAddress and IPEndPoint represent the endpoint that will // receive the request. // Get first IPAddress in list return by DNS. try { // Define those variables to be evaluated in the next for loop and // then used to connect to the server. These variables are defined // outside the for loop to make them accessible there after. Socket^ s = nullptr; IPEndPoint^ hostEndPoint; IPAddress^ hostAddress = nullptr; int conPort = 80; // Get DNS host information. IPHostEntry^ hostInfo = Dns::Resolve( server ); // Get the DNS IP addresses associated with the host. array<IPAddress^>^IPaddresses = hostInfo->AddressList; // Evaluate the socket and receiving host IPAddress and IPEndPoint. for ( int index = 0; index < IPaddresses->Length; index++ ) { hostAddress = IPaddresses[ index ]; hostEndPoint = gcnew IPEndPoint( hostAddress,conPort ); // Creates the Socket to send data over a TCP connection. s = gcnew Socket( AddressFamily::InterNetwork,SocketType::Stream,ProtocolType::Tcp ); // Connect to the host using its IPEndPoint. s->Connect( hostEndPoint ); if ( !s->Connected ) { // Connection failed, try next IPaddress. strRetPage = "Unable to connect to host"; s = nullptr; continue; } // Sent the GET request to the host. s->Send( ByteGet, ByteGet->Length, SocketFlags::None ); } // Receive the host home page content and loop until all the data is received. Int32 bytes = s->Receive( RecvBytes, RecvBytes->Length, SocketFlags::None ); strRetPage = "Default HTML page on "; strRetPage->Concat( server, ":\r\n", ASCII->GetString( RecvBytes, 0, bytes ) ); while ( bytes > 0 ) { bytes = s->Receive( RecvBytes, RecvBytes->Length, SocketFlags::None ); strRetPage->Concat( ASCII->GetString( RecvBytes, 0, bytes ) ); } } catch ( SocketException^ e ) { Console::WriteLine( "SocketException caught!!!" ); Console::Write( "Source : " ); Console::WriteLine( e->Source ); Console::Write( "Message : " ); Console::WriteLine( e->Message ); } catch ( ArgumentNullException^ e ) { Console::WriteLine( "ArgumentNULLException caught!!!" ); Console::Write( "Source : " ); Console::WriteLine( e->Source ); Console::Write( "Message : " ); Console::WriteLine( e->Message ); } catch ( NullReferenceException^ e ) { Console::WriteLine( "NULLReferenceException caught!!!" ); Console::Write( "Source : " ); Console::WriteLine( e->Source ); Console::Write( "Message : " ); Console::WriteLine( e->Message ); } catch ( Exception^ e ) { Console::WriteLine( "Exception caught!!!" ); Console::Write( "Source : " ); Console::WriteLine( e->Source ); Console::Write( "Message : " ); Console::WriteLine( e->Message ); } return strRetPage; } int main() { Console::WriteLine( DoSocketGet( "localhost" ) ); }
import System.*; import System.Text.*; import System.IO.*; import System.Net.*; import System.Net.Sockets.*; public class Sample { public static String DoSocketGet(String server) { //Set up variables and String to write to the server. Encoding ascii = Encoding.get_ASCII(); String get = "GET / HTTP/1.1\r\nHost: " + server + "\r\nConnection: Close\r\n\r\n"; System.Byte byteGet[] = (System.Byte[])ascii.GetBytes(get); System.Byte recvBytes[] = new System.Byte[256]; String strRetPage = null; // IPAddress and IPEndPoint represent the endpoint that will // receive the request. // Get first IPAddress in list return by DNS. try { // Define those variables to be evaluated in the next for loop and // then used to connect to the server. These variables are defined // outside the for loop to make them accessible there after. Socket s = null; IPEndPoint hostEndPoint; IPAddress hostAddress = null; int conPort = 80; // Get DNS host information. IPHostEntry hostInfo = Dns.Resolve(server); // Get the DNS IP addresses associated with the host. IPAddress IPaddresses[] = hostInfo.get_AddressList(); // Evaluate the socket and receiving host IPAddress and IPEndPoint. for (int index = 0; index < IPaddresses.length; index++) { hostAddress = IPaddresses[index]; hostEndPoint = new IPEndPoint(hostAddress, conPort); // Creates the Socket to send data over a TCP connection. s = new Socket(AddressFamily.InterNetwork, SocketType.Stream , ProtocolType.Tcp); // Connect to the host using its IPEndPoint. s.Connect(hostEndPoint); if (!(s.get_Connected())) { // Connection failed, try next IPaddress. strRetPage = "Unable to connect to host"; s = null; continue; } // Sent the GET request to the host. s.Send((ubyte[])byteGet, byteGet.get_Length(), (SocketFlags)0); } // End of the for loop. // Receive the host home page content and loop until all the // data is received. Int32 bytes = (Int32)s.Receive((ubyte[])recvBytes, recvBytes.get_Length(), (SocketFlags)0); strRetPage = "Default HTML page on " + server + ":\r\n"; strRetPage = strRetPage + ascii.GetString((ubyte[])recvBytes, 0, Convert.ToInt32(bytes)); while (Convert.ToInt32(bytes) > 0) { bytes = (Int32)s.Receive((ubyte[])recvBytes, recvBytes.get_Length(), (SocketFlags)0); strRetPage = strRetPage + ascii.GetString((ubyte[])recvBytes, 0, Convert.ToInt32(bytes)); } } // End of the try block. catch (SocketException e) { Console.WriteLine("SocketException caught!!!"); Console.WriteLine("Source : " + e.get_Source()); Console.WriteLine("Message : " + e.get_Message()); } catch (ArgumentNullException e) { Console.WriteLine("ArgumentNullException caught!!!"); Console.WriteLine("Source : " + e.get_Source()); Console.WriteLine("Message : " + e.get_Message()); } catch (NullReferenceException e) { Console.WriteLine("NullReferenceException caught!!!"); Console.WriteLine("Source : " + e.get_Source()); Console.WriteLine("Message : " + e.get_Message()); } catch (System.Exception e) { Console.WriteLine("Exception caught!!!"); Console.WriteLine("Source : " + e.get_Source()); Console.WriteLine("Message : " + e.get_Message()); } return strRetPage; } //DoSocketGet public static void main(String[] args) { Console.WriteLine(DoSocketGet("localhost")); } //main } //Sample

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Socket コンストラクタ (SocketInformation)
アセンブリ: System (system.dll 内)


Socket コンストラクタを複数回呼び出し、毎回同じバイト配列を引数として渡すと、基になるソケットが同じである複数のマネージ Socket が作成されます。このような処理は、できる限り避けてください。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Socket コンストラクタ

名前 | 説明 |
---|---|
Socket (SocketInformation) | DuplicateAndClose から返された値を指定して、Socket クラスの新しいインスタンスを初期化します。 |
Socket (AddressFamily, SocketType, ProtocolType) | 指定したアドレス ファミリ、ソケット タイプ、およびプロトコルを使用して、Socket クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |

Socket プロパティ

名前 | 説明 | |
---|---|---|
![]() | Ttl | Socket によって送信されたインターネット プロトコル (IP) パケットの有効期間 (TTL) の値を指定する値を取得または設定します。 |
![]() | UseOnlyOverlappedIO | ソケットが重複 I/O モードだけを使用する必要があるかどうかを指定します。 |

Socket メソッド


名前 | 説明 | |
---|---|---|
![]() | Dispose | Socket が使用しているアンマネージ リソースを解放します。オプションでマネージ リソースも破棄します。 |
![]() | Finalize | オーバーライドされます。 Socket クラスによって使用されていたリソースを解放します。 |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 ( Object から継承されます。) |


Socket メンバ
Socket データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | Ttl | Socket によって送信されたインターネット プロトコル (IP) パケットの有効期間 (TTL) の値を指定する値を取得または設定します。 |
![]() | UseOnlyOverlappedIO | ソケットが重複 I/O モードだけを使用する必要があるかどうかを指定します。 |


名前 | 説明 | |
---|---|---|
![]() | Dispose | Socket が使用しているアンマネージ リソースを解放します。オプションでマネージ リソースも破棄します。 |
![]() | Finalize | オーバーライドされます。 Socket クラスによって使用されていたリソースを解放します。 |
![]() | MemberwiseClone | 現在の Object の簡易コピーを作成します。 (Object から継承されます。) |


ソケット
(socket から転送)
出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2021/08/15 18:09 UTC 版)
ナビゲーションに移動 検索に移動ソケット (socket)
- ソケット (電気器具) - 電球や豆電球やLED電球などを取り付ける電気器具。
- CPUソケット - CPUのIC用のICソケット
- コンピュータネットワークのソケット
- ネットワークソケット
- ソケット (BSD) - BSD由来のプロセス間通信のAPI
- UNIXドメインソケット
- ソケットレンチ - ハンドルなどと組み合わせて使用される工具の一種
- ソケット (企業) - テレビ番組企画制作、広告企画制作の会社。東京都港区。代表者 櫻井雄一
- ソケット (ゴルフ) - ゴルフで右サイドへ大きく飛んでしまうミスショットの一種。逆に「ヒッカケ」は左方向へ飛んでいくミスショット。
![]() |
このページは曖昧さ回避のためのページです。一つの語句が複数の意味・職能を有する場合の水先案内のために、異なる用法を一覧にしてあります。お探しの用語に一番近い記事を選んで下さい。このページへリンクしているページを見つけたら、リンクを適切な項目に張り替えて下さい。 |
socket()
出典: フリー百科事典『ウィキペディア(Wikipedia)』 (2021/04/03 08:34 UTC 版)
「ソケット (BSD)」の記事における「socket()」の解説
socket() は通信の一方の終端を作成し、それを表すファイル記述子を返す。socket() には以下の3つの引数がある。 domain - 生成するソケットのプロトコルファミリを指定する。AF_INET - IPv4 AF_INET6 - IPv6 AF_UNIX - UNIXドメインソケット type - ソケットのタイプ指定。SOCK_STREAM - 信頼性のあるストリーム指向サービス(TCPに対応) SOCK_DGRAM - データグラムサービス(UDPに対応) SOCK_SEQPACKET - SOCK_STREAMとほぼ同じだが、受信したパケットを読み出す際にパケット全体を読み出さないと、残りを破棄する。 SOCK_RAW - IPレベルのプロトコル処理をユーザー側で用意するときに使用 protocol - 通常 0 を指定し、デフォルトの物が選ばれる。トランスポート層のプロトコルは、domain と type で指定されるが(TCPの場合、AF_INET または AF_INET6 と SOCK_STREAM、UDPの場合、同様の AF_ 値と SOCK_DGRAM)、明示的に指定することも可能で、その値は
※この「socket()」の解説は、「ソケット (BSD)」の解説の一部です。
「socket()」を含む「ソケット (BSD)」の記事については、「ソケット (BSD)」の概要を参照ください。
- socketのページへのリンク