156 lines
7.7 KiB
HTML
156 lines
7.7 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: A connectionless server" />
|
|
<meta name="abstract" content="This example can be used to create your own connectionless server design. This example uses User Datagram Protocol (UDP) to create a connectionless socket server program." />
|
|
<meta name="description" content="This example can be used to create your own connectionless server design. This example uses User Datagram Protocol (UDP) to create a connectionless socket server program." />
|
|
<meta name="DC.Relation" scheme="URI" content="connectionless.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="xconlessserver" />
|
|
<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: A connectionless server</title>
|
|
</head>
|
|
<body id="xconlessserver"><a name="xconlessserver"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: A connectionless server</h1>
|
|
<div><p>This example can be used to create your own connectionless server
|
|
design. This example uses User Datagram Protocol (UDP) to create a connectionless
|
|
socket server program.</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 a code for a connectionless server. */
|
|
/**************************************************************************/
|
|
|
|
/**************************************************************************/
|
|
/* Header files needed for this sample program */
|
|
/**************************************************************************/
|
|
#include <stdio.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
|
|
/**************************************************************************/
|
|
/* Constants used by this program */
|
|
/**************************************************************************/
|
|
#define SERVER_PORT 3555
|
|
#define BUFFER_LENGTH 100
|
|
#define FALSE 0
|
|
|
|
void main()
|
|
{
|
|
/***********************************************************************/
|
|
/* Variable and structure definitions. */
|
|
/***********************************************************************/
|
|
int sd=-1, rc;
|
|
char buffer[BUFFER_LENGTH];
|
|
struct sockaddr_in serveraddr;
|
|
struct sockaddr_in clientaddr;
|
|
int clientaddrlen = sizeof(clientaddr);
|
|
|
|
/***********************************************************************/
|
|
/* 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 INET */
|
|
/* (Internet Protocol) address family with the UDP transport */
|
|
/* (SOCK_DGRAM) will be used for this socket. */
|
|
/********************************************************************/
|
|
sd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
if (sd < 0)
|
|
{
|
|
perror("socket() failed");
|
|
break;
|
|
}
|
|
|
|
/********************************************************************/
|
|
/* After the socket descriptor is created, a bind() function gets a */
|
|
/* unique name for the socket. In this example, the user sets the */
|
|
/* s_addr to zero, which means that the UDP port of 3555 will be */
|
|
/* bound to all IP addresses on the system. */
|
|
/********************************************************************/
|
|
memset(&serveraddr, 0, sizeof(serveraddr));
|
|
serveraddr.sin_family = AF_INET;
|
|
serveraddr.sin_port = htons(SERVER_PORT);
|
|
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
rc = bind(sd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
|
|
if (rc < 0)
|
|
{
|
|
perror("bind() failed");
|
|
break;
|
|
}
|
|
|
|
/********************************************************************/
|
|
/* The server uses the recvfrom() function to receive that data. */
|
|
/* The recvfrom() function waits indefinitely for data to arrive. */
|
|
/********************************************************************/
|
|
rc = recvfrom(sd, buffer, sizeof(buffer), 0,
|
|
(struct sockaddr *)&clientaddr,
|
|
&clientaddrlen);
|
|
if (rc < 0)
|
|
{
|
|
perror("recvfrom() failed");
|
|
break;
|
|
}
|
|
|
|
printf("server received the following: <%s>\n", buffer);
|
|
printf("from port %d and address %s\n",
|
|
ntohs(clientaddr.sin_port),
|
|
inet_ntoa(clientaddr.sin_addr));
|
|
|
|
/********************************************************************/
|
|
/* Echo the data back to the client */
|
|
/********************************************************************/
|
|
rc = sendto(sd, buffer, sizeof(buffer), 0,
|
|
(struct sockaddr *)&clientaddr,
|
|
sizeof(clientaddr));
|
|
if (rc < 0)
|
|
{
|
|
perror("sendto() failed");
|
|
break;
|
|
}
|
|
|
|
/********************************************************************/
|
|
/* Program complete */
|
|
/********************************************************************/
|
|
|
|
} while (FALSE);
|
|
|
|
/***********************************************************************/
|
|
/* Close down any open socket descriptors */
|
|
/***********************************************************************/
|
|
if (sd != -1)
|
|
close(sd);
|
|
}</pre>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="connectionless.htm" title="Connectionless sockets do not establish a connection over which data is transferred. Instead, the server application specifies its name where a client can send requests.">Create a connectionless socket</a></div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |