是发完了信息马上就等着接受么?
如果服务端需要计算一会,我怎么等
谢谢!

解决方案 »

  1.   

    不是有个listen么,它会一直监听端口的,一有数据它就接收
      

  2.   

    你最好去看看socket的例子,参看
    http://www.codeproject.com/csharp/AsyncSocketServerandClien.asp
      

  3.   

    技术交流群号:23266021
    欢迎大家在此讨论关于.net的各种技术。1号群
      

  4.   

    using System;
    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;
        }
    }
      

  5.   

    是发完了信息马上就等着接受么?
    如果服务端需要计算一会,我怎么等?上面其中代码中的这句
    // Receive the response from the remote device.
    int bytesRec = sender.Receive(bytes);如果没有可读取的数据,则 Receive 方法将一直处于阻止状态,直到数据可用,除非使用 Socket.ReceiveTimeout 设置了超时值。如果超过超时值,Receive 调用将引发 SocketException。如果您处于非阻止模式,并且协议堆栈缓冲区中没有可用的数据,则 Receive 方法将立即完成并引发 SocketException。
      

  6.   

    客户端运行这句后处于等待状态,直到接收到了服务器发回的信息,不需要知道服务器计算多长的时间,只要服务器发送了,客户端就可以收到。想一问一答完了就断开就参考MSDN上同步SOCKET的例子就可以完成了。
      

  7.   

    如果是一问一答就断开的话我还是建议你用UPD协议吧,这样还可以省了断开TCP的麻烦
      

  8.   

    wangsaokui(无间道III(终极无间)) : 请把服务器端的程序也发上来,好么