Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Cross-process Communication of named pipes in java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shares with you the content of an example analysis of named pipe cross-process communication in java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Client code:

# include "stdafx.h" # include # include # include int main (int argc, _ TCHAR* argv []) {srand (time (NULL)); DWORD wlen = 0; Sleep (1000); / / wait for pipe to be created successfully! BOOL bRet = WaitNamedPipe (TEXT ("\.\\ Pipe\\ mypipe"), NMPWAIT_WAIT_FOREVER); if (! bRet) {printf ("connect the namedPipe failed!\ n"); return 0 } HANDLE hPipe = CreateFile (/ / pipe belongs to a special file TEXT ("\.\\ Pipe\\ mypipe"), / / the file name created is GENERIC_READ | GENERIC_WRITE, / / file mode 0, / / whether to share NULL / / pointer to a SECURITY_ATTRIBUTES structure OPEN_EXISTING, / / create parameter FILE_ATTRIBUTE_NORMAL, / / file attribute (hidden, read-only) NORMAL is the default attribute NULL) / / handle to the template creation file if (INVALID_HANDLE_VALUE = = hPipe) {printf ("open the exit pipe failed!\ n");} else {while (true) {char buf [256] = "; sprintf (buf,"% s% d ", buf,rand () 00) If (WriteFile (hPipe,buf,sizeof (buf), & wlen,0) = = FALSE) / / send content {printf ("write to pipe failed!\ n") to the server; break } else {printf ("To Server: data =% s, size =% d\ n", buf, wlen); char rbuf [256] = ""; DWORD rlen = 0; ReadFile (hPipe, rbuf, sizeof (rbuf), & rlen, 0) / / accept the content sent by the service printf ("From Server: data =% s, size =% d\ n", rbuf, rlen);} Sleep (1000);} CloseHandle (hPipe); / / close the pipe} system ("pause"); return 0;}

Server code:

# include "stdafx.h" # include # include # include int main (int argc, _ TCHAR* argv []) {srand (time (NULL)); char buf [256] = ""; DWORD rlen = 0 HANDLE hPipe = CreateNamedPipe (TEXT ("\.\\ Pipe\\ mypipe"), / / pipe name PIPE_ACCESS_DUPLEX, / / pipe type PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, / / pipe parameter PIPE_UNLIMITED_INSTANCES / / the maximum number of instances that can be created by the pipeline is 0, / / output buffer length 0 indicates default 0, and / / input buffer length 0 indicates default NMPWAIT_WAIT_FOREVER / / timeout NULL) / / specify a SECURITY_ATTRIBUTES structure, or pass a zero value if (INVALID_HANDLE_VALUE = = hPipe) {printf ("Create Pipe Error (% d)\ n", GetLastError ());} else {printf ("Waiting For Client Connection...\ n") If (! ConnectNamedPipe (hPipe, NULL)) / / blocks waiting for a client connection. {printf ("Connection failed!\ n");} else {printf ("Connection Success!\ n") } while (true) {if (! ReadFile (hPipe,buf,256,&rlen,NULL)) / / accept the content sent by the client {printf ("Read Data From Pipe Failed!\ n"); break } else {printf ("From Client: data =% s, size =% d\ n", buf, rlen); char wbuf [256] = ""; sprintf (wbuf, "% s% d", wbuf, rand () 00); DWORD wlen = 0 WriteFile (hPipe, wbuf, sizeof (wbuf), & wlen, 0); / / send content printf to the client ("To Client: data =% s, size =% d\ n", wbuf, wlen); Sleep (1000);} FlushFileBuffers (hPipe); DisconnectNamedPipe (hPipe); CloseHandle (hPipe) / / close the pipe} system ("pause"); return 0;} Thank you for reading! On the "java named pipe cross-process communication example analysis," this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report