view RewritingTest/SocketIPPortCheck.cs @ 11:1f7d4f168b89

socket test
author riono <e165729@ie.u-ryukyu.ac.jp>
date Thu, 19 Nov 2020 03:05:28 +0900
parents
children
line wrap: on
line source

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

// socketを作成してIPとportを取得する 作動にはListenerが必要
class SocketIPPortCheck {
    public void SetSocket() {
        IPHostEntry host = Dns.GetHostEntry("localhost");
        IPAddress ipAddress = host.AddressList[0];
        //IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
        IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);

        // Create a TCP/IP  socket.    
        Socket sender = new Socket(ipAddress.AddressFamily,
            SocketType.Stream, ProtocolType.Tcp);

        sender.Connect(remoteEP);

        Console.WriteLine(Dns.GetHostEntry(((IPEndPoint)sender.RemoteEndPoint).Address.ToString()).HostName + ":" +
                          ((IPEndPoint) sender.RemoteEndPoint).Port.ToString());
    }

    private static void Main(string[] args) {
        SocketIPPortCheck check = new SocketIPPortCheck();
        check.SetSocket();
    }
}

// Listenerのコード
// using System;
// using System.Net;
// using System.Net.Sockets;
// using System.Text;
//
// // Socket Listener acts as a server and listens to the incoming   
// // messages on the specified port and protocol.  
// public class SocketListener {
//     public static int Main (String[] args) {
//         StartServer ();
//         return 0;
//     }
//
//     public static void StartServer () {
//         // Get Host IP Address that is used to establish a connection  
//         // In this case, we get one IP address of localhost that is IP : 127.0.0.1  
//         // If a host has multiple addresses, you will get a list of addresses  
//         IPHostEntry host = Dns.GetHostEntry ("localhost");
//         IPAddress ipAddress = host.AddressList[0];
// 	//IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
// 	IPEndPoint localEndPoint = new IPEndPoint (ipAddress, 11000);
//
//         try {
//
//             // Create a Socket that will use Tcp protocol      
//             Socket listener = new Socket (ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
//             // A Socket must be associated with an endpoint using the Bind method  
//             listener.Bind (localEndPoint);
//             // Specify how many requests a Socket can listen before it gives Server busy response.  
//             // We will listen 10 requests at a time  
//             listener.Listen (10);
//
//             Console.WriteLine ("Waiting for a connection...");
//             Socket handler = listener.Accept ();
//
//             // Incoming data from the client.    
//             string data = null;
//             byte[] bytes = null;
//
//             while (true) {
//                 bytes = new byte[1024];
//                 int bytesRec = handler.Receive (bytes);
//                 data += Encoding.ASCII.GetString (bytes, 0, bytesRec);
//                 if (data.IndexOf ("<EOF>") > -1) {
//                     break;
//                 }
//             }
//
//             Console.WriteLine ("Text received : {0}", data);
//
//             byte[] msg = Encoding.ASCII.GetBytes (data);
//             handler.Send (msg);
//             handler.Shutdown (SocketShutdown.Both);
//             handler.Close ();
//         } catch (Exception e) {
//             Console.WriteLine (e.ToString ());
//         }
//
//         Console.WriteLine ("\n Press any key to continue...");
//         Console.ReadKey ();
//     }
//}