我定义了一个byte[256]来存储要发达和接收的数据
但在我的程序中,如果前一次发送的字符串比如是"我的世界"这个字符串
再在下一次发送字符串"我的"时候还是显示"我的世界"
只有当发送像"极品五笔"这样可以完全覆盖前面的数据的时候,才可以正确显示.
谁能帮我解决这个问题.
以下是我的代码
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
try
{
Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPAddress serverIP = IPAddress.Parse("127.0.0.1");
s.Connect(serverIP, 10000);
byte[] sendBytes = new byte[256];
if (s.Connected)
{
string sendString = "";
while (true)
{
if (sendString.Equals("exit"))
break;
sendString = Console.ReadLine();
sendBytes = Encoding.UTF8.GetBytes(sendString.ToCharArray());
try
{
s.Send(sendBytes, sendBytes.Length, 0);
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
}
}
s.Shutdown(SocketShutdown.Both);
s.Close();
}
else
{
Console.WriteLine("can not conn");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}namespace ConsoleApplication1
{
public class Processor
{
public Socket s;
public Processor(Socket so)
{
s = so;
}
public void ThreadProcess()
{
try
{
Console.WriteLine("a client has connected");
byte[] recvBytes = new byte[256];
int bytes = 0;
while (true)
{
if (s.Connected)
{
try
{
bytes = s.Receive(recvBytes, recvBytes.Length, 0);
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
}
string recvString = Encoding.UTF8.GetString(recvBytes);
Console.WriteLine(recvString);
}
if (bytes == 0)
break;
}
s.Shutdown(SocketShutdown.Both);
s.Close();
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
}
}
}
}
但在我的程序中,如果前一次发送的字符串比如是"我的世界"这个字符串
再在下一次发送字符串"我的"时候还是显示"我的世界"
只有当发送像"极品五笔"这样可以完全覆盖前面的数据的时候,才可以正确显示.
谁能帮我解决这个问题.
以下是我的代码
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
try
{
Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
IPAddress serverIP = IPAddress.Parse("127.0.0.1");
s.Connect(serverIP, 10000);
byte[] sendBytes = new byte[256];
if (s.Connected)
{
string sendString = "";
while (true)
{
if (sendString.Equals("exit"))
break;
sendString = Console.ReadLine();
sendBytes = Encoding.UTF8.GetBytes(sendString.ToCharArray());
try
{
s.Send(sendBytes, sendBytes.Length, 0);
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
}
}
s.Shutdown(SocketShutdown.Both);
s.Close();
}
else
{
Console.WriteLine("can not conn");
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}namespace ConsoleApplication1
{
public class Processor
{
public Socket s;
public Processor(Socket so)
{
s = so;
}
public void ThreadProcess()
{
try
{
Console.WriteLine("a client has connected");
byte[] recvBytes = new byte[256];
int bytes = 0;
while (true)
{
if (s.Connected)
{
try
{
bytes = s.Receive(recvBytes, recvBytes.Length, 0);
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
}
string recvString = Encoding.UTF8.GetString(recvBytes);
Console.WriteLine(recvString);
}
if (bytes == 0)
break;
}
s.Shutdown(SocketShutdown.Both);
s.Close();
}
catch (SocketException e)
{
Console.WriteLine(e.Message);
}
}
}
}
{
recvBytes = new byte[256];
bytes = s.Receive(recvBytes, recvBytes.Length, 0);
}
{
recvBytes = new byte[256];
bytes = s.Receive(recvBytes, recvBytes.Length, 0);
}
如果用这种方法,不是会造成系统的多余开消?
应该根据bytes的值来控制显示接收到的内容
如果说我一次要发送的内容超过byte[256]
是否这次的信息就只发送了一部分,而丢失了多余那部分了
开销是会有,但是很小,C#中new byte[256]这种操作可能比C还要快,它仅仅做指针的移动就够了。而C则要先找到足够大的空间。
string recvString = Encoding.UTF8.GetString(recvBytes, 0, bytes);
那改这句吧,没法调你这程序,再不行我也不知道什么问题了。