是发完了信息马上就等着接受么?
如果服务端需要计算一会,我怎么等
谢谢!
如果服务端需要计算一会,我怎么等
谢谢!
解决方案 »
- C# datagridview 操作多级节点的xml文件问题
- 谁会从中间截取字符串
- 谁有together for vs.net 2005的crack
- 关于ref传值的一个新问题:难!!!在线讨论。。。
- winform的treeview增加删除节点后我怎么保存,显示?应该在库里保存还是XML?哪个要好一点.
- socket.Receive为什么在收到空字节的时候会kill当前线程
- Combox下拉绑定DataGridView
- Unable to convert MySQL date/time value to System.DateTime
- 文件读写问题,每读多少字后加标记!
- 几个窗体如何共享一个数据库?
- 动态菜单
- C#中用socket如何实现多个客户端与服务器的通信?
http://www.codeproject.com/csharp/AsyncSocketServerandClien.asp
欢迎大家在此讨论关于.net的各种技术。1号群
using System.Net;
using System.Net.Sockets;
using System.Text;public class SynchronousSocketClient { public static void StartClient() {
// Data buffer for incoming data.
byte[] bytes = new byte[1024]; // Connect to a remote device.
try {
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName())
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000); // Create a TCP/IP socket.
Socket sender = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp ); // Connect the socket to the remote endpoint. Catch any errors.
try {
sender.Connect(remoteEP); Console.WriteLine("Socket connected to {0}",
sender.RemoteEndPoint.ToString()); // Encode the data string into a byte array.
byte[] msg = Encoding.ASCII.GetBytes("This is a test<EOF>"); // Send the data through the socket.
int bytesSent = sender.Send(msg); // Receive the response from the remote device.
int bytesRec = sender.Receive(bytes);
Console.WriteLine("Echoed test = {0}",
Encoding.ASCII.GetString(bytes,0,bytesRec)); // Release the socket.
sender.Shutdown(SocketShutdown.Both);
sender.Close();
} catch (ArgumentNullException ane) {
Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
} catch (SocketException se) {
Console.WriteLine("SocketException : {0}",se.ToString());
} catch (Exception e) {
Console.WriteLine("Unexpected exception : {0}", e.ToString());
} } catch (Exception e) {
Console.WriteLine( e.ToString());
}
}
public static int Main(String[] args) {
StartClient();
return 0;
}
}
如果服务端需要计算一会,我怎么等?上面其中代码中的这句
// Receive the response from the remote device.
int bytesRec = sender.Receive(bytes);如果没有可读取的数据,则 Receive 方法将一直处于阻止状态,直到数据可用,除非使用 Socket.ReceiveTimeout 设置了超时值。如果超过超时值,Receive 调用将引发 SocketException。如果您处于非阻止模式,并且协议堆栈缓冲区中没有可用的数据,则 Receive 方法将立即完成并引发 SocketException。