【问题标题】:Writing and reading using socket使用socket读写
【发布时间】:2013-07-17 04:07:47
【问题描述】:

这是我的代码

using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Net.Sockets;

public class s_TCP : MonoBehaviour {

internal Boolean socketReady = false;

TcpClient mySocket;
NetworkStream theStream;
StreamWriter theWriter;
StreamReader theReader;
String Host = "198.57.44.231";
Int32 Port = 1337;
string channel = "testingSona";

void Start () {   
    setupSocket();
    //string msg = "__SUBSCRIBE__"+channel+"__ENDSUBSCRIBE__";
    string msg = "Sending By Sona";
    writeSocket(msg);
    readSocket();

}
void Update () {
    //readSocket();
}

public void setupSocket() { 
    try {
        mySocket = new TcpClient(Host, Port);
        theStream = mySocket.GetStream(); 
        theWriter = new StreamWriter(theStream);
        theReader = new StreamReader(theStream);
        socketReady = true;         
    }
    catch (Exception e) {
        Debug.Log("Socket error: " + e);
    }
}
public void writeSocket(string theLine) {
    if (!socketReady)
        return;
    String foo = theLine + "\r\n";
    theWriter.Write(foo);
    theWriter.Flush();

}
public String readSocket() { 
    if (!socketReady)
        return ""; 
    if (theStream.DataAvailable){           
        string message = theReader.ReadLine();
        print(message);print(12345);
        return theReader.ReadLine();
    }
    else{print("no value");
        return "";
    }

}
public void closeSocket() {
    if (!socketReady)
        return;
    theWriter.Close();
    theReader.Close();
    mySocket.Close();
    socketReady = false;
}

}

连接已创建。但是消息没有写入服务器并读取

我该怎么做

【问题讨论】:

    标签: c# sockets unity3d


    【解决方案1】:

    我认为您已从http://answers.unity3d.com/questions/15422/unity-project-and-3rd-party-apps.html 获取此代码,但我认为此代码中有错误。我将在此处重复我在此处发布的内容。

    以下代码无法正常工作:

    public String readSocket() {
      if (!socketReady)
        return "";
      if (theStream.DataAvailable)
        return theReader.ReadLine();
      return "";
    }
    

    这让我头痛了好几个小时。我认为检查 stream 上的 DataAvailable 并不是检查 streamreader 上是否有要读取的数据的可靠方法。所以你不想检查 DataAvailable。但是,如果您只是删除它,那么当没有更多要读取的内容时,代码将在 ReadLine 上阻塞。因此,您需要设置从流中读取的超时时间,这样您等待的时间不会超过(比如说)一毫秒:

    theStream.ReadTimeout = 1;
    

    然后,你可以使用类似的东西:

    public String readSocket() {
        if (!socketReady)
            return "";
        try {
            return theReader.ReadLine();
        } catch (Exception e) {
            return "";
        }
    }
    

    这段代码并不完美,我还需要改进它(例如,检查引发了什么样的异常,并适当地处理它)。也许总体上有更好的方法来做到这一点(我尝试使用 Peek(),但我怀疑它返回的 -1 是在套接字关闭时,而不仅仅是当现在没有更多数据要读取时)。但是,这应该可以解决已发布代码的问题,就像我遇到的那样。如果您发现服务器中缺少数据,那么它可能位于您的阅读器流中,并且在从服务器发送新数据并将其存储在流中以便 theStream.DataAvailable 返回 true 之前不会被读取。

    【讨论】:

    • 以防万一有人感兴趣,上述方法不是处理套接字的正确方法。不过,我不记得为什么。此外,我在 Windows 机器上忽略了 ReadTimeout 设置时遇到了麻烦。有一种更好的方法来处理潜伏在某个地方的 Unity wiki 上的套接字,iirc。
    猜你喜欢
    • 2012-02-25
    • 2015-04-09
    • 1970-01-01
    • 2016-08-28
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多