with error code "Example Using Local Namespace Sockets"

I am new to Dev C + + and I'm trying to use the code "Example Using Local Namespace Sockets".
I'm not using Linux but window


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
/* Read text from the socket and print it out. Continue until the
socket closes. Return nonzero if the client sent a “quit”
message, zero otherwise. */
int server (int client_socket)
{
while (1) {
int length;
char* text;
/* First, read the length of the text message from the socket. If
read returns zero, the client closed the connection. */
if (read (client_socket, &length, sizeof (length)) == 0)
return 0;
/* Allocate a buffer to hold the text. */
text = (char*) malloc (length);
/* Read the text itself, and print it. */
read (client_socket, text, length);
printf (“%s\n”, text);
/* Free the buffer. */
free (text);
/* If the client sent the message “quit,” we’re all done. */
if (!strcmp (text, “quit”))
return 1;
}
}
int main (int argc, char* const argv[])
{
const char* const socket_name = argv[1];
5.5 Sockets 121
int socket_fd;
struct sockaddr_un name;
int client_sent_quit_message;
/* Create the socket. */
socket_fd = socket (PF_LOCAL, SOCK_STREAM, 0);
/* Indicate that this is a server. */
name.sun_family = AF_LOCAL;
strcpy (name.sun_path, socket_name);
bind (socket_fd, &name, SUN_LEN (&name));
/* Listen for connections. */
listen (socket_fd, 5);
/* Repeatedly accept connections, spinning off one server() to deal
with each client. Continue until a client sends a “quit” message. */
do {
struct sockaddr_un client_name;
socklen_t client_name_len;
int client_socket_fd;
/* Accept a connection. */
client_socket_fd = accept (socket_fd, &client_name, &client_name_len);
/* Handle the connection. */
client_sent_quit_message = server (client_socket_fd);
/* Close our end of the connection. */
close (client_socket_fd);
}
while (!client_sent_quit_message);
/* Remove the socket file. */
close (socket_fd);
unlink (socket_name);
return 0;
}


However appears several errors:
\Dev-Cpp\Sem Título1.cpp C:\Dev-Cpp\C sys/socket.h: No such file or directory.
\Dev-Cpp\Sem Título1.cpp C:\Dev-Cpp\C sys/un.h: No such file or directory.
C:\Dev-Cpp\Sem Título1.cpp In function `int server(int)':
23 C:\Dev-Cpp\Sem Título1.cpp stray '\147' in program
23 C:\Dev-Cpp\Sem Título1.cpp expected primary-expression before '%' token
23 C:\Dev-Cpp\Sem Título1.cpp stray '\' in program
23 C:\Dev-Cpp\Sem Título1.cpp `s' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
23 C:\Dev-Cpp\Sem Título1.cpp stray '\148' in program
27 C:\Dev-Cpp\Sem Título1.cpp stray '\147' in program
27 C:\Dev-Cpp\Sem Título1.cpp stray '\148' in program
27 C:\Dev-Cpp\Sem Título1.cpp `quit' undeclared (first use this function)
C:\Dev-Cpp\Sem Título1.cpp In function `int main(int, char* const*)':
34 C:\Dev-Cpp\Sem Título1.cpp expected `;' before "Sockets"
36 C:\Dev-Cpp\Sem Título1.cpp aggregate `sockaddr_un name' has incomplete type and cannot be defined
39 C:\Dev-Cpp\Sem Título1.cpp `socket_fd' undeclared (first use this function)
39 C:\Dev-Cpp\Sem Título1.cpp `PF_LOCAL' undeclared (first use this function)
39 C:\Dev-Cpp\Sem Título1.cpp `SOCK_STREAM' undeclared (first use this function)
39 C:\Dev-Cpp\Sem Título1.cpp `socket' undeclared (first use this function)
41 C:\Dev-Cpp\Sem Título1.cpp `AF_LOCAL' undeclared (first use this function)
43 C:\Dev-Cpp\Sem Título1.cpp `SUN_LEN' undeclared (first use this function)
43 C:\Dev-Cpp\Sem Título1.cpp `bind' undeclared (first use this function)
45 C:\Dev-Cpp\Sem Título1.cpp `listen' undeclared (first use this function)
49 C:\Dev-Cpp\Sem Título1.cpp aggregate `sockaddr_un client_name' has incomplete type and cannot be defined
50 C:\Dev-Cpp\Sem Título1.cpp `socklen_t' undeclared (first use this function)
50 C:\Dev-Cpp\Sem Título1.cpp expected `;' before "client_name_len"
53 C:\Dev-Cpp\Sem Título1.cpp `client_name_len' undeclared (first use this function)
53 C:\Dev-Cpp\Sem Título1.cpp `accept' undeclared (first use this function)


Anyone know tells me the reason of these errors? ecomo I do to solve them?
It looks like you're trying to use functions and headers meant for linux in a Windows environment, don't do that. If you want to learn how to do this take a look here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms740121(v=vs.85).aspx about half way down the page there is some pretty example code describing how to recieve data from a socket. As a side note, because I don't really know how new you are to all of this, if you're using the default settings for wxDev-C++ then your compiler is MingW so you'll be looking to link to 'libws2_32.a' not 'Ws2_32.lib' as described in the requirements on that page.
Anyone know some library in Windows like Linux library "<sys/un.h>".
Why not just grab a Linux boot disk and use that to program in? You can even install it as a secondary boot option along side windows.
I do not know if I explained well what I want to do.
I have an example of cogido used in Linux to use a socket. and I would like to pass this example for Windows. I already changed some things in the code.


#include <stdio.h>
#include <string.h>
#include <winsock2.h>
#include <winsock.h>
//#include <sys/socket.h>
//#include <sys/un.h>

#include <unistd.h>

/* Read text from the socket and print it out. Continue until the
socket closes. Return nonzero if the client sent a “quit”
message, zero otherwise. */
int server (int client_socket)
{
while (1) {
int length;
char* text;
/* First, read the length of the text message from the socket. If
read returns zero, the client closed the connection. */
if (read (client_socket, &length, sizeof (length)) == 0)
return 0;
/* Allocate a buffer to hold the text. */
text = (char*) malloc (length);
/* Read the text itself, and print it. */
read (client_socket, text, length);
printf ("%s\n", text);
/* Free the buffer. */
free (text);
/* If the client sent the message “quit,” we’re all done. */
if (!strcmp (text, "quit"))
return 1;
}
}
int main (int argc, char* const argv[])
{
const char* const socket_name = argv[1];

int socket_fd;
struct sockaddr_in name;
int client_sent_quit_message;
/* Create the socket. */
socket_fd = socket (PF_UNIX, SOCK_STREAM, 0);
/* Indicate that this is a server. */
name.sun_family = AF_UNIX;
strcpy (name.sun_path, socket_name);
bind (socket_fd, &name, SUN_LEN (&name));
/* Listen for connections. */
listen (socket_fd, 5);
/* Repeatedly accept connections, spinning off one server() to deal
with each client. Continue until a client sends a “quit” message. */
do {
struct sockaddr_in client_name;
socklen_t client_name_len;
int client_socket_fd;
/* Accept a connection. */
client_socket_fd = accept (socket_fd, &client_name, &client_name_len);
/* Handle the connection. */
client_sent_quit_message = server (client_socket_fd);
/* Close our end of the connection. */
close (client_socket_fd);
}
while (!client_sent_quit_message);
/* Remove the socket file. */
close (socket_fd);
unlink (socket_name);
return 0;
}



 but further is pointing out some errors like this:

  C: \ Dev-Cpp \ Kevin \ example1 \ main.cpp In function `int main (int, char * const *) ':
52 C:\Dev-Cpp\Kevin\exemplo1\main.cpp 'struct sockaddr_in' has no member named 'sun_family'

53 C:\Dev-Cpp\Kevin\exemplo1\main.cpp 'struct sockaddr_in' has no member named 'sun_path'

54 C:\Dev-Cpp\Kevin\exemplo1\main.cpp `SUN_LEN' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)

61 C:\Dev-Cpp\Kevin\exemplo1\main.cpp `socklen_t' undeclared (first use this function)

61 C:\Dev-Cpp\Kevin\exemplo1\main.cpp expected `;' before "client_name_len"

64 C:\Dev-Cpp\Kevin\exemplo1\main.cpp `client_name_len' undeclared (first use this function)
C:\Dev-Cpp\Kevin\exemplo1\Makefile.win [Build Error] [main.o] Error 1


I think those who are undefined variables are on the library "<sys/un.h>". you have any idea how I can rsolver these errors?
Last edited on
here is a code of Soquet client running Windows




// Client program example
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define DEFAULT_PORT 2007//2007
// TCP socket type
#define DEFAULT_PROTO SOCK_STREAM

void Usage(char *progname)
{
fprintf(stderr,"Usage: %s -p [protocol] -n [server name/IP] -e [port_num] -l [iterations]\n", progname);
fprintf(stderr,"Where:\n\tprotocol is one of TCP or UDP\n");
fprintf(stderr,"\t- server is the IP address or name of server\n");
fprintf(stderr,"\t- port_num is the port to listen on\n");
fprintf(stderr,"\t- iterations is the number of loops to execute.\n");
fprintf(stderr,"\t- (-l by itself makes client run in an infinite loop,\n");
fprintf(stderr,"\t- Hit Ctrl-C to terminate it)\n");
fprintf(stderr,"\t- The defaults are TCP , localhost and 2007\n");
WSACleanup();
exit(1);
}

int main(int argc, char **argv)
{
char Buffer[512];// 128
// default to localhost
char *server_name= "localhost";
unsigned short port = DEFAULT_PORT;
int retval, loopflag = 0;
int i, loopcount, maxloop=-1;
unsigned int addr;
int socket_type = DEFAULT_PROTO;
struct sockaddr_in server;
struct hostent *hp;
WSADATA wsaData;
SOCKET conn_socket;

if (argc >1)
{
for(i=1; i<argc; i++)
{
if ((argv[i][0] == '-') || (argv[i][0] == '/'))
{
switch(tolower(argv[i][1]))
{
case 'p':
if (!stricmp(argv[i+1], "TCP"))
socket_type = SOCK_STREAM;
else if (!stricmp(argv[i+1], "UDP"))
socket_type = SOCK_DGRAM;
else
Usage(argv[0]);
i++;
break;
case 'n':
server_name = argv[++i];
break;
case 'e':
port = atoi(argv[++i]);
break;
case 'l':
loopflag =1;
if (argv[i+1]) {
if (argv[i+1][0] != '-')
maxloop = atoi(argv[i+1]);
}
else
maxloop = -1;
i++;
break;
default:
Usage(argv[0]);
break;
}
}
else
Usage(argv[0]);
}
}

if ((retval = WSAStartup(0x202, &wsaData)) != 0)
{
fprintf(stderr,"Client: WSAStartup() failed with error %d\n", retval);
WSACleanup();
return -1;
}
else
printf("Client: WSAStartup() is OK.\n");

if (port == 0)
{
Usage(argv[0]);
}
// Attempt to detect if we should call gethostbyname() or gethostbyaddr()
if (isalpha(server_name[0]))
{ // server address is a name
hp = gethostbyname(server_name);
}
else
{ // Convert nnn.nnn address to a usable one
addr = inet_addr(server_name);
hp = gethostbyaddr((char *)&addr, 4, AF_INET);
}
if (hp == NULL )
{
fprintf(stderr,"Client: Cannot resolve address \"%s\": Error %d\n", server_name, WSAGetLastError());
WSACleanup();
exit(1);
}
else
printf("Client: gethostbyaddr() is OK.\n");
// Copy the resolved information into the sockaddr_in structure
memset(&server, 0, sizeof(server));
memcpy(&(server.sin_addr), hp->h_addr, hp->h_length);
server.sin_family = hp->h_addrtype;
server.sin_port = htons(port);

conn_socket = socket(AF_INET, socket_type, 0); /* Open a socket */
if (conn_socket <0 )
{
fprintf(stderr,"Client: Error Opening socket: Error %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
else
printf("Client: socket() is OK.\n");

// Notice that nothing in this code is specific to whether we
// are using UDP or TCP.
// We achieve this by using a simple trick.
// When connect() is called on a datagram socket, it does not
// actually establish the connection as a stream (TCP) socket
// would. Instead, TCP/IP establishes the remote half of the
// (LocalIPAddress, LocalPort, RemoteIP, RemotePort) mapping.
// This enables us to use send() and recv() on datagram sockets,
// instead of recvfrom() and sendto()
printf("Client: Client connecting to: %s.\n", hp->h_name);
if (connect(conn_socket, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
fprintf(stderr,"Client: connect() failed: %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
else
printf("Client: connect() is OK.\n");

// Test sending some string
loopcount = 0;
while(1)
{
wsprintf(Buffer,"This is a test message from client #%d", loopcount++);
retval = send(conn_socket, Buffer, sizeof(Buffer), 0);
if (retval == SOCKET_ERROR)
{
fprintf(stderr,"Client: send() failed: error %d.\n", WSAGetLastError());
WSACleanup();
return -1;
}
else
printf("Client: send() is OK.\n");
printf("Client: Sent data \"%s\"\n", Buffer);

retval = recv(conn_socket, Buffer, sizeof(Buffer), 0);
if (retval == SOCKET_ERROR)
{
fprintf(stderr,"Client: recv() failed: error %d.\n", WSAGetLastError());
closesocket(conn_socket);
WSACleanup();
return -1;
}
else
printf("Client: recv() is OK.\n");

// We are not likely to see this with UDP, since there is no
// 'connection' established.
if (retval == 0)
{
printf("Client: Server closed connection.\n");
closesocket(conn_socket);
WSACleanup();
return -1;
}

printf("Client: Received %d bytes, data \"%s\" from server.\n", retval, Buffer);
if (!loopflag)
{
printf("Client: Terminating connection...\n");
break;
}
else
{
if ((loopcount >= maxloop) && (maxloop >0))
break;
}
}
closesocket(conn_socket);
WSACleanup();

return 0;
}
Topic archived. No new replies allowed.