Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 OZLN61 · Jul 15, 2017 at 03:28 PM · androiderrorsockettcp

Android Tcp Socket Problem

hi everyone,

  • finished a server / client project to send and receive packets. The project works fine on the computer, but android only sends 3-4 large packages and never sends after them.No problem with small packets on android. There is no problem getting the server out of the box, but the android is having trouble sending large packets.I can give you skype if you can help. I share my code down there and I'm waiting for help. Thx...

client:

     void SendWhole(Socket sock, IPacket packet)
     {
         var stream = new MemoryStream();
         var writer = new BinaryWriter(stream);
         
         byte[] payload = packet.ToBytes();
         writer.Write((short)packet.GetOpcode());
         writer.Write(payload.Length);
         writer.Write(payload);
         
         byte[] bufferCopy = new byte[stream.ToArray().Length];
         System.Buffer.BlockCopy(stream.ToArray(), 0, bufferCopy, 0, bufferCopy.Length);
         
         sock.Send(bufferCopy,0,bufferCopy.Length ,SocketFlags.None );
     }`

my server recieve method:

     private void ProcessReceive(SocketAsyncEventArgs e)
     {
         var state = (StateObject)e.UserToken;
         
         // Check if the remote host closed the connection
         if (e.BytesTransferred > 0 && e.SocketError == SocketError.Success)
         {
             // Opcode not set -> Header has not been parsed yet
             if (state.Opcode == -1)
             {
                 ReadHeader(e);
             }
             else
             {
                 ReadBody(e);
             }
         }
         else
         {
             CloseClientSocket(e);
         }
     }
     
     private void ReadHeader(SocketAsyncEventArgs e)
     {
         var state = (StateObject)e.UserToken;
         
         state.TotalBytesRead += e.BytesTransferred;
         
         // If header isn't read fully, read more
         if (state.TotalBytesRead < HeaderLength)
         {
             int missing = HeaderLength - state.TotalBytesRead;
             e.SetBuffer(e.Buffer, e.Offset, missing);
         }
         else
         {
             // Received full header, parse it and read packet body
             state.Opcode = BitConverter.ToInt16(state.Buffer, 0);
             state.PacketDataLength = BitConverter.ToInt32(state.Buffer, 2);
             state.PacketData = new byte[state.PacketDataLength]; // TODO: Hot-Spot, use pooled buffers to avoid GC overhead
             state.TotalBytesRead = 0;
             
             e.SetBuffer(state.PacketData, 0, state.PacketDataLength);
         }
         BeginReceive(e);
     }
     
     private void ReadBody(SocketAsyncEventArgs e)
     {
         var state = (StateObject)e.UserToken;
         Console.WriteLine("GETTED LEGenTH = " + state.PacketData.Length.ToString());
         // Body not received fully, read more
         if (e.BytesTransferred < state.PacketDataLength)
         {
             var missing = state.PacketDataLength - e.BytesTransferred;

             e.SetBuffer(state.PacketData, e.Offset, missing);
             Console.WriteLine("GET DATA CORPUT!");
             //////////////////////////////////////////////////// I GET HERE ON ANDRIOD!////////////////////////////////////
         }
         else
         {
             // Publish packet to business logic (DON'T BLOCK THERE, use worker threads if needed)
             OnPacketReceived(state.Opcode, state.PacketData);
             
             // Don't forget to reset this
             e.SetBuffer(state.Buffer, 0, HeaderLength);
             state.TotalBytesRead = 0;
             state.Opcode = -1;
         }
         
         BeginReceive(e);
         var packet = CreateRandomPacket();
         if (state.ClientSocket.Connected)
             SendWhole(state.ClientSocket, packet);
     }
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 Bunny83 · Jul 15, 2017 at 03:50 PM

Your question sounds a bit confusing. The provided code snippet makes it hard to follow what happens, especially your receiving code.

Are you aware of the fact that TCP is a stream protocol and not a packet protocol. Of course the stream is transmitted with one or multiple packets but from the application point of view a TCP connection just provides a data stream. A stream can be splitted (fragmented) across multiple packets however TCP ensures that the data arrives in the right order. The MTU size can vary between different hardware implementations as well as different operating systems. If the data you send + protocol overhead is larger than the MTU size the data need to be splitted.

Receiving code for a TCP connection should always use a buffer where it can append the next data chunk once it arrived. If you plan to send "packets" over a TCP connection you have to manually detect the beginning and end of your packet as TCP is an endless stream of bytes which can arrive at any speed / fragmentation.

Comment
Add comment · Show 5 · 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
avatar image OZLN61 · Jul 17, 2017 at 09:12 AM 0
Share

Not related to $$anonymous$$TU size.

i have this: I do not see a problem when I put a ThreadSleep(50) on the ProcessReceive function. But I do not have to set a ThreadSleep for my fast data transfer. what should I do?

avatar image Bunny83 OZLN61 · Jul 17, 2017 at 02:42 PM 0
Share

Sorry, but the amount of information that you provided is simply not enough to follow what happens. If you use BeginReceive it might be a concurency problem. $$anonymous$$eep in $$anonymous$$d that a byte buffer must only be accessed by a single thread at a time. If you run your business logic on a seperate thread, make sure you only work on a copy of the data.

But again, we don't know how you actually receive the data and how you further process it. We don't even know what "SetBuffer" actually does. How exactly do you stitch a fragmented packet back together?

avatar image OZLN61 Bunny83 · Jul 17, 2017 at 03:15 PM 0
Share

my full project: https://yadi.sk/d/nBxIgWh$$anonymous$$3L8hW8

i want send continuousy big data.

Show more comments
avatar image OZLN61 · Jul 18, 2017 at 08:54 AM 0
Share

I need to talk to you live. Can you add me on skype(username:ozln61) or another one?

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

148 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

Related Questions

Socket error in Android Build 1 Answer

Unity's TCP socket connection attempt "actively refused" 3 Answers

Is it possible to control a game with an Android phone without Unity? 1 Answer

Weird Android Compile IO Error 1392 1 Answer

Android game crash startup 0 Answers


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