• 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 /
  • Help Room /
avatar image
0
Question by Ti_Mo · Sep 01, 2018 at 08:12 AM · networkingstreamtcpwrite data

TCP Stream is empty

Hey, i´m trying to setup a TCP communication between a server and a client. I get my client connected to the server, so far so good. Now i´m trying to send a message via stream.write to the server. The server doesnt recognize the message until im disconnecting the client. Then the "readingDone" method gets fired. But even then the message is empty. I tried to follow this example but i dont understand what they really did other than me. Except of mine is way less complex because i only need a message from the client to the server and not a chat option.

Below my server code:

 public class Server : MonoBehaviour
 {
     TcpClient client = new TcpClient();
     TcpListener listener;
     int port = 8010;
 
     byte[] bytes = new byte[500];
     String data = null;
 
     NetworkStream stream;
 
     public void Start(){
     
         listener = new TcpListener (IPAddress.Any, port);
         listener.Start ();
         listener.BeginAcceptTcpClient (OnServerConnect, null);
         //StartCoroutine (connecting ());
         
         }
 
 
     void OnServerConnect(IAsyncResult ar){
 
 
         client = listener.EndAcceptTcpClient (ar);
         Debug.Log ("Connected!!!!");
         listener.BeginAcceptSocket (OnServerConnect, null);
 
         stream = client.GetStream ();
         stream.BeginRead (bytes, 0, bytes.Length, readingDone, null);
     }
         
 
 
     void readingDone(IAsyncResult ar){
         int length = stream.EndRead (ar);
         data = System.Text.Encoding.UTF8.GetString (bytes, 0, length);
         stream.BeginRead (bytes, 0, bytes.Length, readingDone, null);
     
     }
 
     void Update(){
         if(data != null)
         Debug.Log (data);
     
     
     }
 
     protected void OnApplicationQuit(){
         listener.Stop ();
         client.Close ();
     
     
     }
         
 }

client:

 public class Client : MonoBehaviour
 {
 
     TcpClient client = new TcpClient();
     IPAddress serverIp = IPAddress.Parse("127.0.0.1");
     int port = 8010;
 
     //messagestuff
     String msg = "TestMessage";
     //byte[] data;
 
     NetworkStream stream;
 
     public void Start(){
         
         client.BeginConnect (serverIp, port, (ar) => client.EndConnect (ar), null);
 
         stream = client.GetStream ();
 
 
         Send ();
 
     }
 
     public void Send(){
 
 
 
         byte[] data = System.Text.Encoding.UTF8.GetBytes (msg);
         stream.Write(data, 0, data.Length);
     
     }

I tried to close the client connection after the writing is done, but it doesnt close. It seems like the writing process does block the rest of the method? Maybe someone can help me on this one, im stuck now for a few days and cant find a proper solution. Thanks!

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
1

Answer by thelghome · Jul 02, 2019 at 04:37 PM

There are some good networking example about TCP/UDP. You might be interested.

FM Exhibition Tool Pack | Forum

You can stream game view / webcam / audio between multiple platforms in local network. All written in C# and easy to modify.

Supported Platform: iOS/Android/Mac/PC

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

190 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need simple scripts to send a message from one unity build to another build. 0 Answers

Unity TCP Listener not working when built 0 Answers

Unity is only printing first message from server 0 Answers

Components for streaming video from one device to another 1 Answer

Unity Networking(old) Question.(Teleporting player) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges