C#的网络编程中,TcpListener和Socket功能一样吗?

我觉得反正都是进行监听,至少差不多吧!一样大家讲讲自己的理解,说说不同!谢谢了。... 我觉得反正都是进行监听,至少差不多吧!一样大家讲讲自己的理解,说说不同!谢谢了。 展开
 我来答
135ltyq020
2011-08-25 · TA获得超过705个赞
知道答主
回答量:486
采纳率:0%
帮助的人:276万
展开全部
1、叙述有点问题:客户端必须先尝试连接,并且不能操作员手工连接,因为不知道服务器的状态,所以客户端有一个定时连接尝试的timer,当然并非倒计时的Timer。
另外一个问题:数据包可能丢失,所以服务器告诉客户端每次-1的做法不可靠,应该是发给客户端当前的秒数。
2、代码:卖过几千元钱的啊
(1) 客户端:
public partial class Form1 : Form
{
Socket clientSock ;//连接服务器的客户端Socket
IPAddress serverIp = IPAddress.Parse("127.0.0.1");
int port = 8000;
Thread thread;//接收数据的线程

delegate void SetReceivedToLabelCallBack(string msg,bool isSeconds);//数据接收线程到窗体的跨线程调用委托

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
try
{
linkToServer();//连接服务器
}
catch (Exception ec)
{
MessageBox.Show(ec.Message+",每隔1秒会自动再试");
timerTryLinkToServer.Enabled = true;
}
}

private void timerTryLinkToServer_Tick(object sender, EventArgs e)
{
try
{
linkToServer();
timerTryLinkToServer.Enabled = false;
}
catch //不再提示,否则每秒一次,够烦
}

private void linkToServer()
{
try
{
IPEndPoint serverRemote = new IPEndPoint(serverIp, port);
clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSock.Connect(serverRemote);

thread = new Thread(new ThreadStart(receiveThread));
thread.Start();
MessageBox.Show("linked");
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}

private void receiveThread()
{
try
{
while (true)
{
byte[] buffer = new byte[8];
int receivedBytesCount=clientSock.Receive(buffer);
string receivedStr = System.Text.Encoding.Unicode.GetString(buffer);
if (receivedBytesCount == 4)//发来的是4字节int数据
{
DisplayReceivedMsg(receivedStr,true);
}
else//发来的是字符串“1”,非4字节
{
DisplayReceivedMsg(receivedStr,false);
}
}
}
catch(Exception ec)
{
MessageBox.Show(ec.Message);
}
}

public void DisplayReceivedMsg(string msg,bool isSeconds)
{
Label label=null;
if (isSeconds)
{
label = label1;
}
else
{
label = label2;
}

if (label.InvokeRequired)//如果是跨线程调用
{
SetReceivedToLabelCallBack d = new SetReceivedToLabelCallBack(DisplayReceivedMsg);
this.Invoke(d, new object[] );
}
else
{
label.Text = msg;
}
}
//好习惯,关闭套接字
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (thread != null)
{
thread.Abort();
thread = null;
}
if (clientSock != null)
{
clientSock.Close();
clientSock = null;
}
}

}
(2)服务器端:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

IPAddress serverIp = IPAddress.Parse("127.0.0.1");
int port = 8000;
Socket listenSock;

ArrayList clientSockets = new ArrayList();//保存客户端连接的Socket
System.Timers.Timer timer = new System.Timers.Timer();

int seconds = 0;
bool blockSocket=false;//是否停止监听(不接受客户)
Thread thListen;//监听线程

private void Form1_Load(object sender, EventArgs e)
{
IPEndPoint local = new IPEndPoint(serverIp, port);
listenSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listenSock.Bind(local);
listenSock.Listen(50);

if (listenStart())
{
timer.Interval = 1000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
}
}

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
seconds++;
if (seconds==5)
{
listenStop();
MessageBox.Show("Stop");
return;
}
if(seconds==45)
{
if (listenStart())
MessageBox.Show("Start");
else MessageBox.Show("startError");

send("1");
seconds = 0;
return;
}
if(seconds<45 && seconds>5)
{
send((45 - seconds).ToString());
}
}

private void listenStop()
{
try
{
blockSocket = true;
if (thListen != null)
{
thListen.Abort();
thListen = null;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}

private bool listenStart()
{
try
{
thListen = new Thread(new ThreadStart(listenThread));
thListen.Start();
return true;
}
catch (Exception ec)
{
MessageBox.Show(ec.Message);
return false;
}
}

private void listenThread()
{
try
{
while (!blockSocket)
{
Socket sock = listenSock.Accept();
if (clientSockets.IndexOf(sock) < 0)
{
clientSockets.Add(sock);
MessageBox.Show("built one");
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}

private void send(string msg)
{
foreach (Socket sock in clientSockets)
{
try
{
byte[] buffer = System.Text.Encoding.Unicode.GetBytes(msg);
sock.SendTimeout = 500;
sock.Send(buffer);
MessageBox.Show("send "+msg);
}
catch (Exception ec)
{
MessageBox.Show(ec.Message);
}
}
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (thListen!= null)
{
thListen.Abort();
thListen = null;
}
if (listenSock != null)
{
listenSock.Close();
listenSock = null;
}
for (int i = 0; i < clientSockets.Count; i++)
{
Socket sock = (Socket)clientSockets[i];
sock.Close();
}
}
}
****************************
楼主没有诚意,这么长时间还不结帖啊。
hb1122
2011-08-25 · TA获得超过473个赞
知道小有建树答主
回答量:424
采纳率:50%
帮助的人:213万
展开全部
TcpListener,是封装的Socket,Listen()
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式