Socket Send and Receive [C#]
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Socket Send and Receive [C#],小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3937字,纯文字阅读大概需要6分钟。
内容图文
![Socket Send and Receive [C#]](/upload/InfoBanner/zyjiaocheng/629/224b327067b8411c99e0633806a4c9ab.jpg)
Socket Send and Receive [C#]
Socket Send and Receive [C#]
This example shows how to send and receive data via TCP/IP using Socket in .NET Framework. There are methods Socket.Send and Socket.Receive.
Socket.Send method
Send method sends data from your buffer to a connected Socket. When you call the Send method it returns number of bytes which were ?sent“. But it doesn't mean that the bytes were already received by the other side, it only means that the data were stored in a socket buffer and the socket will be trying to send them. If the socket buffer is full a WouldBlock error occurs. You should wait for a while a try to send the data again.
Following method sends size bytes stored in the buffer from the offset position. If the operation lasts more than timeout milliseconds it throws an exception.
[C#]
public static void Send(Socket socket, byte[] buffer, int offset, int size, int timeout) { int startTickCount = Environment.TickCount; int sent = 0; // how many bytes is already sent do { if (Environment.TickCount > startTickCount + timeout) throw new Exception("Timeout."); try { sent += socket.Send(buffer, offset + sent, size - sent, SocketFlags.None); } catch (SocketException ex) { if (ex.SocketErrorCode == SocketError.WouldBlock || ex.SocketErrorCode == SocketError.IOPending || ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable) { // socket buffer is probably full, wait and try again Thread.Sleep(30); } else throw ex; // any serious error occurr } } while (sent < size); }
To call the Send method use following code snippet (suppose the static Send method is defined in MyClass class). TCP/IP socket can be obtained using TcpClient class. Use TcpClient.Client property to get the underlying Socket (this property is public since .NET Framework 2.0).
[C#]
Socket socket = tcpClient.Client; string str = "Hello world!"; try { // sends the text with timeout 10s MyClass.Send(socket, Encoding.UTF8.GetBytes(str), 0, str.Length, 10000); } catch (Exception ex) { /* ... */ }
Socket.Receive method
Receive method receives data from a bound Socket to your buffer. The method returns number of received bytes. If the socket buffer is empty a WouldBlock error occurs. You should try to receive the data later.
Following method tries to receive size bytes into the buffer to the offset position. If the operation lasts more than timeout milliseconds it throws an exception.
[C#]
public static void Receive(Socket socket, byte[] buffer, int offset, int size, int timeout) { int startTickCount = Environment.TickCount; int received = 0; // how many bytes is already received do { if (Environment.TickCount > startTickCount + timeout) throw new Exception("Timeout."); try { received += socket.Receive(buffer, offset + received, size - received, SocketFlags.None); } catch (SocketException ex) { if (ex.SocketErrorCode == SocketError.WouldBlock || ex.SocketErrorCode == SocketError.IOPending || ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable) { // socket buffer is probably empty, wait and try again Thread.Sleep(30); } else throw ex; // any serious error occurr } } while (received < size); }
Call the Receive method using code such this:
[C#]
Socket socket = tcpClient.Client; byte[] buffer = new byte[12]; // length of the text "Hello world!" try { // receive data with timeout 10s MyClass.Receive(socket, buffer, 0, buffer.Length, 10000); string str = Encoding.UTF8.GetString(buffer, 0, buffer.Length); } catch (Exception ex) { /* ... */ }
See also
- [C#] Download Files from Web – how to download files from url to local disk
- [C#] Check Local IP Address – check whether an IP is local
- Socket – MSDN – provides many methods for network communications
- Socket.Send – MSDN – sends data to a socket
- Socket.Receive – MSDN – receives data from a socket
- SocketError – MSDN – enumeration with error codes for the Socket (WouldBlock)
- TcpClient – MSDN – provides client connections for TCP
- TcpClient.Client – MSDN – gets the underlying Socket
内容总结
以上是互联网集市为您收集整理的Socket Send and Receive [C#]全部内容,希望文章能够帮你解决Socket Send and Receive [C#]所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。