Named Pipes

Ok so I have some code on named pipes. The client code and server code are both below. I ran/start the server program on computer B and the client program on computer A (If I am not mistaken this should let me write stuff from computer A and have it show up on the program box on computer B) but when I started the client program I got error code 1326 and I have no idea why.

Also the ***.***.*.* in the client code is the IPv4 address of computer B

Thankyou in advanced for all the answers



Client code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "stdafx.h"
#include "windows.h"
#include <iostream>

#define g_szPipeName "\\\\***.***.*.*\\pipe\\pipename"  //Name given to the pipe
//Pipe name format - \\servername\pipe\pipename
//This pipe is for server on the same computer, however, pipes can be used to
//connect to a remote server

#define BUFFER_SIZE 1024 //1k
#define ACK_MESG_RECV "Message received successfully"

HANDLE hPipe;

int repeate() {
	char szBuffer[BUFFER_SIZE];

	printf("\nEnter a message to be sent to the server: ");
	gets(szBuffer);

	DWORD cbBytes;

	//Send the message to server
	BOOL bResult = WriteFile(
		hPipe,                // handle to pipe 
		szBuffer,             // buffer to write from 
		strlen(szBuffer) + 1,   // number of bytes to write, include the NULL
		&cbBytes,             // number of bytes written 
		NULL);                // not overlapped I/O 

	if ((!bResult) || (strlen(szBuffer) + 1 != cbBytes))
	{
		printf("\nError occurred while writing to the server: %d", GetLastError());
		CloseHandle(hPipe);
		system("Pause");
		return 1;  //Error
	}
	else
	{
		printf("\nWriteFile() was successful.");
	}

	//Read server response
	bResult = ReadFile(
		hPipe,                // handle to pipe 
		szBuffer,             // buffer to receive data 
		sizeof(szBuffer),     // size of buffer 
		&cbBytes,             // number of bytes read 
		NULL);                // not overlapped I/O 

	if ((!bResult) || (0 == cbBytes))
	{
		printf("\nError occurred while reading from the server: %d", GetLastError());
		CloseHandle(hPipe);
		system("Pause");
		return 1;  //Error
	}
	else
	{
		printf("\nReadFile() was successful.");
	}

	printf("\nServer sent the following message: %s", szBuffer);
	repeate();

}

int main(int argc, char* argv[])
{
     
     
     //Connect to the server pipe using CreateFile()
     hPipe = CreateFile( 
          g_szPipeName,   // pipe name 
          GENERIC_READ |  // read and write access 
          GENERIC_WRITE, 
          0,              // no sharing 
          NULL,           // default security attributes
          OPEN_EXISTING,  // opens existing pipe 
          0,              // default attributes 
          NULL);          // no template file 
     
     if (INVALID_HANDLE_VALUE == hPipe) 
     {
          printf("\nError occurred while connecting to the server: %d", GetLastError()); 
          //One might want to check whether the server pipe is busy
          //This sample will error out if the server pipe is busy
          //Read on ERROR_PIPE_BUSY and WaitNamedPipe() for that
		  system("Pause");
          return 1;  //Error
     }
     else
     {
          printf("\nCreateFile() was successful.");
     }
     
     //We are done connecting to the server pipe, 
     //we can start communicating with the server using ReadFile()/WriteFile() 
     //on handle - hPipe
     
     
	 repeate(); 
}

Server code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "stdafx.h"
#include "windows.h"
#include <iostream> 

#define g_szPipeName "\\\\.\\pipe\\pipename"  //Name given to the pipe
//Pipe name format - \\.\pipe\pipename

#define BUFFER_SIZE 1024 //1k
#define ACK_MESG_RECV "Message received successfully"


HANDLE hPipe;

int repeate() {
	char szBuffer[BUFFER_SIZE];
	DWORD cbBytes;

	//We are connected to the client.
	//To communicate with the client we will use ReadFile()/WriteFile() 
	//on the pipe handle - hPipe

	//Read client message
	BOOL bResult = ReadFile(
		hPipe,                // handle to pipe 
		szBuffer,             // buffer to receive data 
		sizeof(szBuffer),     // size of buffer 
		&cbBytes,             // number of bytes read 
		NULL);                // not overlapped I/O 

	if ((!bResult) || (0 == cbBytes))
	{
		printf("\nError occurred while reading from the client: %d", GetLastError());
		CloseHandle(hPipe);
		system("Pause");
		return 1;  //Error
	}
	else
	{
		printf("\nReadFile() was successful.");
	}

	printf("\nClient sent the following message: %s", szBuffer);

	strcpy(szBuffer, ACK_MESG_RECV);

	//Reply to client
	bResult = WriteFile(
		hPipe,                // handle to pipe 
		szBuffer,             // buffer to write from 
		strlen(szBuffer) + 1,   // number of bytes to write, include the NULL 
		&cbBytes,             // number of bytes written 
		NULL);                // not overlapped I/O 

	if ((!bResult) || (strlen(szBuffer) + 1 != cbBytes))
	{
		printf("\nError occurred while writing to the client: %d", GetLastError());
		CloseHandle(hPipe);
		system("Pause");
		return 1;  //Error
	}
	else
	{
		printf("\nWriteFile() was successful.");
	}
	repeate();

}



int main(int argc, char* argv[])
{

	hPipe = CreateNamedPipe(
		g_szPipeName,             // 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  
		BUFFER_SIZE,              // output buffer size 
		BUFFER_SIZE,              // input buffer size 
		NMPWAIT_USE_DEFAULT_WAIT, // client time-out 
		NULL);                    // default security attribute  




	if (INVALID_HANDLE_VALUE == hPipe)
	{
		printf("\nError occurred while creating the pipe: %d", GetLastError());
		system("Pause");
		return 1;  //Error
	}
	else
	{
		printf("\nCreateNamedPipe() was successful.");
	}

	printf("\nWaiting for client connection...");

	//Wait for the client to connect
	BOOL bClientConnected = ConnectNamedPipe(hPipe, NULL);

	if (FALSE == bClientConnected)
	{
		printf("\nError occurred while connecting to the client: %d", GetLastError());
		CloseHandle(hPipe);
		system("Pause");
		return 1;  //Error
	}
	else
	{
		printf("\nConnectNamedPipe() was successful.");
	}


	repeate();

}

Last edited on
1326 means "Logon failure: unknown user or bad password"

The server side of a named pipe cannot use a NULL security descriptor. You must, instead, create an empty security descriptor.

EDIT: And please format your code. You've made a few posts so you should understand that by now.
Last edited on
You mind giving me an example of the empty security descriptor please?
I thought NULL would work fine. The programs work fine when both the server and client are on the same computer but fail when I put the server on Computer B and the client on Computer A. Is there something that I am also missing?
Bump to the top
You create an empty security attributes with:
1
2
3
4
5
6
SECURITY_DESCRIPTOR sd;
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);

SECURITY_ATTRIBUTES sa;
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = &sd;
Last edited on
EDIT now I am getting error 5 (Access denined I think?) when the client tries to connect to the server. (this is over a network) I have even tried starting both the files as admin but still get error 5 from the client. What could be causing this??
Last edited on
I don't have a Windows networked environment to check any of this on. But I did create this stuff for use on a large Windows network many years ago. So, I can't help you test any of this stuff, maybe someone else can. Maybe this stuff's changed with these later versions of NT.

Well at least you're not getting the logon failure anymore, which means you're connecting. You could try adding an ACE to grant all access to everyone. I could probable dig up some code if you can't work out how to do it.

EDIT: It's because of all these silly problems, plus the likeliness that Named Pipes are probably bound to TCP/IP why we just use TCP/IP instead. Plus didn't Microsoft just drop support for IPX?

The security isn't real because the traffic is still passed as plaintext over TCP.
Last edited on
Topic archived. No new replies allowed.