首页 新闻 会员 周边 捐助

如何关闭TCPserver侦听 停止侦听

0
悬赏园豆:30 [已关闭问题] 关闭于 2011-09-08 18:04
 求助,这是我写的TCPserver,使用第一种方法,因为连接数不多,有一个按钮,
点击想达到侦听和停止侦听的功能,但程序停止侦听后,再次点击就有异常,
想问如何关闭侦听?这是我的代码,基本上是博客园的。

1 using System;
2  using System.Collections.Generic;
3  using System.ComponentModel;
4  using System.Data;
5  using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Net.Sockets;
9 using System.Net;
10 using System.Threading;
11 using System.Collections;
12
13
14 namespace ServerTCP
15 {
16 public partial class Form1 : Form
17 {
18
19 Socket listener;
20 Thread acceptThread;
21 Hashtable sockets = new Hashtable();//用于保存已连接的socket
22
23 public Form1()
24 {
25 InitializeComponent();
26 }
27
28 private void Form1_Load(object sender, EventArgs e)
29 {
30 IPHostEntry ipe = Dns.GetHostEntry(Dns.GetHostName());
31 IPAddress ipa = ipe.AddressList[0];
32 txtIP.Text = ipa.ToString();
33 txtPort.Text = "4000";
34 }
35
36 private void btnListen_Click(object sender, EventArgs e)
37 {
38 IPAddress ipa = IPAddress.Parse(txtIP.Text);
39 int port = int.Parse(txtPort.Text);
40 IPEndPoint LocalEP = new IPEndPoint(ipa, port);
41 acceptThread = new Thread(new ThreadStart(AcceptWorkThread));
42 //if (btnListen.Text == "开始监听")
43 //{
44 // listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
45 // listener.Bind(LocalEP);
46 // listener.Listen(100);
47
48 // acceptThread.Start();
49 // btnListen.Text = "停止监听";
50 //}
51 //else
52 //{
53 // btnListen.Text = "开始监听";
54 // foreach (DictionaryEntry dic in sockets)
55 // {
56 // Socket socket = (Socket)dic.Value;
57 // socket.Close();
58 // }
59 // listener.Close();
60
61 //}
62
63
64 }
65
66 private void AcceptWorkThread()
67 {
68 Thread.CurrentThread.IsBackground = true;
69 while (true)
70 {
71 Socket accept = listener.Accept();
72 IPEndPoint remoEP = (IPEndPoint)accept.RemoteEndPoint;
73 string remoIP = remoEP.ToString();
74 if (!sockets.Contains(remoIP))
75 {
76 sockets.Add(remoIP, accept);
77 }
78 this.Invoke((EventHandler)(delegate
79 {
80 lstClients.Items.Add(remoIP);
81 }));
82 Thread receiveThread = new Thread(new ParameterizedThreadStart(ReceiveWorkThread));
83 receiveThread.Start(accept);
84 }
85 }
86
87 private void ReceiveWorkThread(Object obj)
88 {
89 Thread.CurrentThread.IsBackground = true;
90 Socket socket = (Socket)obj;
91 byte[] buffer = new byte[1024];
92 while (true)
93 {
94 try
95 {
96 int recvCount = socket.Receive(buffer);
97 if (recvCount > 0)
98 {
99 IPEndPoint remoEP = (IPEndPoint)socket.RemoteEndPoint;
100 string recv = remoEP.ToString() + "发送来的消息:" + Encoding.Default.GetString(buffer, 0, recvCount);
101 this.Invoke((EventHandler)(delegate
102 {
103 txtGet.AppendText(recv + "\r\n");
104 }));
105 socket.Send(buffer, SocketFlags.None);
106 }
107 else//==0
108 {
109 IPEndPoint remoEP = (IPEndPoint)socket.RemoteEndPoint;
110 string remo = remoEP.ToString();
111 this.Invoke((EventHandler)(delegate
112 {
113 lstClients.Items.Remove(remo);
114 }));
115 if (sockets.Contains(socket))
116 {
117 sockets.Remove(remo);
118 }
119
120 socket.Close();
121 break;
122 }
123 }
124 catch
125 {
126 IPEndPoint remoEP = (IPEndPoint)socket.RemoteEndPoint;
127 string remo = remoEP.ToString();
128 this.Invoke((EventHandler)(delegate
129 {
130 lstClients.Items.Remove(remo);
131 }));
132 if (sockets.Contains(socket))
133 {
134 sockets.Remove(remo);
135 }
136 socket.Close();
137 break;
138 }
139 }
140
141 }
142
143 private void btnSnd_Click(object sender, EventArgs e)
144 {
145 if (lstClients.SelectedIndex == -1 || txtSend.Text == "")
146 {
147 MessageBox.Show("选择客户端和内容不能为空");
148 return;
149 }
150 string clients = lstClients.SelectedItem.ToString();
151 Socket socket = (Socket)sockets[clients];
152 byte[] buffer = Encoding.Default.GetBytes(txtSend.Text);
153 socket.Send(buffer, SocketFlags.None);
154 }
155 }
156 }
dkkkk的主页 dkkkk | 初学一级 | 园豆:200
提问于:2011-08-28 09:55
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册