using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000); Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
server.Bind((EndPoint)ipEndPoint);
server.Listen(1024);
// Connect the socket to the remote end point.
client.Connect(ipEndPoint);
Socket soc = server.Accept();
// Send some data to the remote device.
string data = "This is a string of data <EOF>";
byte[] buffer = Encoding.ASCII.GetBytes(data); int bytesTransferred = client.Send(buffer); // Write to the console the number of bytes transferred.
Console.WriteLine("{0} bytes were sent.\n", bytesTransferred);
// Release the socket.
client.Shutdown(SocketShutdown.Both); client.Disconnect(false);
if (client.Connected)
Console.WriteLine("We're still connnected");
else
Console.WriteLine("We're disconnected");
byte[] buff = new byte[1024];
while (true)
{
soc.Receive(buff);)//在这个地方不断的会读取相同的信息
Console.Write(Encoding.ASCII.GetString(buff));
}
Console.Read();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000); Socket client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
server.Bind((EndPoint)ipEndPoint);
server.Listen(1024);
// Connect the socket to the remote end point.
client.Connect(ipEndPoint);
Socket soc = server.Accept();
// Send some data to the remote device.
string data = "This is a string of data <EOF>";
byte[] buffer = Encoding.ASCII.GetBytes(data); int bytesTransferred = client.Send(buffer); // Write to the console the number of bytes transferred.
Console.WriteLine("{0} bytes were sent.\n", bytesTransferred);
// Release the socket.
client.Shutdown(SocketShutdown.Both); client.Disconnect(false);
if (client.Connected)
Console.WriteLine("We're still connnected");
else
Console.WriteLine("We're disconnected");
byte[] buff = new byte[1024];
while (true)
{
soc.Receive(buff);)//在这个地方不断的会读取相同的信息
Console.Write(Encoding.ASCII.GetString(buff));
}
Console.Read();
}
}
}
解决方案 »
- 在vs 2012里,textbox.textmode新增了不少属性,比如Email属性,怎么用?
- 有两个窗体前后顺序希望不变化,请前辈指点,小弟先感谢了!
- 如何将SqlServer数据库中的数据导入到Excel中
- asp.net datagrid checkbox 的全选问题
- 在看C#高级编程第二版,有个例子看不懂,请教一下
- 有关C#文件读写的问题
- c#与VB混合使用问题,在线等
- C#局域网如何与广域网通信的问题
- 关于C# winform大小变化时,其中各个控件的位置的问题!
- 如何在textbox中输入文章时,达到首行缩进的效果
- c#上传问题?我怎么提交不到服务器(代码在文章中)
- 求解!C#实现聊天工具中的好友列表。
{
soc.Receive(buff);
Console.Write(Encoding.ASCII.GetString(buff));
soc.Shutdown(SocketShutdown.Both);
soc.Close();
}
而soc在没有断开的情况下,是可以用来不断的接收的。我的问题是:当DisConnect以后,soc为什么不断的重复接收已经被接收的东西呢?????
这个程序会阻塞在soc.Receive(buffer)这里的呀,直到有新的消息发来为止
-------------------------------------------------------------------
本来就是如此 你用的是同步模式而非异步模式
{
soc.Receive(buff);
Console.Write(Encoding.ASCII.GetString(buff));
soc.Shutdown(SocketShutdown.Both);
soc.Close();
}
----------------------------------------------------------------
按照你的这个东西的话,如果soc要Receive两次,那么soc = server.Accept();两次了吗
对于tcp的话,我只要连接一次就够了呀,
当发送方disconnect以后,soc.Connected为什么还是true呢?????????
我有时调用这个方法时,程序居然就阻塞在这个方法上不动了~~
否则:好像另一方的socket.connected还是true嘛???????????
那Close呢.
我有时写程序这样
soc=new Socket()....
..
..//作用完close
soc.Close();
soc=null;//这样不知道有没有内存问题...因为我可能还要再使用一次soc对象//于是我再
soc=new Socket()...//不知道如何换成Disconnect是不是有区别