I have this code:
// Load the TCP Library
net = require('net');
//var sys = require('sys');
// Keep track of the chat clients
var clients = [];
// Start a TCP Server
net.createServer(function (socket) {
// Identify this client
socket.name = socket.remoteAddress + ":" + socket.remotePort
// Put this new client in the list
clients.push(socket);
// Send a nice welcome message and announce
socket.write("Welcome " + socket.name + "\n");
broadcast(socket.name + " joined the chat\n", socket);
socket.write(tools.foo);
// Handle incoming messages from clients.
socket.on('data', function (data) {
broadcast(socket.name + " >> " + data+"\n", socket);
});
// Remove the client from the list when it leaves
socket.on('end', function () {
clients.splice(clients.indexOf(socket), 1);
broadcast(socket.name + " left the chat.\n");
});
// Send a message to all clients
function broadcast(message, sender) {
clients.forEach(function (client) {
// Don't want to send it to sender
if (client === sender) return;
client.write(message);
});
// Log it to the server output too
process.stdout.write(message)
}
}).listen(5100,"192.168.1.8");
// Put a friendly message on the terminal of the server.
console.log("Chat server running at port 5100\n");
working with node.js.
In practice it's a simple chat server where I've develop an iOS client with the async tcp socket and it works perfectely with a telnet client.
I would develop another client in VisualBasic but all the demos that I have tried crash at the first connection (one of this).
how can I start the development?
What is your vb code? Is it VB6 or vb.net?
In vb6 you can make the following test project:
In the components add "Microsoft Winsock Control 6.0 (SP6)
'1 form with:
' 1 winsock control: name=Winsock1
' 1 textbox control: name=txtShow multiline=true
' 1 textbox control: name=txtCmd
' 1 command button : name=Command1
Option Explicit
Private mstrIP As String
Private mlngPort As Long
Private Sub Command1_Click()
SendCmd txtCmd.Text
End Sub
Private Sub Form_Load()
'setting initial data
txtShow.Text = ""
txtCmd.Text = "da"
mstrIP = "laptop07"
mlngPort = 7000
DoConnect
End Sub
Private Sub Form_Resize()
'positioning controls
Dim sngWidth As Single, sngHeight As Single
Dim sngCmdWidth As Single, sngCmdHeight As Single
Dim sngShowHeight As Single
sngWidth = ScaleWidth
sngHeight = ScaleHeight
sngCmdWidth = sngWidth / 2
sngCmdHeight = 495
sngShowHeight = sngHeight - sngCmdHeight
txtShow.Move 0, 0, sngWidth, sngShowHeight
txtCmd.Move 0, sngShowHeight, sngCmdWidth, sngCmdHeight
Command1.Move sngCmdWidth, sngShowHeight, sngCmdWidth, sngCmdHeight
End Sub
Private Sub Winsock1_Connect()
ShowTxt vbCrLf & "~ Connected" & vbCrLf
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData
ShowTxt strData
End Sub
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
ShowTxt vbCrLf & "~ Error " & CStr(Number) & vbCrLf & Description & vbCrLf
End Sub
Private Sub ShowTxt(strTxt As String)
'show received data
With txtShow
.SelStart = Len(.Text)
.SelText = strTxt
End With 'txtShow
End Sub
Private Sub DoConnect()
ShowTxt "~ connecting" & vbCrLf
With Winsock1
If .State <> sckClosed Then .Close
Do Until .State = sckClosed
DoEvents
Loop
.Connect mstrIP, mlngPort
End With 'Winsock1
End Sub
Private Sub SendCmd(strCmd As String)
With Winsock1
If .State = sckConnected Then
.SendData strCmd
Else
DoConnect
End If
End With 'Winsock1
End Sub
Replace "laptop07" with the IP addres of the server.
Replace 7000 with the port number on which the server is listening.
Replace "da" with the data you want to send to the server.
Run the project and wait until you see the message that it is connected.
If you get a message that the connection timed out, then you can't reach your server from your client (wrong IP addres, wrong port number, server isn't running, firewall settings, etc.).
When you are connected, click on the command button and the text from txtCmd will be sent. The received data will be shown in txtShow
System messages start with "~ ", received data is displayed without a header.