【问题标题】:C# Bot connect using TcpClient classC# Bot 使用 TcpClient 类连接
【发布时间】:2016-01-07 10:43:15
【问题描述】:

我正在尝试使用 IP 和端口连接到服务器。我想创建一个TcpClient 连接,以便稍后发送和接收数据包。

在使用 fsocket 函数之前,我在 PHP 中创建了它。例如:

$fp = fsockopen(packets::$server, packets::$port, $errno, $errstr, 30);
fwrite($fp, packets::login('username', 'password'));

现在我想用 C# 重做这个。我开始在谷歌上搜索,但实际上只发现了一种使用 TcpClient class 的方法,所以我开始如下:

public void tcpConnect(string ip, int port)
{
    TcpClient connection = new TcpClient();
    connection.Connect(ip, port);
}

我以如下格式调用这个函数:

functions tcp = new functions();
tcp.tcpConnect("74.113.233.195", 9061);

我将如何继续向这个 tcp 连接发送数据包?

在 PHP 中,我使用以下格式创建数据包:

public static function login($username, $password) {
      $packet  = "<msg t='sys'>";
      $packet .= "<body action='login' r='0'>";
      $packet .= "<login z='simpleChat'>";
      $packet .= "<nick>";
      $packet .= "<![CDATA[" . $username . "]]>";
      $packet .= "</nick>";
      $packet .= "<pword>";
      $packet .= "<![CDATA[" . $password . "]]>";
      $packet .= "</pword>";
      $packet .= "</login>";
      $packet .= "</body>";
      $packet .= "</msg>";
      $packet .= chr(0);
      return $packet;
}

我曾经以这种格式添加到套接字中:

fwrite($fp, packets::login('username', 'password')); 

我将如何使用 c# 来完成这项工作?


更新:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;

namespace bot_console
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Bot started...");
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect("74.113.233.195", 9061);
            byte[] login = Encoding.ASCII.GetBytes("<msg t='sys'><body action='login' r='0'><login z='simpleChat'><nick><![CDATA[turtle-1]]></nick><pword><![CDATA[password]]></pword></login></body></msg>");
            byte[] navigate = Encoding.ASCII.GetBytes("{\"b\":{\"x\":\"cluster\",\"p\":{\"roomName\":\"|2.1.world1017.school1@|30|2|A\",\"password\":\"\"},\"c\":\"enterRoom\",\"r\":\"-1\"},\"t\":\"xt\"}");
            socket.Send(login);
            socket.Send(navigate);
            Console.ReadLine();
        }
    }
}

那是实际的代码,遗憾的是它似乎没有发送数据包。有人有想法吗?

【问题讨论】:

  • Sockets 这就是你的答案。 tcpClient.Clientmsdn.microsoft.com/en-us/library/…
  • 所以我会开始创建一个 TcpClient 客户端 = new TcpClient(); asnd 为它分配套接字?
  • 或者你可以使用Socket。就像Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.BeginConnect(ip, port, EndConnect, socket);,其中EndConnectcallback。在回调中,您可以将Socket 设置为一些处理发送和接收数据包的包装器。
  • 谢谢,我去看看:)
  • 更新了问题,你知道有什么问题吗?

标签: c# php tcp


【解决方案1】:

你可以参考这个:Socket Send and Receive

Socket.Send 方法

public static void Send(Socket socket, byte[] buffer, int offset, int size, int timeout)
{
  int startTickCount = Environment.TickCount;
  int sent = 0;  // how many bytes is already sent
  do {
    if (Environment.TickCount > startTickCount + timeout)
      throw new Exception("Timeout.");
    try {
      sent += socket.Send(buffer, offset + sent, size - sent, SocketFlags.None);
    }
    catch (SocketException ex)
    {
      if (ex.SocketErrorCode == SocketError.WouldBlock ||
          ex.SocketErrorCode == SocketError.IOPending ||
          ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
      {
        // socket buffer is probably full, wait and try again
        Thread.Sleep(30);
      }
      else
        throw ex;  // any serious error occurr
    }
  } while (sent < size);
}

Socket.Receive 方法

public static void Receive(Socket socket, byte[] buffer, int offset, int size, int timeout)
{
  int startTickCount = Environment.TickCount;
  int received = 0;  // how many bytes is already received
  do {
    if (Environment.TickCount > startTickCount + timeout)
      throw new Exception("Timeout.");
    try {
      received += socket.Receive(buffer, offset + received, size - received, SocketFlags.None);
    }
    catch (SocketException ex)
    {
      if (ex.SocketErrorCode == SocketError.WouldBlock ||
          ex.SocketErrorCode == SocketError.IOPending ||
          ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
      {
        // socket buffer is probably empty, wait and try again
        Thread.Sleep(30);
      }
      else
        throw ex;  // any serious error occurr
    }
  } while (received < size);
}

【讨论】:

  • 谢谢,我看看这个:)
猜你喜欢
  • 2011-09-10
  • 2010-12-12
  • 2020-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-01
相关资源
最近更新 更多