我对套接字很怀疑。不在特定语言的套接字中,而是在Ruby(纯粹)和C#(Unity 5.0.4〜Mono)之间的套接字上。C#中的套接字(客户端)<->红宝石(服务器)
我想为大学做一份工作,我必须在图形引擎和我的服务器之间传递信息。但是现在我陷入了C#!
我如何在C#中亲爱的代码连接到我的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();
}
}
搜索了很多后,我尝试在虚拟机(Ubuntu)上运行我的服务器,并在PC上运行我的客户机(Windows 10)。从那以后,他们通常会谈到相同的代码! 显然它是防病毒或防火墙。 –