【问题标题】:How I can to create python(or swift) TCP client for my TCP c# server?如何为我的 TCP c# 服务器创建 python(或 swift)TCP 客户端?
【发布时间】:2019-07-23 08:55:27
【问题描述】:

如何为我的 TCP c# 服务器创建 python(或 swift)TCP 客户端?

我的 TCP 服务器的 c# API:

Client client = Client();
bool Connect()
{
    UserInfo userInfo = new UserInfo("login", "pass");
    NetworkConnectionParam connectionParams = new NetworkConnectionParam("127.0.0.1", 4021);
    try
    {
        client.Connect(connectionParams,userInfo,ClientInitFlags.Empty);
    }
    catch
    {
        return false;
    }
    return client.IsStarted;
}

我试试(python):

import socket

sock = socket.socket()
sock.connect(('localhost', 4021))

但我不明白我必须如何发送我的登录名和密码(例如在 API for c# 中)。

【问题讨论】:

  • 你为什么要删除你之前的问题并发布一个完全相同的问题?

标签: c# python swift tcp


【解决方案1】:

我不明白你想要实现什么。 如果你想在 C# 中运行 Python 代码,那么看看这个IronPython

如果你想在 C# 中实现 TCP 客户端,那就试试这样吧:

using System.Net; 
using System.Net.Sockets;

 class Program
{
    static int port = 8005; // your port
    static void Main(string[] args)
    {
        // get address to run socket
        var ipPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);

        // create socket
        var listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        try
        {
            // binding
            listenSocket.Bind(ipPoint);

            // start listen
            listenSocket.Listen(10);

            Console.WriteLine("Server is working");

            while (true)
            {
                Socket handler = listenSocket.Accept();
                // recieve message
                StringBuilder builder = new StringBuilder();
                int bytes = 0; // quantity of received bytes 
                byte[] data = new byte[256]; // data buffer

                do
                {
                    bytes = handler.Receive(data);
                    builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                }
                while (handler.Available>0);

                Console.WriteLine(DateTime.Now.ToShortTimeString() + ": " + builder.ToString());

                // send response
                string message = "your message recieved";
                data = Encoding.Unicode.GetBytes(message);
                handler.Send(data);
                // close socket
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

【讨论】:

  • 我想为 TCP 服务器(在 c# 上)创建一个 python TCP 客户端(在 python 上,而不是 c# 和 c# 中的 python 代码)。 c# 服务器的 python 客户端。我的 TCP 服务器有 c# API:Client client = Client(); bool Connect() { UserInfo userInfo = new UserInfo("login", "pass"); NetworkConnectionParam connectionParams = new NetworkConnectionParam("127.0.0.1", 4021); try { client.Connect(connectionParams,userInfo,ClientInitFlags.Empty); } catch { return false; } return client.IsStarted; }
  • 我在 python 中尝试sock.connect(('localhost', 4021))。但我不明白我必须如何发送我的登录名和密码(就像在 API for c# 中一样)。
  • @Xcodebeginner 首先你想实现哪种“类型”的身份验证?如果您只想简单地发送登录名和密码,那么这里是如何在 Python 中发送数据:stackoverflow.com/questions/21233340/…,您还需要在 c# 中修改代码才能接收此数据。
  • 谢谢)。我有一个服务器(c#),我无法更改此服务器上的某些部分代码。但我为这台服务器准备了 API (c#)。我必须使用这个 API。 c# Client client = Client(); bool Connect() { UserInfo userInfo = new UserInfo("login", "pass"); NetworkConnectionParam connectionParams = new NetworkConnectionParam("127.0.0.1", 4021); try { client.Connect(connectionParams,userInfo,ClientInitFlags.Empty); } catch { return false; } return client.IsStarted; } 我不知道我必须如何在另一种编程语言(python,swift)上创建类似的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-18
  • 1970-01-01
  • 2019-02-26
  • 2019-02-26
  • 2019-09-28
相关资源
最近更新 更多