명명 된 파이프 서버와 그에 연결된 클라이언트를 만드는 약간의 지침을 작성했습니다. 서버에 데이터를 보낼 수 있으며 서버가 성공적으로 데이터를 읽습니다.하나의 Named Pipe 클라이언트로 읽고 쓸 수 있습니까?
내가해야 할 일은 서버에서 메시지를받는 것이므로 들어오는 데이터를 생성하고 기다리는 또 다른 스레드가 있습니다.
문제는 스레드가 들어오는 데이터를 기다리는 동안 앉아있는 동안 파이프가 이제 데이터 검사에 묶여 있다고 가정 할 때 WriteLine
호출에서 응답하지 않을 때 더 이상 서버에 메시지를 보낼 수 없다는 것입니다.
그래서 제가 제대로 접근하지 못하고 있습니까? 또는 이와 같이 사용하지 않는 명명 된 파이프입니까? 네임드 파이프에서 본 예는 클라이언트의 전송과 서버의 수신 방향을 나타내는 것입니다. 파이프의 방향을 In
, Out
또는 둘 다 지정할 수 있습니다.
도움, 조언 또는 제안을 보내 주시면 감사하겠습니다.
Heres는 '지금까지 코드 :
// Variable declarations
NamedPipeClientStream pipeClient;
StreamWriter swClient;
Thread messageReadThread;
bool listeningStopRequested = false;
// Client connect
public void Connect(string pipeName, string serverName = ".")
{
if (pipeClient == null)
{
pipeClient = new NamedPipeClientStream(serverName, pipeName, PipeDirection.InOut);
pipeClient.Connect();
swClient = new StreamWriter(pipeClient);
swClient.AutoFlush = true;
}
StartServerThread();
}
// Client send message
public void SendMessage(string msg)
{
if (swClient != null && pipeClient != null && pipeClient.IsConnected)
{
swClient.WriteLine(msg);
BeginListening();
}
}
// Client wait for incoming data
public void StartServerThread()
{
listeningStopRequested = false;
messageReadThread = new Thread(new ThreadStart(BeginListening));
messageReadThread.IsBackground = true;
messageReadThread.Start();
}
public void BeginListening()
{
string currentAction = "waiting for incoming messages";
try
{
using (StreamReader sr = new StreamReader(pipeClient))
{
while (!listeningStopRequested && pipeClient.IsConnected)
{
string line;
while ((line = sr.ReadLine()) != null)
{
RaiseNewMessageEvent(line);
LogInfo("Message received: {0}", line);
}
}
}
LogInfo("Client disconnected");
RaiseDisconnectedEvent("Manual disconnection");
}
// Catch the IOException that is raised if the pipe is
// broken or disconnected.
catch (IOException e)
{
string error = "Connection terminated unexpectedly: " + e.Message;
LogError(currentAction, error);
RaiseDisconnectedEvent(error);
}
}
물론 비동기가 필요하지 않는 한 가능합니다. 그건 참 독립입니다. 읽기와 쓰기 용으로 2 개의 파이프를 사용하는 것은 의미심장합니다. –
약간의 암호가 들리네요. 그래서 나는 언제든지 한 가지 행동에 대해서만 사용할 수있는 것처럼 독서 용 파이프와 글쓰기 용 파이프가 있어야한다고 말하고있는 것 같습니까? 그렇다면 의미가 있습니다. – w69rdy