码迷,mamicode.com
首页 > Windows程序 > 详细

C# Socket编程

时间:2015-01-19 20:45:48      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

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

namespace ConsoleApplication2
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //取得预保存的文件名 
            string fileName = "index.html";
            //远程主机 
            string hostName = "www.baidu.com";
            //端口 
            int port = 80;
            //得到主机信息 
            IPHostEntry ipInfo = Dns.GetHostByName(hostName);
            //取得IPAddress[] 
            IPAddress[] ipAddr = ipInfo.AddressList;
            //得到ip 
            IPAddress ip = ipAddr[0];
            //组合出远程终结点 
            IPEndPoint hostEP = new IPEndPoint(ip, port);
            //创建Socket 实例 
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                //尝试连接 
                socket.Connect(hostEP);
            }
            catch (Exception se)
            {
                Console.WriteLine(se.Message);
            }
            //发送给远程主机的请求内容串 
            string sendStr = "GET / HTTP/1.1\r\nHost: " + hostName +
            "\r\nConnection: Close\r\n\r\n";
            //创建bytes字节数组以转换发送串 
            byte[] bytesSendStr = new byte[1024];
            //将发送内容字符串转换成字节byte数组 
            bytesSendStr = Encoding.ASCII.GetBytes(sendStr);
            try
            {
                //向主机发送请求 
                socket.Send(bytesSendStr, bytesSendStr.Length, 0);
            }
            catch (Exception ce)
            {
                Console.WriteLine(ce.Message);
            }
            //声明接收返回内容的字符串 
            string recvStr = "";
            //声明字节数组,一次接收数据的长度为1024字节 
            byte[] recvBytes = new byte[1024];
            //返回实际接收内容的字节数 
            int bytes = 0;
            //循环读取,直到接收完所有数据 
            while (true)
            {
                bytes = socket.Receive(recvBytes, recvBytes.Length, 0);
                //读取完成后退出循环 
                if (bytes <= 0)
                    break;
                //将读取的字节数转换为字符串 
                recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
            }
            //将所读取的字符串转换为字节数组 
            byte[] content = Encoding.ASCII.GetBytes(recvStr);
            try
            {
                //创建文件流对象实例 
                FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                //写入文件 
                fs.Write(content, 0, content.Length);
            }
            catch (Exception fe)
            {
                Console.WriteLine(fe.Message);
            }
            //禁用Socket 
            socket.Shutdown(SocketShutdown.Both);
            //关闭Socket 
            socket.Close();
        }
    }
}

 

C# Socket编程

标签:

原文地址:http://www.cnblogs.com/mtsl/p/4234509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!