昨夜飘风
昨 夜, 风, 飘 过; 枯 树, 叶, 飞 落。

 

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

/// <summary>
///TcpClass 的摘要说明
/// </summary>
public class TcpClass
{
    
public static string ConnectServer(String[] args)
    {
        TcpClient tcpc 
= new TcpClient();
        Byte[] read 
= new Byte[32];
        
if (args.Length != 1)
        {
            
return "No Server Address";//未指定服务器名称
        }
        String server 
= args[0];
        
// 验证服务器是否存在
        if (Dns.GetHostByName(server) == null)
        {
            
return "Can't find Server";//找不到服务器
        }
        
// 尝试连接到服务器
        try
        {
            tcpc.Connect(server, 
13);
            
if (tcpc.Connected == false)
            {
                
return "Can't connect Server";//无法连接到服务器
            }
        }
        
catch
        {
            
return "Can't connect Server";//无法连接到服务器
        }
        
// 获取流
        Stream s = tcpc.GetStream();
        
// 读取流并将它转换为 ASCII 码形式
        int bytes = s.Read(read, 0, read.Length);
        String Time 
= Encoding.ASCII.GetString(read);
        
// 显示数据
        
//Console.WriteLine("已接收到的 + 字节" + "字节");
        
//Console.WriteLine("当前日期和时间是:" + "时间");
        tcpc.Close();
        
return "已接收到" + bytes.ToString() + "字节    " + "当前日期和时间" + Time.ToString();
    }
}
posted on 2008-07-14 09:33  昨夜飘风  阅读(898)  评论(1编辑  收藏  举报