|
|
|
![]() |
|
Strumenti |
![]() |
#21 |
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
Grazie mille, topix, gentilissimo!
Ho visto che apri la pipe con "r" mentre io la apro con "rw". Potrebbe dipendere da questo? Faccio la prova... Codice:
pipe = new RandomAccessFile("\\\\.\\pipe\\temp", "r"); |
![]() |
![]() |
![]() |
#22 |
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
No, mi dice giustamente "accesso negato" se tolgo l'attributo write.
|
![]() |
![]() |
![]() |
#23 |
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
anche così:
Codice:
//pipe.write ( echoText.getBytes("US-ASCII") ); //pipe.write ( echoText.getBytes("UTF-8") ); pipe.write ( echoText.getBytes("ASCII") ); ![]() Ultima modifica di Vincenzo1968 : 11-01-2013 alle 15:58. |
![]() |
![]() |
![]() |
#24 |
Member
Iscritto dal: Dec 2009
Messaggi: 98
|
Non capisco... non vorrei che dipenda poi da visual studio... io ti posso assicurare che funziona ti metto il link del mio progetto, il progetto in C l'ho sviluppato con Blodsheed dev-c++
ecco il link https://www.4shared.com/rar/DjNlpwwZ/FIFO.html? forse devi essere registrato per scaricarlo, ma comunque la registrazione è gratuita |
![]() |
![]() |
![]() |
#25 |
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
Ci siamo!
Bisognava inviare la stringa nel formato UTF-16 Little Endian(questo almeno su Windows; su Linux non lo so, debbo provare): Codice:
pipe.write ( echoText.getBytes("UTF-16LE") ); ![]() ![]() ![]() ![]() Il codice Java client completo : Codice:
import java.io.*; import java.util.*; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; /** * * @author VLC */ public class ClientPipeJava { public static void main(String[] args) { // TODO code application logic here try { // Connect to the pipe RandomAccessFile pipe = new RandomAccessFile("\\\\.\\pipe\\mynamedpipe", "rw"); //String echoText = "Hello word\n"; String echoText = null; echoText = "Hello word\n"; // write to pipe //pipe.write ( echoText.getBytes() ); //pipe.write ( echoText.getBytes("US-ASCII") ); //pipe.write ( echoText.getBytes("UTF-8") ); //pipe.write ( echoText.getBytes("UTF-16") ); //pipe.write ( echoText.getBytes("UTF-16BE") ); pipe.write ( echoText.getBytes("UTF-16LE") ); //pipe.write ( echoText.getBytes("ASCII") ); // read response String echoResponse = pipe.readLine(); System.out.println("Response: " + echoResponse ); pipe.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } Codice:
// Server.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <windows.h> #include <stdio.h> #include <tchar.h> #define BUFSIZE 4096 VOID InstanceThread(LPVOID); VOID GetAnswerToRequest(LPTSTR, LPTSTR, LPDWORD); int _tmain(int argc, _TCHAR* argv[]) { BOOL fConnected; DWORD dwThreadId; HANDLE hPipe, hThread; LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\mynamedpipe"); // The main loop creates an instance of the named pipe and // then waits for a client to connect to it. When the client // connects, a thread is created to handle communications // with that client, and the loop is repeated. for (;;) { hPipe = CreateNamedPipe( lpszPipename, // pipe name PIPE_ACCESS_DUPLEX, // read/write access PIPE_TYPE_MESSAGE | // message type pipe PIPE_READMODE_MESSAGE | // message-read mode PIPE_WAIT, // blocking mode PIPE_UNLIMITED_INSTANCES, // max. instances BUFSIZE, // output buffer size BUFSIZE, // input buffer size NMPWAIT_USE_DEFAULT_WAIT, // client time-out NULL); // default security attribute if (hPipe == INVALID_HANDLE_VALUE) { printf("CreatePipe failed"); return 0; } // Wait for the client to connect; if it succeeds, // the function returns a nonzero value. If the function // returns zero, GetLastError returns ERROR_PIPE_CONNECTED. fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED); if (fConnected) { // Create a thread for this client. hThread = CreateThread( NULL, // no security attribute 0, // default stack size (LPTHREAD_START_ROUTINE) InstanceThread, (LPVOID) hPipe, // thread parameter 0, // not suspended &dwThreadId); // returns thread ID if (hThread == NULL) { printf("CreateThread failed"); return 0; } else CloseHandle(hThread); } else // The client could not connect, so close the pipe. CloseHandle(hPipe); } return 1; } VOID InstanceThread(LPVOID lpvParam) { TCHAR chRequest[BUFSIZE]; TCHAR chReply[BUFSIZE]; DWORD cbBytesRead, cbReplyBytes, cbWritten; BOOL fSuccess; HANDLE hPipe; // The thread's parameter is a handle to a pipe instance. hPipe = (HANDLE) lpvParam; while (1) { // Read client requests from the pipe. fSuccess = ReadFile( hPipe, // handle to pipe chRequest, // buffer to receive data BUFSIZE*sizeof(TCHAR), // size of buffer &cbBytesRead, // number of bytes read NULL); // not overlapped I/O if (! fSuccess || cbBytesRead == 0) break; GetAnswerToRequest(chRequest, chReply, &cbReplyBytes); // Write the reply to the pipe. fSuccess = WriteFile( hPipe, // handle to pipe chReply, // buffer to write from cbReplyBytes, // number of bytes to write &cbWritten, // number of bytes written NULL); // not overlapped I/O if (! fSuccess || cbReplyBytes != cbWritten) break; } // Flush the pipe to allow the client to read the pipe's contents // before disconnecting. Then disconnect the pipe, and close the // handle to this pipe instance. FlushFileBuffers(hPipe); DisconnectNamedPipe(hPipe); CloseHandle(hPipe); } VOID GetAnswerToRequest(LPTSTR chRequest, LPTSTR chReply, LPDWORD pchBytes) { _tprintf( TEXT("%s\n"), chRequest ); lstrcpy( chReply, TEXT("Default answer from server") ); *pchBytes = (lstrlen(chReply)+1)*sizeof(TCHAR); } Ultima modifica di Vincenzo1968 : 11-01-2013 alle 16:51. |
![]() |
![]() |
![]() |
#26 | |
Bannato
Iscritto dal: Mar 2008
Città: Villabate(PA)
Messaggi: 2515
|
Quote:
Nel client Java che ho copiato dal link invece si scrive nella pipe per inviare i dati al server. E bisognava utilizzare la codifica UTF-16 Little Endian. Grazie mille per la tua disponibilità. ![]() |
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 19:21.