重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Dim th As Threading.Thread
创新互联专业为企业提供茌平网站建设、茌平做网站、茌平网站设计、茌平网站制作等企业网站建设、网页设计与制作、茌平企业网站模板建站服务,10多年茌平做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
2 Dim tcpl As System.Net.Sockets.TcpListener
3
4 Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
5 th = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf MyListen))
6 th.Start()
7 End Sub
8
9 Public Sub SendMessage()Sub SendMessage(ByVal IP As String, ByVal SendMsg As String)
10 Try
11 If IP "" Then
12 Dim tcpc As New System.Net.Sockets.TcpClient(IP, 5656)
13 Dim tcpStream As Net.Sockets.NetworkStream = tcpc.GetStream
14 Dim reqStream As New IO.StreamWriter(tcpStream)
15 reqStream.Write(SendMsg)
16 reqStream.Flush()
17 tcpStream.Close()
18 tcpc.Close()
19 End If
20 Catch ex As Exception
21 MsgBox(ex.Message.ToString)
22 End Try
23 End Sub
24 Private Sub MyListen()Sub MyListen()
25 Try
26 Dim ipAddress As System.Net.IPAddress = System.Net.Dns.Resolve(System.Net.Dns.GetHostName).AddressList(0)
27 tcpl = New System.Net.Sockets.TcpListener(ipAddress, 5656)
28 tcpl.Start()
29 While True
30 Dim s As System.Net.Sockets.Socket = tcpl.AcceptSocket()
31 Dim MyBuffer(1024) As Byte
32 Dim i As Integer
33 i = s.Receive(MyBuffer)
34 If i 0 Then
35 Dim lstrRec As String
36 Dim j As Integer
37 For j = 0 To i - 1
38 TextBox1.Text += Chr(MyBuffer(j)) ","
39 Next
40 End If
41 End While
42 Catch ex As Exception
43 MsgBox(ex.Message.ToString)
44 End Try
45 End Sub
46
47 Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
48 SendMessage("192.168.0.61", TextBox2.Text)
49 End Sub
做exe程序获取本机IP地址用
System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName()).AddressList
做web程序获取客户端的IP地址用
HttpContext.Current.Request.ServerVariables("REMOTE_ADDR"])
你最好可以换台电脑访问网址看获取IP地址状况。
如果你测试访问的时候,使用的是 localhost 进行访问的,那么服务器端获得的也就是127.0.0.1,因为localhost是一个环路地址,特殊的.如果你是通过你的ip地址进行访问的,那么获得的IP地址就会是正确的了. 要通过自己本地的IP进行访问,在控制面板的网络连接里找到本地连接,查看它的属性,并找到"详细信息"按钮,点击后弹出的对话框中IPV4地址就是本地地址.或打开命令行,输入 ipconfig /all 即可找到本地地址.在浏览器中通过这个IP地址访问,就不会是127.0.0.1了.
1.获取当前电脑名:System.Net.Dns.GetHostName()
2.根据电脑名取出全部IP地址:System.Net.Dns.Resolve(电脑名).AddressList
或者 System.Net.Dns.GetHostByName(电脑名).AddressList
3.根据IP地址取出电脑名:System.Net.Dns.Resolve(IP地址).HostName