我试图重复使用正常的代码
TcpClient oC = new TcpClient(ip, port);
oC = new TcpClient(ip, port);
StreamReader messageReader;
try {
messageReader = new StreamReader(oC.GetStream(), Encoding.ASCII);
reply = messageReader.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
同
try {
using (messageReader = new StreamReader(oC.GetStream(), Encoding.ASCII))
{
reply = messageReader.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
但我得到一个InvalidOperationException说法
非连接套接字上不允许该操作.
有什么问题,我该如何解决?
更多:我有oc.Connect在此代码之前,所以我连接,当它想要第一次使用是工作正常,只有在那之后,我得到了这个例外,我玩了一下它现在和现在我明白了:
无法访问已处置的对象.\ r \n对象名称:'System.Net.Sockets.Socket'.
我有一个用Ruby编写的TCPclient.我想在后台使用它.目前它有一个循环:
loop do
end
Run Code Online (Sandbox Code Playgroud)
所以我可以运行一个客户端,它永远有效.
有没有办法作为服务运行,并写一个pid文件,然后我将能够通过pid杀死该进程?
我正在创建简单的代理服务器,但遇到了一个奇怪的情况,我有以下代码:
var clientRequestStream = _tcpClient.GetStream();
var requestHeader = clientRequestStream.GetUtf8String();
Run Code Online (Sandbox Code Playgroud)
GetUtf8String是Stream读取流(包含HttpRequest标题)的类的扩展方法。我需要提取这些标头以访问主机和请求的 URL。完成读取 NetworkStream 后。我需要执行搜索操作并设置它,clientRequestStream.Position = 0;因为我必须读取该流并将其写入另一个远程NetworkStream.
我不知道我应该如何解决这个问题。任何建议都会有所帮助。
编辑:我也尝试将 NetworkStream 复制到 MemoryStream 然后对 MemoryStream 执行查找操作,没有例外,但是当我想从 NetworkStream 读取时,其缓冲区始终为空。
我也用反射器看看里面发生了什么Stream.CopyTo。见下面的代码:
private void InternalCopyTo(Stream destination, int bufferSize)
{
int num;
byte[] buffer = new byte[bufferSize];
while ((num = this.Read(buffer, 0, buffer.Length)) != 0)
{
destination.Write(buffer, 0, num);
}
}
Run Code Online (Sandbox Code Playgroud)
这就是 CopyTo 所做的。即使我使用CopyTo问题仍然没有解决。因为它将源(此处为 NetworkStream)读取到最后。我有另一种方法来处理这种情况吗?
我需要帮助为消息队列找到最好的和可能最快的数据结构。
架构如下:客户端从外部 C++ 服务器获取消息(不能更改它的代码)。msg.length + data 形式的消息。
在旧代码中,使用 TcpClient 类作为连接和 MemoryStream 作为队列。客户端从此 TCPClient 读取数据并将其复制到流中。如果在此读取期间客户端读取了整个消息,则它正在另一个线程中进行处理,并且一切都很完美。如果有部分消息或 2 条消息在一起,代码会变得非常混乱。
我有一种强烈的感觉,可能有一种简单的方法来编写代码。非常困扰我的是 MemoryStream 中的指针的“播放”以及需要以某种方式从中删除旧数据。
我用c语言编写了tcp连接的代码,在某些地方我添加了两个perrors:
perror("FAIL1: ...");
perror("FAIL2: ...");
Run Code Online (Sandbox Code Playgroud)
输出是:FAIL1:..:成功FAIL2:..:无效的论点只想了解 - "成功"是什么意思?TNX!
您好我正在尝试使用BinaryReader/BinaryWriter建立聊天,我陷入了死胡同,我无法弄清楚如何让我的服务器将消息发送给所有连接的客户端..
我已经尝试将所有客户端添加到列表中并在列表上运行foreach循环,以将消息发送到每个连接的客户端,但没有进行锻炼.
服务器:
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace server {
internal class Program {
public static int counter = 0;
//List<Client> clientList = new List <Client>();
private static readonly IPAddress sr_ipAddress = IPAddress.Parse("127.0.0.1");
public TcpListener Listener = new TcpListener(sr_ipAddress, 8888);
public static void Main(string[] args) {
Program server = new Program();
server.Start();
Console.ReadKey();
}
public void Start() {
Listener.Start();
Console.WriteLine("Server started");
StartAccept();
}
private void StartAccept() {
Listener.BeginAcceptTcpClient(HandleAsyncConnection, Listener);
}
public void HandleAsyncConnection(IAsyncResult res) …Run Code Online (Sandbox Code Playgroud) 我需要一个简单的函数,它将 TcpClient 作为参数。假设TcpClient已连接到服务器。该函数应返回客户端设备用于与服务器设备连接的 IP。不是已建立连接的服务器设备的 IP,而是用于与服务器连接的客户端设备的 IP。由于一台设备可以有多个 IP 地址,我想要与服务器连接的特定 IP。
当我运行这个程序时,Client 类会提示用户输入一个命令,该命令应该转到 Server 类并打开一个文件并读取该文件的每一行并将字节长度返回给 Client 类以进行显示。
不幸的是,一旦我输入命令,就没有任何反应并且不知道为什么。
TCP客户端类
package TcpClient;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import java.io.*;
public class TcpClient {
public static void main(String[] args) {
String temp;
String displayBytes;
try {
//create input stream
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
//create client socket, connect to server
Socket clientSocket = new Socket("localhost",5555);
//create output stream attached to socket
DataOutputStream outToServer =
new DataOutputStream(clientSocket.getOutputStream());
System.out.print("Command : ");
//create input stream attached to socket
BufferedReader inFromServer =
new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); …Run Code Online (Sandbox Code Playgroud) 我的场景是我有一百个小文本文件,我想加载、解析和存储在 DLL 中。DLL 的客户端是瞬态的(命令行程序),我不想在每次命令行调用时重新加载数据。
所以,我想我会写一个 Windows 服务器来存储数据并让客户端使用 TCP 查询服务器。但是,TCP 性能确实很慢。我编写了以下代码Stopwatch用于测量套接字设置时间。
// time the TCP interaction to see where the time goes
var stopwatch = new Stopwatch();
stopwatch.Start();
// create and connect socket to remote host
client = new TcpClient (hostname, hostport); // auto-connects to server
Console.WriteLine ("Connected to {0}",hostname);
// get a stream handle from the connected client
netstream = client.GetStream();
// send the command to the far end
netstream.Write(sendbuf, 0, sendbuf.Length);
Console.WriteLine ("Sent command to far end: …Run Code Online (Sandbox Code Playgroud) 目前,我正在尝试以更快的方式通过单个连接发送许多原始请求。我使用 HTTPClient lib 实现了这一点,但由于需要发送原始字节,我使用的是 TCPClient。
此应用程序的一些必要条件:
我有什么代码:
发送所有请求的方法
private static async Task SendAllRequests(Dictionary<int, string> requestsDictionary)
{
var client = new TcpClient();
var ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
await client.ConnectAsync("localhost", 44392);
SslStream sslStream = new SslStream(
client.GetStream(),
false,
null
);
await sslStream.AuthenticateAsClientAsync("localhost");
var tasks = new List<Task<KeyValuePair<int, SslStream>>>();
foreach (var k in requestsDictionary.Keys)
{
tasks.Add(SendStreamAsync(requestString, sslStream));
}
var requestsInfo = await Task.WhenAll(tasks);
}
Run Code Online (Sandbox Code Playgroud)
发送和读取单个请求字节的方法
byte[] buffer = new byte[2048];
int bytes;
byte[] request = Encoding.UTF8.GetBytes(requestString);
await sslStream.WriteAsync(request, 0, request.Length); …Run Code Online (Sandbox Code Playgroud) tcpclient ×10
c# ×7
.net ×3
sockets ×3
tcp ×3
background ×1
binaryreader ×1
binarywriter ×1
c ×1
daemon ×1
debugging ×1
java ×1
linux ×1
performance ×1
ruby ×1
service ×1
task ×1
tcpserver ×1
unix ×1