206 lines
9.5 KiB
HTML
206 lines
9.5 KiB
HTML
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
<!DOCTYPE html
|
||
|
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||
|
<html lang="en-us" xml:lang="en-us">
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||
|
<meta name="security" content="public" />
|
||
|
<meta name="Robots" content="index,follow" />
|
||
|
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
|
||
|
<meta name="DC.Type" content="reference" />
|
||
|
<meta name="DC.Title" content="Example: Server application that uses AF_UNIX_CCSID address family" />
|
||
|
<meta name="abstract" content="The sample programs use the AF_UNIX_CCSID address family." />
|
||
|
<meta name="description" content="The sample programs use the AF_UNIX_CCSID address family." />
|
||
|
<meta name="DC.Relation" scheme="URI" content="uafunixccsid.htm" />
|
||
|
<meta name="copyright" content="(C) Copyright IBM Corporation 2001, 2006" />
|
||
|
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2001, 2006" />
|
||
|
<meta name="DC.Format" content="XHTML" />
|
||
|
<meta name="DC.Identifier" content="xafunixccsidsrv" />
|
||
|
<meta name="DC.Language" content="en-us" />
|
||
|
<!-- All rights reserved. Licensed Materials Property of IBM -->
|
||
|
<!-- US Government Users Restricted Rights -->
|
||
|
<!-- Use, duplication or disclosure restricted by -->
|
||
|
<!-- GSA ADP Schedule Contract with IBM Corp. -->
|
||
|
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
|
||
|
<link rel="stylesheet" type="text/css" href="./ic.css" />
|
||
|
<title>Example: Server application that uses AF_UNIX_CCSID address family</title>
|
||
|
</head>
|
||
|
<body id="xafunixccsidsrv"><a name="xafunixccsidsrv"><!-- --></a>
|
||
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
||
|
<h1 class="topictitle1">Example: Server application that uses AF_UNIX_CCSID address family</h1>
|
||
|
<div><p>The sample programs use the AF_UNIX_CCSID address family.</p>
|
||
|
<div class="section"><div class="note"><span class="notetitle">Note:</span> By using the code examples, you agree to the terms
|
||
|
of the <a href="codedisclaimer.htm">Code license and disclaimer information</a>.</div>
|
||
|
</div>
|
||
|
<div class="section"><pre>/**************************************************************************/
|
||
|
/* This sample program provides code for a server application for */
|
||
|
/* AF_UNIX_CCSID address family. */
|
||
|
/**************************************************************************/
|
||
|
|
||
|
/**************************************************************************/
|
||
|
/* Header files needed for this sample program */
|
||
|
/**************************************************************************/
|
||
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <sys/socket.h>
|
||
|
#include <sys/unc.h>
|
||
|
|
||
|
/**************************************************************************/
|
||
|
/* Constants used by this program */
|
||
|
/**************************************************************************/
|
||
|
#define SERVER_PATH "/tmp/server"
|
||
|
#define BUFFER_LENGTH 250
|
||
|
#define FALSE 0
|
||
|
|
||
|
void main()
|
||
|
{
|
||
|
/***********************************************************************/
|
||
|
/* Variable and structure definitions. */
|
||
|
/***********************************************************************/
|
||
|
int sd=-1, sd2=-1;
|
||
|
int rc, length;
|
||
|
char buffer[BUFFER_LENGTH];
|
||
|
struct sockaddr_unc serveraddr;
|
||
|
|
||
|
/***********************************************************************/
|
||
|
/* A do/while(FALSE) loop is used to make error cleanup easier. The */
|
||
|
/* close() of each of the socket descriptors is only done once at the */
|
||
|
/* very end of the program. */
|
||
|
/***********************************************************************/
|
||
|
do
|
||
|
{
|
||
|
/********************************************************************/
|
||
|
/* The socket() function returns a socket descriptor representing */
|
||
|
/* an endpoint. The statement also identifies that the UNIX_CCSID */
|
||
|
/* address family with the stream transport (SOCK_STREAM) will be */
|
||
|
/* used for this socket. */
|
||
|
/********************************************************************/
|
||
|
sd = socket(AF_UNIX_CCSID, SOCK_STREAM, 0);
|
||
|
if (sd < 0)
|
||
|
{
|
||
|
perror("socket() failed");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
/********************************************************************/
|
||
|
/* After the socket descriptor is created, a bind() function gets a */
|
||
|
/* unique name for the socket. */
|
||
|
/********************************************************************/
|
||
|
memset(&serveraddr, 0, sizeof(serveraddr));
|
||
|
serveraddr.sunc_family = AF_UNIX_CCSID;
|
||
|
serveraddr.sunc_format = SO_UNC_USE_QLG;
|
||
|
serveraddr.sunc_qlg.CCSID = 500;
|
||
|
serveraddr.sunc_qlg.Path_Type = QLG_PTR_SINGLE;
|
||
|
serveraddr.sunc_qlg.Path_Length = strlen(SERVER_PATH);
|
||
|
serveraddr.sunc_path.p_unix = SERVER_PATH;
|
||
|
|
||
|
rc = bind(sd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
|
||
|
if (rc < 0)
|
||
|
{
|
||
|
perror("bind() failed");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
/********************************************************************/
|
||
|
/* The listen() function allows the server to accept incoming */
|
||
|
/* client connections. In this example, the backlog is set to 10. */
|
||
|
/* This means that the system will queue 10 incoming connection */
|
||
|
/* requests before the system starts rejecting the incoming */
|
||
|
/* requests. */
|
||
|
/********************************************************************/
|
||
|
rc = listen(sd, 10);
|
||
|
if (rc< 0)
|
||
|
{
|
||
|
perror("listen() failed");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
printf("Ready for client connect().\n");
|
||
|
|
||
|
/********************************************************************/
|
||
|
/* The server uses the accept() function to accept an incoming */
|
||
|
/* connection request. The accept() call will block indefinitely */
|
||
|
/* waiting for the incoming connection to arrive. */
|
||
|
/********************************************************************/
|
||
|
sd2 = accept(sd, NULL, NULL);
|
||
|
if (sd2 < 0)
|
||
|
{
|
||
|
perror("accept() failed");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
/********************************************************************/
|
||
|
/* In this example we know that the client will send 250 bytes of */
|
||
|
/* data over. Knowing this, we can use the SO_RCVLOWAT socket */
|
||
|
/* option and specify that we don't want our recv() to wake up */
|
||
|
/* until all 250 bytes of data have arrived. */
|
||
|
/********************************************************************/
|
||
|
length = BUFFER_LENGTH;
|
||
|
rc = setsockopt(sd2, SOL_SOCKET, SO_RCVLOWAT,
|
||
|
(char *)&length, sizeof(length));
|
||
|
if (rc < 0)
|
||
|
{
|
||
|
perror("setsockopt(SO_RCVLOWAT) failed");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
|
||
|
/********************************************************************/
|
||
|
/* Receive that 250 bytes data from the client */
|
||
|
/********************************************************************/
|
||
|
rc = recv(sd2, buffer, sizeof(buffer), 0);
|
||
|
if (rc < 0)
|
||
|
{
|
||
|
perror("recv() failed");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
printf("%d bytes of data were received\n", rc);
|
||
|
if (rc == 0 ||
|
||
|
rc < sizeof(buffer))
|
||
|
{
|
||
|
printf("The client closed the connection before all of the\n");
|
||
|
printf("data was sent\n");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
/********************************************************************/
|
||
|
/* Echo the data back to the client */
|
||
|
/********************************************************************/
|
||
|
rc = send(sd2, buffer, sizeof(buffer), 0);
|
||
|
if (rc < 0)
|
||
|
{
|
||
|
perror("send() failed");
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
/********************************************************************/
|
||
|
/* Program complete */
|
||
|
/********************************************************************/
|
||
|
|
||
|
} while (FALSE);
|
||
|
|
||
|
/***********************************************************************/
|
||
|
/* Close down any open socket descriptors */
|
||
|
/***********************************************************************/
|
||
|
if (sd != -1)
|
||
|
close(sd);
|
||
|
|
||
|
if (sd2 != -1)
|
||
|
close(sd2);
|
||
|
|
||
|
/***********************************************************************/
|
||
|
/* Remove the UNIX path name from the file system */
|
||
|
/***********************************************************************/
|
||
|
unlink(SERVER_PATH);
|
||
|
}</pre>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div>
|
||
|
<div class="familylinks">
|
||
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="uafunixccsid.htm" title="AF_UNIX_CCSID address family sockets have the same specifications as AF_UNIX address family sockets. They can be used for connection-oriented or connectionless and provide communications on the same system.">Use AF_UNIX_CCSID address family</a></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</body>
|
||
|
</html>
|