gpt4 book ai didi

c# - 使用 C# 通过套接字发送和接收图像

转载 作者:可可西里 更新时间:2023-11-01 08:19:32 24 4
gpt4 key购买 nike

我正在尝试在 C# 中设置两个程序。基本上,我希望服务器设置一个简单的客户端服务器来监听来自客户端的图像。然后,在收到图像后,将其显示在 PictureBox 中。

我一直遇到以下错误:

A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll

错误发生在监听这一行的服务器代码上:图片 bmp = Image.FromStream(ms);有任何想法吗?

监听的Server代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace NetView
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
startListening();
}

private void startListening()
{
////////////////////////////////////////////

Console.WriteLine("Server is starting...");
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);

Socket newsock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);

newsock.Bind(ipep);
newsock.Listen(10);
Console.WriteLine("Waiting for a client...");

Socket client = newsock.Accept();
IPEndPoint newclient = (IPEndPoint)client.RemoteEndPoint;
Console.WriteLine("Connected with {0} at port {1}",
newclient.Address, newclient.Port);

while (true)
{
data = ReceiveVarData(client);
MemoryStream ms = new MemoryStream(data);
try
{
Image bmp = Image.FromStream(ms);
pictureBox1.Image = bmp;
}
catch (ArgumentException e)
{
Console.WriteLine("something broke");
}


if (data.Length == 0)
newsock.Listen(10);
}
//Console.WriteLine("Disconnected from {0}", newclient.Address);
client.Close();
newsock.Close();
/////////////////////////////////////////////

}

private static byte[] ReceiveVarData(Socket s)
{
int total = 0;
int recv;
byte[] datasize = new byte[4];

recv = s.Receive(datasize, 0, 4, 0);
int size = BitConverter.ToInt32(datasize, 0);
int dataleft = size;
byte[] data = new byte[size];


while (total < size)
{
recv = s.Receive(data, total, dataleft, 0);
if (recv == 0)
{
break;
}
total += recv;
dataleft -= recv;
}
return data;
}

private void Form1_Load(object sender, EventArgs e)
{

}

}
}

客户端代码:

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

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
byte[] data = new byte[1024];
int sent;
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);

Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);

try
{
server.Connect(ipep);
}
catch (SocketException e)
{
Console.WriteLine("Unable to connect to server.");
Console.WriteLine(e.ToString());
Console.ReadLine();
}


Bitmap bmp = new Bitmap("c:\\eek256.jpg");

MemoryStream ms = new MemoryStream();
// Save to memory using the Jpeg format
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

// read to end
byte[] bmpBytes = ms.GetBuffer();
bmp.Dispose();
ms.Close();

sent = SendVarData(server, bmpBytes);

Console.WriteLine("Disconnecting from server...");
server.Shutdown(SocketShutdown.Both);
server.Close();
Console.ReadLine();
}

private static int SendVarData(Socket s, byte[] data)
{
int total = 0;
int size = data.Length;
int dataleft = size;
int sent;

byte[] datasize = new byte[4];
datasize = BitConverter.GetBytes(size);
sent = s.Send(datasize);

while (total < size)
{
sent = s.Send(data, total, dataleft, SocketFlags.None);
total += sent;
dataleft -= sent;
}
return total;
}
}
}

最佳答案

ArgumentException 告诉您流中的图像格式无效。这可能是由于客户端应用程序在发送数据之前关闭了内存流造成的。

尝试将 byte[] bmpBytes = ms.GetBuffer(); 替换为

byte[] bmpBytes = ms.ToArray();

或者在数据发送后关闭流。

请记住,.GetBuffer() 返回的字节数组返回底层数组,而不是它的副本(.ToArray() 返回一个副本),只要父流有效。

关于c# - 使用 C# 通过套接字发送和接收图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/749964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com