大家帮我看看我的程序哪儿出错啦
在线等
服务端
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IPAddress serverIP = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(serverIP,10000);
listener.Start();
while (true)
{
Socket s = listener.AcceptSocket();
byte[] recvBytes = new byte[256];
int bytes = 0;
while (true)
{
bytes = s.Receive(recvBytes, recvBytes.Length,0);
string recvString = Encoding.ASCII.GetString(recvBytes);
Console.WriteLine();
if (bytes == 0)
break;
}
}
}
}
}
客户端
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
TcpClient client = new TcpClient();
IPAddress serverIP = IPAddress.Parse("127.0.0.1");
client.Connect(serverIP, 10000);
NetworkStream stream = client.GetStream();
string sendString = "hello";
byte[] sendBytes = Encoding.ASCII.GetBytes(sendString.ToCharArray());
stream.Write(sendBytes,0,sendBytes.Length);
}
}
}
改一下客户端也不行
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Socket s = new Socket(AddressFamily.InterNetwork,SocketType.stream,ProtocolType.Tcp);
IPAddress serverIP = IPAddress.Parse("127.0.0.1");
s.Connect(serverIP, 10000);
string sendString = "hello";
byte[] sendBytes = Encoding.ASCII.GetBytes(sendString.ToCharArray());
s.Send(sendBytes,sendBytes.Length,0);
}
}
}
在线等
服务端
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IPAddress serverIP = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(serverIP,10000);
listener.Start();
while (true)
{
Socket s = listener.AcceptSocket();
byte[] recvBytes = new byte[256];
int bytes = 0;
while (true)
{
bytes = s.Receive(recvBytes, recvBytes.Length,0);
string recvString = Encoding.ASCII.GetString(recvBytes);
Console.WriteLine();
if (bytes == 0)
break;
}
}
}
}
}
客户端
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
TcpClient client = new TcpClient();
IPAddress serverIP = IPAddress.Parse("127.0.0.1");
client.Connect(serverIP, 10000);
NetworkStream stream = client.GetStream();
string sendString = "hello";
byte[] sendBytes = Encoding.ASCII.GetBytes(sendString.ToCharArray());
stream.Write(sendBytes,0,sendBytes.Length);
}
}
}
改一下客户端也不行
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Socket s = new Socket(AddressFamily.InterNetwork,SocketType.stream,ProtocolType.Tcp);
IPAddress serverIP = IPAddress.Parse("127.0.0.1");
s.Connect(serverIP, 10000);
string sendString = "hello";
byte[] sendBytes = Encoding.ASCII.GetBytes(sendString.ToCharArray());
s.Send(sendBytes,sendBytes.Length,0);
}
}
}
解决方案 »
- 各位老大,急求:请求被中止: 连接被意外关闭
- C# Cpu 使用不端增加的问题
- winform 怎么让datatimepicker显示为空?
- (VS2008)我使用的report service,我在分组时实现了分页功能,但我同时想在每一个页面的下方将此页中的某一列中的值求和。并且将求和的结果,显示在其相应的页面中。不知怎么实现?
- 求救 数据库备份和还原
- 使用C#+rar.exe压缩文件,对于有空格名的文件无法压缩,求解。
- 问一下WIN8问题,求大礼回复
- 关于datagrid控件的问题!!求救!!
- CSDN中的人注意一下
- CSC编译时说找不到SYSTEM
- 在线求救,我有一个vb6做的控件,其中一个参数是Variant类型的,在C#中要将一个数据源付值进出,怎么付??
- 我想问问一个简单的问题
原来是while (true)
{
bytes = s.Receive(recvBytes, recvBytes.Length,0);
string recvString = Encoding.ASCII.GetString(recvBytes);
Console.WriteLine();
if (bytes == 0)
break;
}
出了问题
去掉while()
接收数据就OK啦
因为在下一次接收的时候,客户端的连接已关闭.服务端就不能检测连接了