我只是想问为什么当我尝试发送一个字节时,它根本没有发送,我做了一个简单的程序并且不知道为什么它现在工作(在客户端和服务器中发送属性)这是我的服务器端代码
public void ServerStartReceiving()
{
sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sck.Bind(new IPEndPoint(0, 1980));
sck.Listen(0);
acc = sck.Accept();
sck.Close();
new Thread(() =>
{
while (true)
{
byte[] sizeBuff = new byte[4];
acc.Receive(sizeBuff, 0, sizeBuff.Length, 0);
int size = BitConverter.ToInt32(sizeBuff, 0);
MemoryStream ms = new MemoryStream();
while (size > 0)
{
byte[] buffer;
lblStatus.Text = "Receiveing Text....";
if (size < acc.ReceiveBufferSize)
{
buffer = new byte[size];
}
else
{
buffer = new byte[acc.ReceiveBufferSize];
}
int rec = acc.Receive(buffer, 0, buffer.Length, 0);
size -= rec;
ms.Write(buffer, 0, buffer.Length);
}
ms.Close();
byte[] data = ms.ToArray();
ms.Dispose();
while (true == true)
{
byte[] sizelang = new byte[4];
acc.Receive(sizelang, 0, sizelang.Length, 0);
lblStatus.Text = "Receiving Language Data....";
int sizela = BitConverter.ToInt32(sizelang, 0);
MemoryStream ms1 = new MemoryStream();
while (sizela > 0)
{
byte[] lang;
if (sizela < acc.ReceiveBufferSize)
{
lang = new byte[sizela];
}
else
{
lang = new byte[acc.ReceiveBufferSize];
}
int rec = acc.Receive(lang, 0, lang.Length, 0);
sizela -= rec;
ms1.Write(lang, 0, lang.Length);
}
ms1.Close();
byte[] datalang = ms1.ToArray();
ms1.Dispose();
Invoke((MethodInvoker)delegate
{
string lang = Encoding.Default.GetString(datalang);
string datall = Encoding.Default.GetString(data);
Translator t = new Translator();
t.SourceLanguage = lang;
t.SourceText = datall;
t.TargetLanguage = ReadLangRead();
lblStatus.Text = "Translating...";
t.Translate();
txtLog.Text += System.Environment.NewLine + "Other Person: " + t.Translation;
});
}
}
}).Start();
}
public void ServerSend(byte[] message,string lang)
{
message = Encoding.Default.GetBytes(txtMessage.Text);
sck.Send(BitConverter.GetBytes(message.Length),0,4,0);
sck.Send(message);
//--------------------------------------------
byte[] langbyte;
langbyte = Encoding.Default.GetBytes(lang);
sck.Send(BitConverter.GetBytes(langbyte.Length),0,4,0);
sck.Send(langbyte);
}
和客户端代码
public void ClientSend(byte[] message, string lang)
{
message = Encoding.Default.GetBytes(txtMessage.Text);
sck.Send(BitConverter.GetBytes(message.Length), 0, 4, 0);
sck.Send(message);
//--------------------------------------------
byte[] langbyte;
langbyte = Encoding.Default.GetBytes(lang);
sck.Send(BitConverter.GetBytes(langbyte.Length), 0, 4, 0);
sck.Send(langbyte);
}
public void ClientStartReceiving()
{
MemoryStream lgst = new MemoryStream();
MemoryStream bfst = new MemoryStream();
new Thread(() =>
{
while (true)
{
byte[] bufferSize = new byte[4];
byte[] buffer;
byte[] langsize = new byte[4];
byte[] lang;
NetworkStream ns = new NetworkStream(sck);
ns.Read(bufferSize, 0, bufferSize.Length);
int datasize = (BitConverter.ToInt32(bufferSize, 0));
buffer = new byte[datasize];
int read = 0;
while (read != buffer.Length)
{
read += ns.Read(buffer, 0, buffer.Length);
}
bfst.Write(buffer,0,buffer.Length);
//------------------------------------
ns.Read(langsize, 0, langsize.Length);
int langsizeint = (BitConverter.ToInt32(langsize, 0));
lang = new byte[langsizeint];
int read1 = 0;
while (read1 != lang.Length)
{
read1 += ns.Read(lang, 0, lang.Length);
}
lgst.Write(lang, 0, lang.Length);
lgst.Close();
bfst.Close();
byte[] langau = lgst.ToArray();
byte[] dataau = bfst.ToArray();
lgst.Dispose();
bfst.Dispose();
Invoke((MethodInvoker)delegate
{
string langst = Encoding.Default.GetString(langau);
string datall = Encoding.Default.GetString(dataau);
Translator t = new Translator();
t.SourceLanguage = langst;
t.SourceText = datall;
t.TargetLanguage = ReadLangRead();
lblStatus.Text = "Translating...";
t.Translate();
txtLog.Text +=System.Environment.NewLine +"Other Person: "+ t.Translation;
});
}
}).Start();
}
public void ClientConnect()
{
Select_Partnener Form = new Select_Partnener();
sck.Connect(new IPEndPoint(IPAddress.Parse(Form.IP),1980));
}