【发布时间】:2013-06-28 05:15:23
【问题描述】:
我在Windows Phone上开发了一个简单的TCP客户端,如图here on MSDN
这是按预期工作的。
现在,我想通过这个客户端发送非常大的 Base64 字符串(用于传输图像)。 但是,当我尝试从该客户端发送 Base64 字符串时,我在服务器上只收到了部分字符串,因此我无法在服务器上生成整个图像。
接收字符串的服务器端代码为:(Edited)
IPAddress ipAd = IPAddress.Any;
Console.Write("Port No. (leave blank for port 8001): ");
string port;
port = Console.ReadLine();
if (port == "")
port = "8001";
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAd, int.Parse(port));
/* Start Listeneting at the specified port */
myList.Start();
Console.WriteLine("\nThe server is running at port " + port);
Console.WriteLine("The local End point is :" +
myList.LocalEndpoint);
Console.WriteLine("\nWaiting for a connection.....");
Socket s = myList.AcceptSocket();
Console.WriteLine("\nConnection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[5 * 1024 * 1024]; // BIG SIZE for byte array, is this correct?
String message = String.Empty;
int k = s.Receive(b);
Console.WriteLine("\nRecieved...");
for (int i = 0; i < k; i++)
{
message += Convert.ToChar(b[i]);
Console.Write(Convert.ToChar(b[i]));
}
System.IO.File.WriteAllText(@"Message.txt", message); // write it to a file
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\n\nSent Acknowledgement");
/* clean up */
s.Close();
myList.Stop();
我真的被困在这里了。 请帮帮我。
我认为问题出在客户端而不是服务器。 请帮助我。
我在客户端使用的类可以在上面提到的MSDN article找到。
PS:我已经尝试增加类中TIMEOUT_MILLISECONDS和MAX_BUFFER_SIZE的值。但这并没有帮助。
更新:
这是一些客户端代码 (look here on MSDN for reference):
// Make sure we can perform this action with valid data
if (ValidateRemoteHost() && ValidateInput())
{
// Instantiate the SocketClient
SocketClient client = new SocketClient();
// Attempt to connect to the echo server
Log(String.Format("Connecting to server '{0}' over port {1} (echo) ...", txtRemoteHost.Text, ECHO_PORT), true);
string result = client.Connect(txtRemoteHost.Text, ECHO_PORT);
Log(result, false);
byte[] bytearray = null;
// Attempt to send our message to be echoed to the echo server
Log(String.Format("Sending '{0}' to server ...", txtInput.Text), true);
if (checkBox1.IsChecked == true) // this checkbox is for image selection
{
// This is the part where we send images
using (MemoryStream ms = new MemoryStream())
{
WriteableBitmap wbitmp = new WriteableBitmap((BitmapImage)image1.Source);
wbitmp.SaveJpeg(ms, (int)wbitmp.PixelWidth, (int)wbitmp.PixelHeight, 0, 10);
bytearray = ms.ToArray();
string str = Convert.ToBase64String(bytearray);
result = client.Send(str);
System.Diagnostics.Debug.WriteLine("\n\nMessge sent:\n\n" + str + "\n\n");
}
}
else
{
result = client.Send(txtInput.Text);
}
Log(result, false);
// Receive a response from the server
Log("Requesting Receive ...", true);
result = client.Receive();
Log(result, false);
// Close the socket connection explicitly
client.Close();
}
【问题讨论】:
-
客户端能否发送大的 Base64 字符串?
-
你确定你没有在一切都完成之前关闭客户端的套接字吗?我建议您使用 Wireshark 之类的工具来查看实际发送的数据。
-
如果你认为客户端有问题,那么客户端的代码在哪里?
-
Jon 很可能是正确的 - TCP 是“礼貌”协议,因此“即发即弃”不像在 UDP 中那样工作,并且断开连接事件可能会超过网络缓冲区。正确的方法是有一个协议,服务器向客户端确认已收到所有数据。在移动世界中,对协议进行大小健全性检查也可能会有所帮助。
-
TCP 通常会(尝试)确保在完全处理断开连接之前所有数据都通过(除非尚未完成接收数据的系统尝试发送)
标签: c# windows-phone-7 tcp base64 tcpclient