百度首页 | 百度空间
 
查看文章
 
转获取客户端IP及端口号
2008-02-02 16:12
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;//为了IPEndPoint而添加的引用
using System.Net.Sockets;

namespace GetClntIP
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener tcpListener = new TcpListener(9000);//监听的端口号,可根据需要修改
            tcpListener.Start();

            //loop for listen
            while (true)
            {
                Socket sock = tcpListener.AcceptSocket();

                //服务器当前时间
                DateTime connTime = DateTime.Now;
                Console.WriteLine("The time is :"+connTime.ToString());

                System.Net.IPAddress ipAdd;
                int port;

                //获得连接当前用户的IP以及端口号
                ipAdd = (sock.RemoteEndPoint as IPEndPoint).Address;
                port = (sock.RemoteEndPoint as IPEndPoint).Port;

                Console.WriteLine("The client IP :"+ipAdd.ToString ());
                Console.WriteLine("The client port :" + port.ToString());
            }

            tcpListener.Stop();
        }
    }
}

类别:asp/asp.net | 浏览() | 评论 (2)
 
网友评论:
1
2008-02-22 16:55
现在你主要搞什么呢 winform还是webform??
 
2
2008-02-24 19:36
啥都搞,呵呵,瞎搞
 
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码:
 

     

©2008 Baidu