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 hbey · Oct 20, 2014 at 04:24 AM · clientsocket

how to use SocketAsyncEventArgs in unity3d

our game have to support wp8 platform, so i have to use SocketAsyncEventArgs rewrite my communication logic, but when i use SocketAsyncEventArgs in unity3d i can't get message exactly from server,here is my logic,please help me, Thanks!

 public class NewBehaviourScript : MonoBehaviour
 {
     SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
     static IPAddress address = IPAddress.Parse("192.168.1.200");
     static IPEndPoint hostEntry = new IPEndPoint(address, 3699);
     //static IPAddress address = IPAddress.Parse("192.168.1.201");
     //IPEndPoint hostEntry = new IPEndPoint(address, 7777);
     public static Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     // Use this for initialization
     void Start()
     {
         socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(SocketEventArg_Completed);
 
         socketEventArg.RemoteEndPoint = hostEntry;
         socketEventArg.UserToken = sock;
         sock.ConnectAsync(socketEventArg);
     }
 
     // Update is called once per frame
     void Update()
     {
 
     }
     void OnGUI()
     {
         if (GUI.Button(new Rect(0, 0, 100, 20), "click"))
             Send();
     }
     static void SocketEventArg_Completed(object sender, SocketAsyncEventArgs e)
     {
         switch (e.LastOperation)
         {
             case SocketAsyncOperation.Connect:
                 ProcessConnect(e);
                 break;
 
             case SocketAsyncOperation.Receive:
                 ProcessReceive(e);
                 break;
 
             case SocketAsyncOperation.Send:
                 ProcessSend(e);
                 break;
 
             default:
                 throw new Exception("Invalid operation completed");
         }
     }
     private static void ProcessConnect(SocketAsyncEventArgs e)
     {
         if (e.SocketError == SocketError.Success)
         {
             // Successfully connected to the server
             Debug.Log("connect success");
         }
         else
         {
             throw new SocketException((int)e.SocketError);
         }
     }
     void Send()
     {
         SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
         socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(SocketEventArg_Completed);
         socketEventArg.RemoteEndPoint = hostEntry;
         socketEventArg.UserToken = sock;
 
         byte[] buffer = Encoding.UTF8.GetBytes("Hello World");
         socketEventArg.SetBuffer(buffer, 0, buffer.Length);
 
         bool willRaiseEvent = sock.SendAsync(socketEventArg);
 
         if (!willRaiseEvent)
         {
             ProcessSend(socketEventArg);
         }
     }
     private static void ProcessReceive(SocketAsyncEventArgs e)
     {
         if (e.SocketError == SocketError.Success)
         {
             // Received data from server
             Debug.Log("get from server" + Encoding.UTF8.GetString(e.Buffer));
         }
         else
         {
             throw new SocketException((int)e.SocketError);
         }
     }
     static void ProcessSend(SocketAsyncEventArgs e)
     {
         if (e.SocketError == SocketError.Success)
         {
             //Read data sent from the server
             Socket sock = e.UserToken as Socket;
             bool willRaiseEvent = sock.ReceiveAsync(e);
 
             if (!willRaiseEvent)
             {
                 ProcessReceive(e);
             }
         }
         else
         {
             throw new SocketException((int)e.SocketError);
         }
     }
 }
 
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 dscroggi · Dec 31, 2014 at 02:17 AM

hard to tell with only the client code. looks ok overall just see one thing, setbuffer is not getting called before the receive

try [this] video series, it uses saea[1]

[1]: https://www.youtube.com/playlist?list=PLVrxpIRQe-GElmDh-apbTfViywsfHLGNj

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

JavaScript TcpClient().Connect freezes 1 Answer

MATLAB to Unity Port Connection 0 Answers

a question about Client/Server programming in Unity 1 Answer

Threaded Socket Client with Thread-Safe Queue for Unity 0 Answers

TCP responses are not receiving properly on client side. 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