0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vbsの作法 その45

Last updated at Posted at 2019-10-21

概要

vbsの作法調べてみた。
winsock使ってみる。
httpserver作ってみた。

写真

image

環境

windows7 64bit

サンプルコード

Dim oWin
Dim App6
Dim Appflg
Dim html
Const LocalPort = 80
Const sckClosed = 0
Const sckOpen = 1
Const sckListening = 2
Const sckConnectionPending = 3
Const sckResolvingHost = 4
Const sckHostResolved = 5
Const sckConnecting = 6
Const sckConnected = 7
Const sckClosing = 8
Const sckError = 9

Sub App6_exit()
	Appflg = 2
End Sub

Sub App6_ok()
	Set oWin = CreateObject("OSWINSCK.Winsock")
	WScript.ConnectObject oWin, "soc_"
	oWin.LocalPort = LocalPort
	ServerListen
End Sub

Sub soc_OnConnectionRequest(requestID)
	If oWin.State <> sckClosed Then
		oWin.CloseWinsock
	End If
	oWin.Accept(requestID)
	WScript.Sleep 2000
End Sub

Sub soc_OnDataArrival(bytesTotal)
	Dim strData
	strData = ""
	oWin.GetData strData, vbString
	App6.text = strData
	oWin.SendData html
	WScript.Sleep 1000
	oWin.CloseWinsock
End Sub

Sub soc_OnError(Number, Description, SCode, Source, HelpFile, HelpContext, CancelDisplay)
	MsgBox "Server Error " & Number & vbCrLf & Description
	ServerClose()
End Sub

Sub ServerListen()
	If oWin.State <> sckClosed Then
		oWin.CloseWinsock
	End If
	oWin.Listen
End SUb

Sub ServerClose()
	If oWin.State <> sckClosed Then
		oWin.CloseWinsock
	End If
	Set oWin = Nothing
	Wscript.Quit
End SUb

Set App6 = Createobject("OHIcompo6.srv")
WScript.ConnectObject App6, "App6_"
html = "HTTP/1.1 200 OK" & vbcrlf & "Content-Type: text/html" & vbcrlf & "Content-Length: 100" & vbcrlf & vbcrlf & "<!DOCTYPE html><html><body><h1>it's work!</h1></body></html>" & vbcrlf
App6.text = html
Do
	WScript.Sleep(100)
	App6.ping
Loop until Appflg = 2
Set App6 = Nothing


以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?