186 lines
8.9 KiB
HTML
186 lines
8.9 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 client" />
|
|
<meta name="abstract" content="This example shows how to use User Datagram Protocol (UDP) to connect a connectionless socket client program to a server." />
|
|
<meta name="description" content="This example shows how to use User Datagram Protocol (UDP) to connect a connectionless socket client program to a server." />
|
|
<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="xconlessclient" />
|
|
<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 client</title>
|
|
</head>
|
|
<body id="xconlessclient"><a name="xconlessclient"><!-- --></a>
|
|
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
|
|
<h1 class="topictitle1">Example: A connectionless client</h1>
|
|
<div><p>This example shows how to use User Datagram Protocol (UDP) to connect
|
|
a connectionless socket client program to a server.</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 client. */
|
|
/**************************************************************************/
|
|
|
|
/**************************************************************************/
|
|
/* Header files needed for this sample program */
|
|
/**************************************************************************/
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <netdb.h>
|
|
|
|
/**************************************************************************/
|
|
/* Constants used by this program */
|
|
/**************************************************************************/
|
|
#define SERVER_PORT 3555
|
|
#define BUFFER_LENGTH 100
|
|
#define FALSE 0
|
|
#define SERVER_NAME "ServerHostName"
|
|
|
|
/* Pass in 1 parameter which is either the */
|
|
/* address or host name of the server, or */
|
|
/* set the server name in the #define */
|
|
/* SERVER_NAME */
|
|
void main(int argc, char *argv[])
|
|
{
|
|
/***********************************************************************/
|
|
/* Variable and structure definitions. */
|
|
/***********************************************************************/
|
|
int sd, rc;
|
|
char server[NETDB_MAX_HOST_NAME_LENGTH];
|
|
char buffer[BUFFER_LENGTH];
|
|
struct hostent *hostp;
|
|
struct sockaddr_in serveraddr;
|
|
int serveraddrlen = sizeof(serveraddr);
|
|
|
|
/***********************************************************************/
|
|
/* A do/while(FALSE) loop is used to make error cleanup easier. The */
|
|
/* close() of the socket descriptor 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_STREAM) will be used for this socket. */
|
|
/********************************************************************/
|
|
sd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
if (sd < 0)
|
|
{
|
|
perror("socket() failed");
|
|
break;
|
|
}
|
|
|
|
/********************************************************************/
|
|
/* If an argument was passed in, use this as the server, otherwise */
|
|
/* use the #define that is located at the top of this program. */
|
|
/********************************************************************/
|
|
if (argc > 1)
|
|
strcpy(server, argv[1]);
|
|
else
|
|
strcpy(server, SERVER_NAME);
|
|
|
|
memset(&serveraddr, 0, sizeof(serveraddr));
|
|
serveraddr.sin_family = AF_INET;
|
|
serveraddr.sin_port = htons(SERVER_PORT);
|
|
serveraddr.sin_addr.s_addr = inet_addr(server);
|
|
if (serveraddr.sin_addr.s_addr == (unsigned long)INADDR_NONE)
|
|
{
|
|
/*****************************************************************/
|
|
/* The server string that was passed into the inet_addr() */
|
|
/* function was not a dotted decimal IP address. It must */
|
|
/* therefore be the hostname of the server. Use the */
|
|
/* gethostbyname() function to retrieve the IP address of the */
|
|
/* server. */
|
|
/*****************************************************************/
|
|
hostp = gethostbyname(server);
|
|
if (hostp == (struct hostent *)NULL)
|
|
{
|
|
printf("Host not found --> ");
|
|
printf("h_errno = %d\n", h_errno);
|
|
break;
|
|
}
|
|
|
|
memcpy(&serveraddr.sin_addr,
|
|
hostp->h_addr,
|
|
sizeof(serveraddr.sin_addr));
|
|
}
|
|
|
|
/********************************************************************/
|
|
/* Initialize the data block that is going to be sent to the server */
|
|
/********************************************************************/
|
|
memset(buffer, 0, sizeof(buffer));
|
|
strcpy(buffer, "A CLIENT REQUEST");
|
|
|
|
/********************************************************************/
|
|
/* Use the sendto() function to send the data to the server. */
|
|
/********************************************************************/
|
|
rc = sendto(sd, buffer, sizeof(buffer), 0,
|
|
(struct sockaddr *)&serveraddr,
|
|
sizeof(serveraddr));
|
|
if (rc < 0)
|
|
{
|
|
perror("sendto() failed");
|
|
break;
|
|
}
|
|
|
|
/********************************************************************/
|
|
/* Use the recvfrom() function to receive the data back from the */
|
|
/* server. */
|
|
/********************************************************************/
|
|
rc = recvfrom(sd, buffer, sizeof(buffer), 0,
|
|
(struct sockaddr *)&serveraddr,
|
|
& serveraddrlen);
|
|
if (rc < 0)
|
|
{
|
|
perror("recvfrom() failed");
|
|
break;
|
|
}
|
|
|
|
printf("client received the following: <%s>\n", buffer);
|
|
printf("from port %d, from address %s\n",
|
|
ntohs(serveraddr.sin_port),
|
|
inet_ntoa(serveraddr.sin_addr));
|
|
|
|
/********************************************************************/
|
|
/* 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> |