class Program
{
static void Main(string[] args)
{ try
{//创建服务端
TcpListener tcpListener = new TcpListener(System.Net.IPAddress.Parse("127.0.0.1"), 8686);
tcpListener.Start();
while (true)
{
TcpClient client = tcpListener.AcceptTcpClient();
StreamReader sr = new StreamReader(client.GetStream());
NetworkStream ns = client.GetStream();
StreamWriter sw = new StreamWriter(ns);
sw.Write("I hava receive");
Console.WriteLine(sr.ReadLine());
sw.Flush();
}
}
catch (Exception e){
//创建客户端
TcpClient tcpClinet = new TcpClient();
while (true)
{
tcpClinet.Connect("127.0.0.1", 8686);
StreamReader sr = new StreamReader(tcpClinet.GetStream());
NetworkStream ns = tcpClinet.GetStream();
StreamWriter sw = new StreamWriter(ns);
sw.Write(Console.ReadLine());
sw.Flush();
if (sr.ReadLine().Length != 0)
{
Console.WriteLine(sr.ReadLine());
} ns.Close();
}
}
}
}
大神们帮我看一下这个程序为什么服务端只能接收客户消息,却不能发送消息
{
static void Main(string[] args)
{ try
{//创建服务端
TcpListener tcpListener = new TcpListener(System.Net.IPAddress.Parse("127.0.0.1"), 8686);
tcpListener.Start();
while (true)
{
TcpClient client = tcpListener.AcceptTcpClient();
StreamReader sr = new StreamReader(client.GetStream());
NetworkStream ns = client.GetStream();
StreamWriter sw = new StreamWriter(ns);
sw.Write("I hava receive");
Console.WriteLine(sr.ReadLine());
sw.Flush();
}
}
catch (Exception e){
//创建客户端
TcpClient tcpClinet = new TcpClient();
while (true)
{
tcpClinet.Connect("127.0.0.1", 8686);
StreamReader sr = new StreamReader(tcpClinet.GetStream());
NetworkStream ns = tcpClinet.GetStream();
StreamWriter sw = new StreamWriter(ns);
sw.Write(Console.ReadLine());
sw.Flush();
if (sr.ReadLine().Length != 0)
{
Console.WriteLine(sr.ReadLine());
} ns.Close();
}
}
}
}
大神们帮我看一下这个程序为什么服务端只能接收客户消息,却不能发送消息
解决方案 »
- bin下的Debug文件全没了该怎么办
- |WPF| Grid如何让一行自适应,有相应的XAML源码 谢谢
- 编译出错的问题
- 怎样实现在datagrid里任意位置插入一行
- 在winform中如何获得DataGrid某一单元格的值
- 请问用鼠标点击DataGrid(Winform)某一个单元格如何得到单元格的值
- Winform布局问题
- c# emgucv 获取外部摄像头(ip camera)
- System.Runtime.Remoting.Channels.Tcp 这个名称空间怎么找不到?
- C#中如何将一个*.ocx嵌入其中,因为属性方法都没有了
- 难道反射建立的对象与对象本身不完全一样?
- dataGridView1 汇总行如何实现
这句不能写在循环里,客户端每次循环都去创建一个新连接当然就收不到服务端返回的消息了