|
You can use the Windows Sockets on the (.w_js, v.vbs) scripts:
Here's an example that writes the server on the port 9000 (Port must be open: ~XP see {win}\system32\drivers\etc\services file):
// File: server.w_js
AF_INET = 2 /* internetwork: UDP, TCP, etc. */
SOCK_STREAM = 1
// The code JScript
var flag = new Array();
var my_host = TCPIP.gethostname();
var sock_id = 100;
flag.push(TCPIP.WSAStartup(2,0)); //major=2, minor=0
flag.push(TCPIP.socket(sock_id, AF_INET, SOCK_STREAM, 0));
flag.push(TCPIP.fill_addr(sock_id, my_host, "9000"));
flag.push(TCPIP.bind(sock_id));
flag.push(TCPIP.listen(sock_id, 1024));
flag.push(TCPIP.acceptThread(sock_id));
for (var i=0; i<flag.length; i++)
Window.TextOut(1, 50, 30 + 20*i, i + ": " + flag[i]);
var text = "";
function EventByteRecv(id, byte)
{ text = text + String.fromCharCode(byte);
};
function EventEndAcceptThread(id)
{ Window.Delete(2);
Window.TextOut(2, 70, 150, "Recv: " + text);
Window.UpdateWindow();
TCPIP.acceptThread(sock_id); // new accept
text = "";
};
Here's an example that writes the client on the port 9000:
File: client.w_js
AF_INET = 2 /* internetwork: UDP, TCP, etc. */
SOCK_STREAM = 1
// The code JScript
var flag = new Array();
var my_host = TCPIP.gethostname();
var sock_id = 100;
flag.push(TCPIP.WSAStartup(2,0)); //major=2, minor=0 Winsock 2.0
flag.push(TCPIP.socket(sock_id, AF_INET, SOCK_STREAM, 0));
flag.push(TCPIP.fill_addr(sock_id, my_host, "9000"));
flag.push(TCPIP.connect(sock_id));
flag.push(TCPIP.send(sock_id, "Hello World!"));
//flag.push(TCPIP.fsend(sock_id, "d:\\bin.txt"));
flag.push(TCPIP.closesocket(sock_id));
for (var i=0; i<flag.length; i++)
Window.TextOut(1, 50, 30 + 20*i, i + ": " + flag[i]);
'VBScript server on port 9000
' Const section
AF_INET = 2 ' internetwork: UDP, TCP, etc.
SOCK_DGRAM = 0
SOCK_STREAM = 1
'The code VBScript
Dim flag(6), my_host, sock_id, i, text
my_host = TCPIP.gethostname()
sock_id = 100
flag(0) = TCPIP.WSAStartup(2,0)
flag(1) = TCPIP.socket(sock_id, AF_INET, SOCK_STREAM, 0)
flag(2) = TCPIP.fill_addr(sock_id, my_host, "9000")
flag(3) = TCPIP.bind(sock_id)
flag(4) = TCPIP.listen(sock_id, 1024)
flag(5) = TCPIP.acceptThread(sock_id)
For i = 0 To 5
Window.TextOut 1, 50, 30 + 20*i, i & ": " & flag(i)
Next
'The callback function (event)
Function EventByteRecv(id, rbyte)
If id = sock_id Then
text = text & String(1, rbyte)
End If
End Function
Function EventEndAcceptThread(id)
Window.Delete(2)
Window.TextOut 2, 70, 150, "Recv: " & text
Window.UpdateWindow()
TCPIP.acceptThread(sock_id)
text = ""
End Function
The new shell provides the work with function Winsock from script.
- WSAStartup(major, minor)
- The WSAStartup function initiates use of the Windows Sockets DLL by a script engine.
This function must be the first function called by an script. Major, minor - set winsock.dll
version required.
Return value: boolean.
- socket(id, family, type, protocol)
-
The socket function creates a socket which is bound to a specific service provider.
- id - The unique socket ID for your script (integer).
- family - An address family specification (integer):
AF_UNSPEC = 0 /* unspecified */
AF_UNIX = 1 /* local to host (pipes, portals) */
AF_INET = 2 /* internetwork: UDP, TCP, etc. */
AF_IMPLINK = 3 /* arpanet imp addresses */
AF_PUP = 4 /* pup protocols: e.g. BSP */
AF_CHAOS = 5 /* mit CHAOS protocols */
AF_IPX = 6 /* IPX and SPX */
AF_NS = 6 /* XEROX NS protocols */
AF_ISO = 7 /* ISO protocols */
AF_OSI = AF_ISO /* OSI is ISO */
AF_ECMA = 8 /* european computer manufacturers */
AF_DATAKIT = 9 /* datakit protocols */
AF_CCITT = 10 /* CCITT protocols, X.25 etc */
AF_SNA = 11 /* IBM SNA */
AF_DECnet = 12 /* DECnet */
AF_DLI = 13 /* Direct data link interface */
AF_LAT = 14 /* LAT */
AF_HYLINK = 15 /* NSC Hyperchannel */
AF_APPLETALK= 16 /* AppleTalk */
AF_NETBIOS = 17 /* NetBios-style addresses */
AF_VOICEVIEW= 18 /* VoiceView */
AF_FIREFOX = 19 /* FireFox */
AF_UNKNOWN1 = 20 /* Somebody is using this! */
AF_BAN = 21 /* Banyan */
- type - A type specification for the new socket: 0 - SOCK_DGRAM, 1 - SOCK_STREAM
- protocol - A particular protocol to be used with the socket which is specific to the indicated address family. Can be 0.
Return value: boolean
- closesocket(id)
- The closesocket function closes a socket (id). If no error occurs, closesocket returns
zero. Otherwise, a value of specific error code.
- gethostname()
- The gethostname function returns the standard host name for the local machine.
- fill_addr(id, addr, port)
- You can set the address for your socket (id). addr = "aa.bb.cc.dd" or hostname, port ="number" or
service name (string).
Return value: boolean
- connect(id)
- The connect function establishes a connection to a peer. This function is used to create a
connection to the specified destination. If the socket (id), is unbound, unique values are assigned to
the local association by the system, and the socket is marked as bound.
Return value: integer (error code)
See the
Windows Sockets Error Codes
- bind(id)
-
The bind function associates a local address with a socket.
This routine is used on an unconnected connectionless or connection-oriented socket, before subsequent connects or listens.
Return value: integer (error code)
See the
Windows Sockets Error Codes
- send(id, string)
- The send function sends data (string) on a connected socket (id).
Return value: integer (error code or byte count)
- fsend(id, path_to_file)
- The fsend function sends data from file (path_to_file) on a connected socket (id).
Return value: integer (error code or byte count)
- recv(id, size)
- The recv function receives data from a socket. This function is used on connected sockets
or bound connectionless sockets specified by the s parameter and is used to read incoming data.
The size -
size of the block to memories.
Return value: integer (error code or byte count)
- frecv(id, size, path_to_file)
- The frecv function receives data from a socket to file. This function is used on connected sockets
or bound connectionless sockets specified by the s parameter and is used to read incoming data. The size -
size of the block to memories.
Return value: integer (error code or byte count)
- listen(id, backlog)
-
The listen function establishes a socket to listen for an incoming connection. The backlog -
maximum length to which the queue of pending connections can grow.
Return value: integer (error code)
See the
Windows Sockets Error Codes
- acceptThread(id)
- The acceptThread function create thread for socket and return the thread ID (integer). Zero - error code.
The thread accepts a connection on a socket and data receive. You can make the callback function
in your script: EventByteRecv(id, byte) and EventEndAcceptThread(id). See the example.
content
|