Coder Social home page Coder Social logo

bombplane's People

Contributors

rosenberg37 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

kitori22

bombplane's Issues

不同终端之间可以建立TCP连接但无法进行交互的问题

private void Listen(object obj) //建立与客户端的连接
{
	CancellationToken ct = (CancellationToken)obj;
	while (!ct.IsCancellationRequested) /*!此处问题!*/
	{
		try
		{
			......//省略
		}
		catch (SocketException) { }
	}
}

在上面这个函数中,使用了while (!ct.IsCancellationRequested)来判断线程是否被取消。(Server.cs行82)

public int ListenPort
{
	get { return _listenPort; }
	set
	{
		try
		{
			if (listenThread != null)
				listenCTS.Cancel();  /*!此处问题!*/

			......//省略

			//开启线程Accept进行通信的客户端socket
			listenThread = new Thread(Listen)
			{
				IsBackground = true    //运行线程在后台执行
			};   //线程绑定Listen函数
			listenThread.Start(listenCTS.Token);  /*!此处问题!*/    //Start里面的参数是Listen函数所需要的参数 

			_listenPort = value;
		}
		catch (SocketException)
		{
			MessageBox.Show("监听程序启动失败,请尝试重新设置监听端口");
		}
	}
}

但在设置ListenPort属性的时候,调用了listenCTS.Cancel()方法(Server.cs行50),导致ct.IsCancellationRequested变为true。
之后在创建新线程时,仍然使用了旧的listenCTS提供Token(Server.cs行67),导致Listen函数的while循环永远不会执行。
应该在ListenPort属性的set方法的中,调用listenCTS.Cancel()方法后,重新创建一个新的CancellationTokenSource对象,以便生成一个新的未取消的token。

public class Server
{
	......//省略
	private readonly CancellationTokenSource? listenCTS;
	......//省略
}

但在原代码中,使用了readonly修饰listenCTS(Server.cs行38),因此我不确定将 readonly 删去,之后再粗暴地添加 listenCTS.Dispose()listenCTS = new CancellationTokenSource() 是不是一个好的做法,不过确实是可行的。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.