无论目标net5.0或net6.0框架,当尝试创建一个时,它在Ubuntu操作系统sslstream上运行时反复抛出错误,而在Windows操作系统上运行时,处理此错误的正确方法是什么?
System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception.
---> Interop+OpenSsl+SslException: SSL Handshake failed with OpenSSL error - SSL_ERROR_SSL.
---> Interop+Crypto+OpenSslCryptographicException: error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure
--- End of inner exception stack trace ---
at Interop.OpenSsl.DoSslHandshake(SafeSslHandle context, ReadOnlySpan`1 input, Byte[]& sendBuf, Int32& sendCount)
at System.Net.Security.SslStreamPal.HandshakeInternal(SafeFreeCredentials credential, SafeDeleteSslContext& context, ReadOnlySpan`1 inputBuffer, Byte[]& outputBuffer, SslAuthenticationOptions sslAuthenticationOptions)
--- End of inner exception stack trace ---
at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](TIOAdapter adapter, Boolean receiveFirst, Byte[] reAuthenticationData, Boolean isApm) …Run Code Online (Sandbox Code Playgroud) 客户将能够与Skype进行一对一和群组(温和的房间)聊天.
我将使用服务器来授权客户端
我的问题是哪个更好?
(WCF)或(TCPClient,StreamReader和StreamWriter)
cheesr
我有一个基于tcp的客户端 - 服务器应用程序.我能够发送和接收字符串,但不知道如何发送字节数组.
我正在使用以下函数从客户端向服务器发送字符串:
static void Send(string msg)
{
try
{
StreamWriter writer = new StreamWriter(client.GetStream());
writer.WriteLine(msg);
writer.Flush();
}
catch
{
}
}
Run Code Online (Sandbox Code Playgroud)
通讯实例
客户端发送字符串:
Send("CONNECTED| 84.56.32.14")
Run Code Online (Sandbox Code Playgroud)
Server收到一个字符串:
void clientConnection_ReceivedEvent(Connection client, String Message)
{
string[] cut = Message.Split('|');
switch (cut[0])
{
case "CONNECTED":
Invoke(new _AddClient(AddClient), client, null);
break;
case "STATUS":
Invoke(new _Status(Status), client, cut[1]);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
我需要一些帮助来修改上面的函数,以便除了字符串之外还发送和接收字节数组.我想打这样的电话:
Send("CONNECTED | 15.21.21.32", myByteArray);
Run Code Online (Sandbox Code Playgroud) 我知道这是一个经常被问到的问题,我没有明确的答案将std :: string或String ^转换为字节数组,以便写入流进行tcp通信.
这就是我尝试过的
bool CTcpCommunication::WriteBytes(const std::string& rdatastr)
{
bool retVal = false;
try
{
if (static_cast<NetworkStream^>(stream) != nullptr)
{
array<Byte>^data = System::Text::Encoding::ASCII->GetBytes(rdatastr);
stream->Write( data, 0, data->Length );
}
}
catch(Exception^)
{
// Ignore, just return false
}
return retVal;
}
Run Code Online (Sandbox Code Playgroud)
我知道这里GetBytes不会工作,我也检查了编组选项,将std:string转换为.NET String但是没有发现任何.有人帮我解决这个问题.
我写了一个类,试图读取服务器发送的字节数据,当我的应用程序结束时,我也希望循环结束,但是NetworkStream.Read()如果没有数据可用,它似乎等待.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.Threading.Tasks;
namespace Stream
{
class Program
{
private static TcpClient client = new TcpClient("127.0.0.1", 80);
private static NetworkStream stream = client.GetStream();
static void Main(string[] args)
{
var p = new Program();
p.Run();
}
void Run()
{
ThreadPool.QueueUserWorkItem(x =>
{
while (true)
{
stream.Read(new byte[64], 0, 64);
}
});
client.Close();
// insert Console.ReadLine(); if you want to see an exception occur.
}
} …Run Code Online (Sandbox Code Playgroud) 我有一个C#TcpClient来异步读取数据.简化代码如下:
public async Task StartReading(CancellationToken token)
{
// To fit the largest package
const int BufferSize = 524288;
while(true)
{
var buffer = new byte[BufferSize];
int bytesRead = await this.networkStream.ReadAsync(buffer, 0, BufferSize);
.WithWaitCancellation(token);
if (bytesRead > 0)
{
Console.Read(string.Fromat("{0} bytes received."));
// Run processing at background process
Task.Run(() => ProcessData(buffer, bytesRead));
}
else
{
// If received 0 bytes then a tcp connection is terminated
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
收到的包可能包含一个或多个以STX开头并以ETX结尾的消息,不会发送包长度.此Feed由第三方提供,不会更改.
在测试期间,似乎服务器以8192个块发送数据,因此消息可能被分成两个后续的块并且无法处理.
这是一个日志:
8192字节收到
消息89处理/ 1失败
8192字节收到
消息89处理/ 2失败
8192字节收到
消息89处理/ …
我有两个基本的控制台应用程序,通过网络进行通信,即使所有通信都在我的本地计算机上进行.
客户代码:
public static void Main()
{
while (true)
{
try
{
TcpClient client = new TcpClient();
client.Connect("127.0.0.1", 500);
Console.WriteLine("Connected.");
byte[] data = ASCIIEncoding.ASCII.GetBytes(new FeederRequest("test", TableType.Event).GetXmlRequest().ToString());
Console.WriteLine("Sending data.....");
using (var stream = client.GetStream())
{
stream.Write(data, 0, data.Length);
stream.Flush();
Console.WriteLine("Data sent.");
}
client.Close();
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.StackTrace);
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
服务器代码:
public static void Main()
{
try
{
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
Console.WriteLine("Starting TCP listener...");
TcpListener listener = new TcpListener(ipAddress, 500);
listener.Start();
Console.WriteLine("Server is …Run Code Online (Sandbox Code Playgroud) 我有几个使用相同代码运行的其他网络代码,我以前从未见过这个.接收缓冲区中的剩余字节正在填充\0\0\0\0\0\0\0\0\0\0\
我已经看过几次了,但从未有过答案.
TcpListener tlist = new TcpListener(IPAddress.Parse(ip.ToString()), 27275);
一旦有连接...
byte[] byData = new byte[textBox.Text.Length];
byData = System.Text.Encoding.UTF8.GetBytes(textBox.Text);
client.Send(byData, 0, byData.Length, 0);
Run Code Online (Sandbox Code Playgroud)
接收端:
byte[] bufferRec = new byte[56];
tc.Client.Receive(bufferRec, 0, 56, 0);
string output = Encoding.UTF8.GetString(bufferRec);
Run Code Online (Sandbox Code Playgroud)
我通常使用的缓冲区大小是256,我实际上很少发送/接收256字节的数据,但我从未看到那些剩余的字节被垃圾数据填充,仅在这种情况下.
编辑:我当然可以简单地解析垃圾数据,但由于没有理由,它仍然是奇怪的行为.
您好,感谢您的帮助。这次我想问一下TcpClient。我有一个服务器程序,正在编写一个客户端程序。该客户端使用TcpClient。首先创建一个新客户端
clientSocket=new TcpClient();
Run Code Online (Sandbox Code Playgroud)
(顺便说一句,这会导致异常吗?以防万一我将其放入try-catch中,但不确定是否确实必要)
clientSocket.Connect("xx.xx.xx.xx",port);
Run Code Online (Sandbox Code Playgroud)
然后我用创建一个NetworkStream
clientStream=clientSocket.GetStream();
Run Code Online (Sandbox Code Playgroud)
然后开始通过读取等待服务器中的数据。我知道这会阻止,所以我还设置了ReadTimeOut(比如说1秒)
无论如何,到目前为止一切都很好。稍后,如果我没有从服务器收到任何信息,则尝试向其发送信息。如果这种情况持续发生3次,我想关闭连接并再次重新连接到服务器
(请注意,一个完全不同的问题是服务器以某种方式关闭时,导致客户端中导致其他类型的错误的原因,也许稍后再问)
那么,我该怎么办?
if(clientSocket.Connected)
{
Console.WriteLine("Closing the socket");
clientSocket.Close();
}
Run Code Online (Sandbox Code Playgroud)
我关闭插座。循环已完成,因此我再次回到开头并尝试连接到服务器。
clientSocket.Connect("xx.xx.xx.xx",port);
Run Code Online (Sandbox Code Playgroud)
但这会导致错误(实际上是未处理的异常)“无法访问已处置的对象”
所以我的问题是 如何关闭并重新连接到服务器?
再次感谢任何帮助
我正在尝试创建一个函数,尝试在特定时间内异步连接到主机,然后检查是否已建立连接.
我的问题是我无法为此异步连接添加持续时间.
我的功能:
public async Task<bool> IsConnected()
{
// Host IP Address and communication port
string ipAddress = "192.168.0.11";
int port = 9100;
//Try to Connect with the host during 2 second
{
// Create TcpClient and try to connect
using (TcpClient client = new TcpClient())
{
Task<bool> mytask = client.ConnectAsync(ipAddress, port).Wait(TimeSpan.FromSeconds(2));
bool isconnected = await mytask;
if (isconnected)
{
//Connection with host
return true;
}
else
{
// No connection with host
return false;
}
//Close Connection
client.Close();
} …Run Code Online (Sandbox Code Playgroud)