zoukankan      html  css  js  c++  java
  • C# Socket 入门2

    现在来传一个图片看看, 改改程序, 看看服务端

    图片为 140K, 1.jgp

    1. 服务端

    代码
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 using System.Net.Sockets;
     5 using System.Net;
     6 using System.IO;
     7 
     8 namespace ConsoleApplication1
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             // 1.创建套节字
    15             Socket sListen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    16 
    17             // 2.填充IP
    18             IPAddress ip = IPAddress.Parse("127.0.0.1");
    19             IPEndPoint ipe = new IPEndPoint(ip, 4321);
    20 
    21             // 3.绑定
    22             sListen.Bind(ipe);
    23 
    24             // 4.监听
    25             Console.WriteLine("服务正在监听...");
    26             sListen.Listen(2);
    27 
    28             // 5.循环接受客户的连接请求
    29             while (true)
    30             {
    31                 Socket clientSocket;
    32                 try
    33                 {
    34                     clientSocket = sListen.Accept();
    35                 }
    36                 catch
    37                 {
    38                     throw;
    39                 }
    40 
    41                 // 向客户端发送数据
    42                 //clientSocket.Send(Encoding.Unicode.GetBytes("我是服务器, 你好呀!!!!"));
    43 
    44                 // 发送文件
    45                 byte[] buffer = ReadImageFile("1.jpg");
    46                 clientSocket.Send(buffer, buffer.Length, SocketFlags.None);
    47                 Console.WriteLine("发送成功!");
    48             }
    49         }
    50 
    51         private static byte[] ReadImageFile(string img)
    52         {
    53             FileInfo fileInfo = new FileInfo(img);
    54             byte[] buf = new byte[fileInfo.Length];
    55             FileStream fs = new FileStream(img, FileMode.Open, FileAccess.Read);
    56             fs.Read(buf, 0, buf.Length);
    57             fs.Close();
    58             //fileInfo.Delete();
    59             GC.ReRegisterForFinalize(fileInfo);
    60             GC.ReRegisterForFinalize(fs);
    61             return buf;
    62         }
    63 
    64     }
    65 }
    66 

    2. 客户端

    代码
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 using System.Net.Sockets;
     5 using System.Net;
     6 using System.IO;
     7 
     8 namespace ConsoleApplication2
     9 {
    10     class Program
    11     {
    12         static void Main(string[] args)
    13         {
    14             // 1.创建套节字
    15             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    16 
    17             // 2.填写远程IP
    18             IPAddress ip = IPAddress.Parse("127.0.0.1");
    19             IPEndPoint ipe = new IPEndPoint(ip, 4321);
    20 
    21             Console.WriteLine("开始连接服务....");
    22             // 3.连接服务器
    23             s.Connect(ipe);
    24 
    25             // 4.接收数据
    26             byte[] buffer = new byte[1000000];
    27             s.Receive(buffer, buffer.Length, SocketFlags.None);
    28             //var msg = Encoding.Unicode.GetString(buffer);
    29             //Console.WriteLine("接收消息: {0}", msg);
    30             Console.WriteLine("接收成功");
    31 
    32             FileStream fs =  File.Create("1.jpg");
    33             fs.Write(buffer, 0, buffer.Length);
    34             fs.Close();
    35 
    36             Console.ReadKey();
    37         }
    38     }
    39 }
    40 

    哈哈, 就这样成了,,,,看看在客户端下会生成 1.jpg

  • 相关阅读:
    JQuery操作元素的属性与样式及位置 复制代码
    【转】从零开始编写自己的C#框架
    一步一步Asp.Net MVC系列_权限管理设计
    ASP.NET MVC5 网站开发实践
    MVC5+EF6 入门
    ASP.NET中使用JqGrid完整实现
    技术是容易学会的(Copy)
    Oracle 创建用户并且授权
    python安装zlib一直无效
    linux ln 命令(转载)
  • 原文地址:https://www.cnblogs.com/LinFx/p/2123684.html
Copyright © 2011-2022 走看看