我有这段代码:
// 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");
使用node.js。
在实践中,它是一个简单的聊天服务器,我使用异步tcp套接字开发iOS客户端,它完全可以与telnet客户端一起使用。
我会在VisualBasic中开发另一个客户端,但我尝试过的所有演示在第一次连接时崩溃(one of this)。
我该如何开始开发?
答案 0 :(得分:3)
你的vb代码是什么?是VB6还是vb.net?
在vb6中,您可以进行以下测试项目:
在组件中添加“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
将“laptop07”替换为服务器的IP地址。
将7000替换为服务器正在侦听的端口号。
将“da”替换为您要发送给服务器的数据。
运行项目并等待,直到看到它已连接的消息。
如果收到连接超时的消息,则表示无法从客户端访问服务器(IP地址错误,端口号错误,服务器未运行,防火墙设置等)。
连接后,单击命令按钮,将发送txtCmd中的文本。 收到的数据将显示在txtShow
中系统消息以“〜”开头,接收的数据显示没有标题。