Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by midavi · Jan 17, 2012 at 08:38 PM · c#networking

C# Networking Issue

Hi,

I've created a Unity world that allows users to generate simple programs which are stored as local external files. The files can also individually be uploaded to an online server via a GUI interface using the inbuit Unity tools and a C# TCPClient.

So far I have the Unity client uploading program files to the online server working fine. My problem comes when I try to send a String back to the client from the server containing an acknowledgement message. For some reason after sending the file to the server Unity simply hangs/freezes up when trying to receive the acknowledgement String.

I need to send the response string on the same TCP connection that sent the program file (byte array). The code I've written is below. The Write function on the NetworkStream works fine if I take out the StreamReader, but Unity freezes if it is included.

 public static string sendFile(string filepath) {
 
     String response = "";
 
     // Get the file
     Stream fileStream = File.OpenRead(filepath);
 
     byte[] buffer = new byte[fileStream.Length];
     fileStream.Read(buffer, 0, buffer.Length);
 
     try {
 
         // Create the connection
         TCPClient client = new TCPClient("127.0.0.1", 21000);
 
         NetworkStream stream = client.GetStream();
 
         // Send the file to the server
         stream.Write(buffer, 0, buffer.Length);
         stream.Flush();
 
         // Receive a single string response from the server
         if (stream.CanRead) {
 
             StreamReader input = new StreamReader(stream);
             inputString = input.ReadLine();
 
         }
 
         input.Close();
         stream.Close();
         client.Close();
 
     }
 
     catch (IOException e) {
 
         print ("Error: " + e);
 
     }
 
     // Return the response message string
     return inputString;
 
 }

The online server the Unity script is connecting to is written in Java, (I'm actually a Java programmer so my C# is less than perfect). According to some logging on the server the file sent by the code above is being received and the response is being sent, but I believe Unity is hanging because for some reason it isn't being recieved correctly at input.ReadLine().

I have also tried implementing the code above using a StreamWriter rather than writing directly from the NetworkStream itself and everything works perfectly. Unfortunately the StreamWriter class doesn't have a method for sending a byte array, (only a char array).

If anybody has any idea why the code above isn't working then please let me know. Alternatively, if you have a different solution that would allow me to send a file (byte array) and receive back a string message using the same TCPClient connection then please also feel free to mention it.

Regards,

midavi.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by midavi · Jan 18, 2012 at 01:47 PM

Hi,

Does anybody have any ideas about a solution to my problem or an alternative method I could use?

I'm getting rather desperate now.

Regards,

Midavi.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

"Strange behaviour may occur" when using Network.AllocateViewID() 0 Answers

C# How to make the gun remember who equipped it 1 Answer

How to connect to hosted scene (Multiplayer) 2 Answers

C# How to have a gun select 1 Answer

Issue with synchronizing NPC Health over the network. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges