我有一个小的客户端/服务器,可以通过TCP通过套接字发送数据。 这是发件人:
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();
}
for (int i = 0; i < 212; i++) {
Bitmap bmp = new Bitmap("C:/Users/ITFreak/source/repos/CamStream/CamStream/bin/x86/Debug/images/image" + Convert.ToString(i) + ".bmp");
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] bmpBytes = ms.ToArray();
bmp.Dispose();
ms.Close();
sent = SendVarData(server, bmpBytes);
Console.WriteLine("Sent " + sent + " bytes");
}
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;
}
}
和接收方:
private static 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)
{
int i = 0;
data = ReceiveVarData(client);
MemoryStream ms = new MemoryStream(data);
try
{
Image bmp = Image.FromStream(ms);
Image img2 = new Bitmap(bmp);
img2.Save("./images/image " + Convert.ToString(i) + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
Console.WriteLine("Saved image #" + Convert.ToString(i));
i++;
}
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;
}
如果我仅要传输一个图像,则代码可以正常工作,但是如果要传输所有数据,则代码会在Image.FromStream
处失败,并出现ArgumentException。
我发现i
在客户端从未增加,始终为0。
有人看到我的错误了,为什么没有相应保存多张图片/为什么我的客户端流损坏了?
答案 0 :(得分:1)
在您的startListening
方法中,您是在i
循环内声明while
,而在循环外进行声明:
int i = 0;
while (true)
{
data = ReceiveVarData(client);
...
}
否则,在每次循环迭代中,i
的值都将重新设置为0。