Visual Basic

BACK


コンピュータ名を取得する

Dim strHost As String

strHost = System.Net.Dns.GetHostName()

MessageBox.Show(strHost, "実行結果")


コンピュータのIPアドレスを取得する

Dim strHost As String

Dim ip As System.Net.IPHostEntry

Dim ipAddr As System.Net.IPAddress

'ホスト名を取得

strHost = System.Net.Dns.GetHostName()

'IPリストを取得

ip = System.Net.Dns.GetHostByName(strHost)

'IPリストの最初を取得

ipAddr = ip.AddressList(0)

'ピリオド付きで表示する

MessageBox.Show(ipAddr.ToString(), "実行結果")


サーバーに TCP/IPで接続する

'TCP接続を行う

Dim tcpClient As New System.Net.Sockets.TcpClient()

Try

tcpClient.Connect("www.microsoft.com", 80)

MessageBox.Show("接続できた", "実行結果")

tcpClient.Close()

Catch ex As Exception

'接続が出来なかった場合は例外が発生する

MessageBox.Show(ex.ToString(), "実行結果")

End Try


サーバーへTCP/IPでデータを送信する

'サーバーに接続する

Dim tcpClient As New System.Net.Sockets.TcpClient()

tcpClient.Connect("www.microsoft.com", 80)

'ストリームを取得する

Dim Stream As System.Net.Sockets.NetworkStream

Stream = tcpClient.GetStream

'送信データを作成する

Dim bytSend As Byte()

bytSend = System.Text.Encoding.ASCII.GetBytes( _

"GET /Default.asp HTTP/1.0" + vbCrLf + vbCrLf)

'送信準備が出来ていれば送信する

If Stream.CanWrite() = True Then

Try

Stream.Write(bytSend, 0, bytSend.Length)

MessageBox.Show("データ送信しました", "実行結果")

Catch ex As Exception

MessageBox.Show(ex.ToString(), "実行結果")

End Try

Else

MessageBox.Show("送信に失敗しました", "実行結果")

End If

tcpClient.Close()


サーバーからTCP/IP でデータを受信する

'サーバーに接続する

Dim tcpClient As New System.Net.Sockets.TcpClient()

tcpClient.Connect("www.microsoft.com", 80)

'ストリームを作成しデータ送信する

Dim Stream As System.Net.Sockets.NetworkStream = tcpClient.GetStream()

Dim bytSend As Byte() = System.Text.Encoding.ASCII.GetBytes("GET /Default.asp HTTP/1.0" + ControlChars.CrLf + ControlChars.CrLf)

Stream.Write(bytSend, 0, bytSend.Length())

'受信バッファを用意する

Dim bytRead(2000) As Byte

Dim strText As String

Dim intLength As Integer

'ストリームからデータを受信する

strText = ""

Do

intLength = Stream.Read(bytRead, 0, bytRead.Length())

strText += System.Text.Encoding.ASCII.GetString(bytRead, 0, intLength)

If intLength < bytRead.Length Then

Exit Do

End If

tcpClient.Close()

Loop

TextBox1.Text = strText


HTTPサーバーに接続する

Dim webClient As New System.Net.WebClient()

Dim sr As System.IO.Stream =webClient.OpenRead("http://www.microsoft.com/")

Dim srRead As New System.IO.StreamReader(sr)

TextBox1.Text = srRead.ReadToEnd()

srRead.Close()

 

BACK