如何模拟套接字断开连接?
我的应用程序将文件上传到客户端的服务器,但他想要特殊的“暂停上传”功能。我不能简单地关闭连接,甚至不能终止进程 - 他需要失去连接,否则他的服务器应用程序会删除未完成的文件 - 所以我必须在代码“电缆拔出”中模拟 - 你有什么建议吗? 感谢您的帮助,并对我的英语表示抱歉:) 吉尔卡
My application uploads files to server of my client, but he want special "pause upload" function. I cant simply close connection, not even kill process - he need to lost connection otherwise his server application delete unfinished file - so i have to simulate in code "cable unplug" - do you have any suggestion?
thanks for your help and sorry for my english :)
jirka
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 Mockito 为您的设备创建
Socket
模拟测试,如这个问题所建议的:测试 Java 套接字You can use Mockito to create a
Socket
mock for your unit tests, as suggested on this question: Testing Java Sockets如果您同时编写了客户端和服务器,也许更好的选择是发送一些 OOB 数据 告诉服务器您正在暂停(或者一条消息告诉服务器不要在下一次套接字关闭时删除文件)。通常最好是明确的,而不是依赖某些操作的副作用 - 在您的情况下,关闭套接字而不明确 连接终止。
If you wrote both the client and the server, maybe the better option is to send some OOB data to tell the server that you are pausing (or a message to tell the server to not delete the file on next socket close). It is usually better to be explicit instead of relying on side effects of certain actions - in your case, closing a socket without explicit connection termination.