c# - writing and reading using socket in unity 3d -


this code

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 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; } 

}

connection created. message not writing server , reading

how can it

i think have taken code http://answers.unity3d.com/questions/15422/unity-project-and-3rd-party-apps.html, think there error in code. i'll repeat here posted there.

the following code not work correctly:

public string readsocket() {   if (!socketready)     return "";   if (thestream.dataavailable)     return thereader.readline();   return ""; } 

this caused me headache quite few hours. think checking dataavailable on stream not reliable way check if there data read on streamreader. not want check dataavailable. however, if remove that, code block on readline when there no more read. instead, need set timeout reading stream, won't wait longer (say) millisecond:

thestream.readtimeout = 1; 

and then, can use like:

public string readsocket() {     if (!socketready)         return "";     try {         return thereader.readline();     } catch (exception e) {         return "";     } } 

this code isn't perfect, still need improve (e.g., check kind of exception raised, , deal appropriately). , maybe there's better way overall (i experimented using peek(), -1 returns suspect when socket closes, , not when there no more data read now). however, should solve problems posted code, having. if you're finding data missing server, it's sitting in reader stream, , won't read until new data sent server , stored in stream such thestream.dataavailable returns true.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -