我试图用C#编写一个最简单的多线程TCP服务器,它接收来自多个客户端的数据.
每次连接新客户端时,都会建立套接字连接,并将套接字作为参数传递给新的类函数,之后while循环运行并接收数据直到客户端连接.
这里的问题是"socket.receive"没有阻塞并且接收0字节的数据.因此循环连续运行,而不会阻塞socket.receive(代码中的"clientSocket.Receive(bb)".).
我使用Chrome浏览器作为客户端进行测试.即使我使用任何其他客户端,TCP服务器的行为仍然是相同的.
客户端仅发送一次数据,但服务器连续接收0字节,而循环继续运行.
我粘贴服务器输出以供参考.
请帮助阻塞socket.receive上的服务器以等待下一次客户端传输.奇怪的是,即使接收到0个字节,也不会调用异常.
请帮忙.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
Threading;
using System.Timers;
using System.Security;
using System.Security.Permissions;
namespace SERVER
{
static class Constants
{
public const int port = 8080;
public const int buffer_size = 512;
}
class Program
{
static public string LocalIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString(); …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个输入字段,其中只允许数字键。为此,我已成功在表单中实现了仅数字验证。
但这里的要求是,除了数字键之外,其他键都不能工作。为此,我已经绑定了实现 @HostListener
在这种情况下,当我们单击字母键时,它不会显示在输入字段中,但会分配该字母的值。
请检查代码:HostListener代码:
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
import { NgControl } from '@angular/forms';
@Directive({
selector: 'input[numbersOnly]'
})
export class NumberDirective {
constructor(private _el: ElementRef) { }
@HostListener('input', ['$event']) onInputChange(event) {
const initalValue = this._el.nativeElement.value;
this._el.nativeElement.value = initalValue.replace(/[^0-9]*/g, '');
if ( initalValue !== this._el.nativeElement.value) {
event.stopPropagation();
}
}
}
Run Code Online (Sandbox Code Playgroud)
HTML:
Type anything other than numbers, the change is fired even if the textfield content does not change :
<br/>
<input type="text" [(ngModel)]="value" (ngModelChange)="onChange($event)" numbersOnly/> …Run Code Online (Sandbox Code Playgroud) 我正在编写代码(Python和wxpython for GUI),它将在Raspberry PI上的Debian OS上运行.我想保护/隐藏源代码.有什么办法吗?可能是py2exe,还是将它转换为库或其他东西?
我正在尝试在控制台和 HTML 中打印获取嵌套表单的表单控件的值。
userForm = new FormGroup({
name: new FormControl("Tom Alter"),
address: new FormGroup({
Country: new FormControl("India"),
State: new FormControl("Delhi")
})
});
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我都可以使用 get 语句找到值。
console.log ("name : ", this.userForm.controls.name.value);
console.log ("Country using Get Way 1 : ", this.userForm.get(['address', 'Country']).value) ;
console.log ("Country using Get Way 2 : ", this.userForm.get(['address']).get(['Country']).value);
console.log ("Country using Get Way 3 : ", this.userForm.get(['address.Country']).value);
console.log ("Country without Get: ", this.userForm.group.address.controls.cCountry.value);
Run Code Online (Sandbox Code Playgroud)
在这些“名称”中,“Way1”、“Way2”正在工作,但是“Way 3”和“Without get”不起作用,因为它适用于“name”
同样在 HTML 中:
Name : {{userForm.controls.name.value}}
<br>
<br>
Country with get Way …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用bcrypt将用户密码与存储的密码进行比较。但是Express给出错误是我在bcrypt.compare之前使用await
这是代码:
app.post ('/users/login', (req, res) => {
const user = users.find(user=> user.name === req.body.user)
if (user == null) {
return res.status(400).send('Can Not find user');
} else {
try{
if ( await bcrypt.compare(req.body.password, user.password)) {
res.send("Success");
} else {
res.send("Incorrect PAssword");
}
} catch {
return res.status(500).send('Some Error has occurred');
}
}
});
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
C:\Data\Ashish\projects\jwtAuthentication\app.js:32
if ( await bcrypt.compare(req.body.password, user.password)) {
^^^^^^
SyntaxError: Unexpected identifier
at Module._compile (internal/modules/cjs/loader.js:723:23)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3) …Run Code Online (Sandbox Code Playgroud)