Sockets









How do I use Sockets
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: SocketUdp.cs Copy
39         public override bool Connect()
40         {
41             lock (this.syncer)
42             {
43                 bool baseOk = base.Connect();
44                 if (!baseOk)
45                 {
46                     return false;
47                 }
48
49                 this.State = PhotonSocketState.Connecting;
50
51                 Thread dns = new Thread(this.DnsAndConnect);
52                 dns.Name = "photon dns thread";
53                 dns.IsBackground = true;
54                 dns.Start();
55
56                 return true;
57             }
58         }
File name: SocketUdp.cs Copy
60         public override bool Disconnect()
61         {
62             if (this.ReportDebugOfLevel(DebugLevel.INFO))
63             {
64                 this.EnqueueDebugReturn(DebugLevel.INFO, "CSharpSocket.Disconnect()");
65             }
66
67             this.State = PhotonSocketState.Disconnecting;
68
69             lock (this.syncer)
70             {
71                 if (this.sock != null)
72                 {
73                     try
74                     {
75                         this.sock.Close();
76                     }
77                     catch (Exception ex)
78                     {
79                         this.EnqueueDebugReturn(DebugLevel.INFO, "Exception in Disconnect(): " + ex);
80                     }
81
82                     this.sock = null;
83                 }
84             }
85
86             this.State = PhotonSocketState.Disconnected;
87             return true;
88         }
File name: SocketUdp.cs Copy
119         internal void DnsAndConnect()
120         {
121             try
122             {
123                 lock (this.syncer)
124                 {
125                     this.sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
126
127                     IPAddress ep = IPhotonSocket.GetIpAddress(this.ServerAddress);
128                     this.sock.Connect(ep, this.ServerPort);
129
130                     this.State = PhotonSocketState.Connected;
131                 }
132             }
133             catch (SecurityException se)
134             {
135                 if (this.ReportDebugOfLevel(DebugLevel.ERROR))
136                 {
137                     this.Listener.DebugReturn(DebugLevel.ERROR, "Connect() failed: " + se.ToString());
138                 }
139
140                 this.HandleException(StatusCode.SecurityExceptionOnConnect);
141                 return;
142             }
143             catch (Exception se)
144             {
145                 if (this.ReportDebugOfLevel(DebugLevel.ERROR))
146                 {
147                     this.Listener.DebugReturn(DebugLevel.ERROR, "Connect() failed: " + se.ToString());
148                 }
149
150                 this.HandleException(StatusCode.ExceptionOnConnect);
151                 return;
152             }
153
154             Thread run = new Thread(new ThreadStart(ReceiveLoop));
155             run.Name = "photon receive thread";
156             run.IsBackground = true;
157             run.Start();
158         }
File name: SocketUdp.cs Copy
161         public void ReceiveLoop()
162         {
163             byte[] inBuffer = new byte[this.MTU];
164             while (this.State == PhotonSocketState.Connected)
165             {
166                 try
167                 {
168                     int read = this.sock.Receive(inBuffer);
169                     this.HandleReceivedDatagram(inBuffer, read, true);
170                 }
171                 catch (Exception e)
172                 {
173                     if (this.State != PhotonSocketState.Disconnecting && this.State != PhotonSocketState.Disconnected)
174                     {
175                         if (this.ReportDebugOfLevel(DebugLevel.ERROR))
176                         {
177                             this.EnqueueDebugReturn(DebugLevel.ERROR, "Receive issue. State: " + this.State + " Exception: " + e);
178                         }
179
180                         this.HandleException(StatusCode.ExceptionOnReceive);
181                     }
182                 }
183             } //while Connected receive
184
185             // on exit of the receive-loop: disconnect socket
186             this.Disconnect();
187         }

Sockets 112 lượt xem

Gõ tìm kiếm nhanh...