C#使用Socket实现发送和接收图片的方法
作者:令狐不聪 时间:2023-11-19 01:43:22
本文实例讲述了C#使用Socket实现发送和接收图片的方法。分享给大家供大家参考。具体如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
Class Program
{
static void Main (String[] args)
{
// 1. to create a socket
Socket sListen = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 2. Fill IP
IPAddress IP = IPAddress.Parse ("127.0.0.1");
IPEndPoint IPE = new IPEndPoint (IP, 4321);
// 3. binding
sListen.Bind (IPE);
// 4. Monitor
Console.WriteLine ("Service is listening ...");
sListen.Listen (2);
// 5. loop to accept client connection requests
while (true)
{
Socket clientSocket;
try
{
clientSocket = sListen.Accept ();
}
catch
{
throw;
}
// send data to the client
//clientSocket.Send (Encoding.Unicode.GetBytes ("I am a server, you there?? !!!!"));
// send the file
byte[] buffer = ReadImageFile ("1.jpg");
clientSocket.Send (buffer, buffer.Length, SocketFlags.None);
Console.WriteLine ("Send success!");
}
}
private static byte[] ReadImageFile (String img)
{
FileInfo fileinfo = new FileInfo (img);
byte[] buf = new byte[fileInfo.Length];
FileStream fs = new FileStream (img, FileMode.Open, FileAccess.Read);
fs.Read (buf, 0, buf.Length);
fs.Close ();
//fileInfo.Delete ();
GC.ReRegisterForFinalize (fileinfo);
GC.ReRegisterForFinalize (fs);
return buf;
}
}
}
客户端接收和保存图片的代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
namespace ConsoleApplication2
{
Class Program
{
static void Main (String[] args)
{
// 1. to create a socket
Socket S = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// 2. fill in the remote IP
IPAddress IP = IPAddress.Parse ("127.0.0.1");
IPEndPoint IPE = new IPEndPoint (IP, 4321);
Console.WriteLine ("started connection service ....");
// 3. connect to the server
s.Connect (IPE);
// 4. receive data
byte[] buffer = new byte[1000000];
s.Receive (buffer, buffer.Length, SocketFlags.None);
//var Msg = Encoding.Unicode.GetString (buffer);
//Console.WriteLine ("received message: (0)", msg);
Console.WriteLine ("Receive success");
FileStream fs = File.Create ("1.jpg");
fs.Write (buffer, 0, buffer.Length);
fs.Close ();
Console.ReadKey ();
}
}
}
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,Socket
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot 中application.yml与bootstrap.yml的区别
2023-07-09 12:30:56
Android 优雅的读写Excel
2021-09-19 15:10:37
详解Java匿名内部类
2023-04-30 14:51:28
![](https://img.aspxhome.com/file/2023/2/62432_0s.jpg)
详解Android StrictMode严格模式的使用方法
2023-09-14 17:03:39
sqlite数据库的介绍与java操作sqlite的实例讲解
2023-05-09 03:07:40
Spring+Junit4进行接口测试实例代码
2021-09-15 07:44:06
Android Studio和Gradle使用不同位置JDK的问题解决
2023-06-27 17:35:04
![](https://img.aspxhome.com/file/2023/2/84222_0s.png)
深入理解spring事务
2023-10-13 14:51:36
java swing GUI窗口美化方式
2023-04-03 00:13:56
![](https://img.aspxhome.com/file/2023/3/90963_0s.jpg)
java中对字符串每个字符统计的方法
2023-09-23 13:53:16
java初学者必须理解这几个问题
2023-04-07 14:22:36
Android WebP 图片压缩与传输
2022-05-30 16:18:16
![](https://img.aspxhome.com/file/2023/8/130328_0s.jpg)
全面理解Java类和对象
2021-11-17 11:11:50
springcloud feign传输List的坑及解决
2023-06-20 18:31:57
springmvc实现简单的拦截器
2022-02-27 05:13:39
![](https://img.aspxhome.com/file/2023/4/108474_0s.jpg)
基于java查找并打印输出字符串中字符出现次数
2022-07-28 19:45:47
Java关于IO流的全面介绍
2023-08-12 08:14:46
![](https://img.aspxhome.com/file/2023/9/58069_0s.png)
java结合keytool如何实现非对称签名和验证详解
2021-12-21 21:49:35
Java利用Jackson序列化实现数据脱敏详解
2023-12-22 17:47:38
![](https://img.aspxhome.com/file/2023/2/110812_0s.png)
解决java启动时报线程占用报错:Exception in thread “Thread-14“ java.net.BindException: Address already in use: bind
2021-07-05 04:26:23
![](https://img.aspxhome.com/file/2023/3/83583_0s.png)