c#(客户端)中的套接字<->Ruby(服务器)

本文关键字:Ruby 服务器 客户端 套接字 | 更新日期: 2023-09-27 18:16:13

我对套接字很怀疑。不是特定语言的套接字,而是Ruby(纯)和c# (Unity 5.0.4 ~ Mono)之间的套接字。

我正在为大学做一份工作,我必须在图形引擎和服务器之间传递信息。但是现在我被c#困住了!

我如何,我亲爱的代码在c#,连接到我的服务器在Ruby?我试过了,但还是失败了。

Ruby服务器

#####################
# HelmTek Serv v001 #
#####################
require "socket"                                                                    
#####################
# Funções e Métodos #
#####################
def TCPService()                                                
    server = TCPServer.open(8080)
    puts("Serv started on 8080")
    loop{
    Thread.start(server.accept) do |client|
        puts ("New Client") 
        client.puts(Time.now.ctime)
        printf("Enviado a data")
        client.puts "Closing the connection. Bye!"  
        line = client.recv(100)
                puts line.to_s
        client.close
    end
    }
end
def UDPService()
    loop{
    }
end
#####################
# Algoritmo do Serv #
#####################
puts "HelmTek Server 2015"                                                 
puts "por Marlon H. Schweigert"
Thread.new {TCPService()}                                   
UDPService()                                                            
c#客户机

using UnityEngine;
using System.Collections;
using System;
using System.Net.Sockets;
using System.Text;
using System.IO;
public class SocketConnector : MonoBehaviour {
    // Use this for initialization
    void Start () {
        TcpClient client = new TcpClient ("localhost", 8080);
        StreamReader sr = new StreamReader (client.GetStream ());
        StreamWriter sw = new StreamWriter (client.GetStream ());
        sw.WriteLine ("Ola");
        sw.Flush ();
        while (true){
            string linha = sr.ReadLine();
            if (linha == null) break;
            Debug.Log (linha);
        }
        client.Close ();
    }
}

c#(客户端)中的套接字<->Ruby(服务器)

由于我不知道你的错误是什么,我只能显示我的源代码,它的工作…

bool isConnect = false;
void ConnectAndRead(){  //Instead of Start(), I use this to control the connection
  using (TcpClient client = new TcpClient() ){
    client.Connect(your_ip, your_port);
    if (!client.Connected) throw new Exception();
    isConnect = true;
    NetworkStream stream = client.GetStream();
    StreamReader sr = new StreamReader (stream );
    StreamWriter sw = new StreamWriter (stream );
    sw.AutoFlush = true;
    while (true) {
        //I copy your source and paste here!!
        string linha = sr.ReadLine();
        if (linha == null) break;
        Debug.Log (linha);
    }
  }
}

在搜索了很多之后,我尝试在虚拟机(Ubuntu)上运行我的服务器,在我的PC (Windows 10)上运行我的客户端。从那以后,他们通常用同样的密码说话!显然是杀毒软件或防火墙。