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

C# Socket 入门2(转)

时间:2014-10-30 20:38:33      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   for   sp   

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

图片为 140K, 1.jgp

bubuko.com,布布扣

1. 服务端

bubuko.com,布布扣
bubuko.com,布布扣
 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 
bubuko.com,布布扣

2. 客户端

bubuko.com,布布扣
bubuko.com,布布扣
 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 
bubuko.com,布布扣

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

C# Socket 入门2(转)

标签:style   blog   http   io   color   os   ar   for   sp   

原文地址:http://www.cnblogs.com/vonly/p/4063465.html

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