如何向连接着的Socket发送字符串?急!!!
譬如:要发送"999999",该怎么写呢?
socket.Send();

解决方案 »

  1.   

    先将字符串转换成byte[],然后调用Socket.Send (Byte[])
      

  2.   

    字符串转换成byte[],是怎么转换的?
      

  3.   

    private static string SocketSendReceive(string server, int port) 
        {
            string request = "GET / HTTP/1.1\r\nHost: " + server + 
                "\r\nConnection: Close\r\n\r\n";
            Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
            Byte[] bytesReceived = new Byte[256];
           
            // Create a socket connection with the specified server and port.
            Socket s = ConnectSocket(server, port);        if (s == null)
                return ("Connection failed");
          
            // Send request to the server.
            s.Send(bytesSent, bytesSent.Length, 0);  
            
            // Receive the server home page content.
            int bytes = 0;
            string page = "Default HTML page on " + server + ":\r\n";        // The following will block until te page is transmitted.
            do {
                bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
                page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
            }
            while (bytes > 0);
            
            return page;
        }