-1

我有这个代码:

// 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 中开发另一个客户端,但我尝试过的所有演示都在第一次连接时崩溃(其中一个)。

我该如何开始开发?

4

1 回答 1

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中

系统消息以“~”开头,接收到的数据不带标题显示。

于 2013-05-16T06:04:15.463 回答