我有两个类处理套接字连接的类和Form1,它在从服务器获取数据时在Panel上显示内容。目前,我有数据进入,第一项因任何原因被截断。其余的值都很好,只是第一个。
我认为可能是两个线程覆盖信息并试图使用锁来防止这种情况,但这不是问题。根据我放置锁的位置,我从服务器收到的信息是不同的,这很奇怪所以我认为线程可能起作用。
由于截断,似乎不完整的Cube对象被JSON转换添加到字符串中,这是导致我的异常的原因。我只是不明白为什么我在第一个周期发送不完整的数据。我假设如果我修复了开始部分,结束部分应该自行修复,因为它需要所有1024个字节。非常感谢帮助。
private void button1_Click(object sender, EventArgs e)
{
String name = textBox1.Text;
string ip = textBox2.Text;
if (name == "" || ip == "")
{
MessageBox.Show("Name or IP cannot be empty!");
return;
}
label1.Hide();
textBox1.Hide();
label2.Hide();
textBox2.Hide();
button1.Hide();
panel1.Show();
net.Connect_to_Server(info, ip);
}
public void info(StateObject state)
{
net.Send(state.socket, textBox1.Text + " \n");
net.i_want_more_data(state);
List<String> myList = new List<String>();
CubeObject c;
//lock (_object)
//{
System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
String s = encoding.GetString(state.buffer, 0, state.buffer.Length);
Console.WriteLine(s);
myList.Add(s);
char[] delimitedChars = { '}' };
string[] words = s.Split(delimitedChars);
// myList = s.Split(new char[] { ',' }).ToList();
//s = s.Replace(",", System.Environment.NewLine);
// Console.WriteLine(s);
lock (_object)
{
for (int i = 0; i < words.Length; i++)
{
words[i] += "}";
// words[i].Replace("\\", "");
c = JsonConvert.DeserializeObject<CubeObject>(words[i]);
Console.Out.WriteLine(c.loc_x);
paint(c.loc_x, c.loc_y, (int)c.getWidth());
}
}
//CubeObject c = JsonConvert.DeserializeObject<CubeObject>(s);
// int i = 0;
}
private void paint(double x, double y, double width)
{
SolidBrush myBrush = new SolidBrush(System.Drawing.Color.Red);
Graphics formGraphics = panel1.CreateGraphics();
formGraphics.FillRectangle(myBrush, new Rectangle((int)x, (int)y, (int)width, (int)width));
myBrush.Dispose();
formGraphics.Dispose();
}
}
网络代码:
{
public class StateObject
{
// Client socket.
public Socket socket;
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer;
// Received data string.
public StringBuilder sb = new StringBuilder();
public Action<StateObject> callback { get; set; }
public StateObject(Action<StateObject> result)
{
callback = result;
sb = new StringBuilder();
buffer = new byte[BufferSize];
socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
}
}
public class Network
{
private const int port = 11000;
private readonly object sendSync = new object();
private static System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
private byte[] buffer = new byte[1024];
//private Socket client;
public Socket Connect_to_Server(Action<StateObject> callback, string hostname)
{
StateObject state = new StateObject(callback);
state.socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
state.callback = callback;
state.socket.BeginConnect(hostname, port, new AsyncCallback(Connected_to_Server), state);
return state.socket;
}
public void Connected_to_Server(IAsyncResult state_in_an_ar_object)
{
StateObject state = (StateObject) state_in_an_ar_object.AsyncState;
// Socket client = new Socket(SocketType.Stream, ProtocolType.Tcp);
try
{
state.socket.EndConnect(state_in_an_ar_object);
state.callback(state);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
public void ReceiveCallback(IAsyncResult state_in_an_ar_object)
{
StateObject state = (StateObject)state_in_an_ar_object.AsyncState;
byte[] bt = state.buffer;
Socket socket = state.socket;
int newBt = socket.EndReceive(state_in_an_ar_object);
lock (sendSync)
{
if (newBt != 0)
{
state.sb.Append(encoding.GetString(bt, 0, newBt));
state.callback(state);
// i_want_more_data(state); //nmot sure about this one
}
else
{
//socket.Shutdown(SocketShutdown.Send);
socket.Close();
}
}
}
public void i_want_more_data(StateObject state)
{
state.socket.BeginReceive(state.buffer, 0, state.buffer.Length,
SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
}
public void Send(Socket socket, String data)
{
byte[] outgoingBuffer = encoding.GetBytes(data);
socket.BeginSend(outgoingBuffer, 0, outgoingBuffer.Length,
SocketFlags.None, new AsyncCallback(SendCallBack), socket);
}
public void SendCallBack(IAsyncResult state_in_an_ar_object)
{
try
{
// Retrieve the socket from the state object.
Socket client = (Socket)state_in_an_ar_object.AsyncState;
// Complete sending the data to the remote device.
int bytesSent = client.EndSend(state_in_an_ar_object);
Console.WriteLine("Sent {0} bytes to server.", bytesSent);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
预期结果:
{"loc_x":444.0,"loc_y":393.0,"argb_color":-11798065,"uid":9,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":256.0,"loc_y":415.0,"argb_color":-15991370,"uid":10,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":244.0,"loc_y":493.0,"argb_color":-12771693,"uid":11,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":396.0,"loc_y":256.0,"argb_color":-11670589,"uid":12,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":218.0,"loc_y":359.0,"argb_color":-5975094,"uid":13,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":177.0,"loc_y":599.0,"argb_color":-13474943,"uid":14,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":691.0,"loc_y":504.0,"argb_color":-12280922,"uid":15,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":593.0,"loc_y":207.0,"argb_color":-8033330,"uid":16,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":582.0,"loc_y":390.0,"argb_color":-1306819,"uid":17,"team_id":0,"food":true,"Name":"","Mass":1.0}
实际结果:
eam_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":444.0,"loc_y":393.0,"argb_color":-11798065,"uid":9,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":256.0,"loc_y":415.0,"argb_color":-15991370,"uid":10,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":244.0,"loc_y":493.0,"argb_color":-12771693,"uid":11,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":396.0,"loc_y":256.0,"argb_color":-11670589,"uid":12,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":218.0,"loc_y":359.0,"argb_color":-5975094,"uid":13,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":177.0,"loc_y":599.0,"argb_color":-13474943,"uid":14,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":691.0,"loc_y":504.0,"argb_color":-12280922,"uid":15,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":593.0,"loc_y":207.0,"argb_color":-8033330,"uid":16,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":582.0,"loc_y":390.0,"argb_color":-1306819,"uid":17,"team_id":0,"food":true,"Name":"","Mass":1.0}
{"loc_x":678.0,"loc_y