2015-10-06 50 views
1

我对套接字很怀疑。不在特定语言的套接字中,而是在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(); 
    } 
} 

回答

0

由于我不知道什么是你的错误,我只能说明我的来源,它的工作原理...

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); 
    } 
    } 
} 
+0

搜索了很多后,我尝试在虚拟机(Ubuntu)上运行我的服务器,并在PC上运行我的客户机(Windows 10)。从那以后,他们通常会谈到相同的代码! 显然它是防病毒或防火墙。 –

0

搜索了很多后,我尝试在虚拟机(Ubuntu)上运行我的服务器,并在PC上运行我的客户机(Windows 10)。从那以后,他们通常会谈到相同的代码! 显然它是防病毒或防火墙。

相关问题