home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
tolkit45.zip
/
os2tk45
/
samples
/
tcpiptk
/
socket
/
udps.c
< prev
next >
Wrap
Text File
|
1999-05-11
|
5KB
|
125 lines
/********************************************************copyrite.xic********/
/* */
/* Licensed Materials - Property of IBM */
/* IBM TCP/IP for OS/2 */
/* (C) Copyright IBM Corporation. 1990,1991. */
/* */
/* All rights reserved. */
/* */
/* US Government Users Restricted Rights - */
/* Use, duplication or disclosure restricted by GSA ADP Schedule */
/* Contract with IBM Corp. */
/* */
/*--------------------------------------------------------------------------*/
/* */
/* DISCLAIMER OF WARRANTIES. The following [enclosed] code is */
/* sample code created by IBM Corporation. This sample code is not */
/* part of any standard or IBM product and is provided to you solely */
/* for the purpose of assisting you in the development of your */
/* applications. The code is provided "AS IS", without */
/* warranty of any kind. IBM shall not be liable for any damages */
/* arising out of your use of the sample code, even if they have been */
/* advised of the possibility of such damages. */
/*--------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <arpa/inet.h>
main(void)
{
int sockint,s, namelen, client_address_size;
struct sockaddr_in client, server;
char buf[32];
/*
* Initialize with sockets.
*/
if ((sockint = sock_init()) != 0)
{
printf(" INET.SYS probably is not running");
exit(1);
}
/*
* Create a datagram socket in the internet domain and use the
* default protocol (UDP).
*/
if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
{
psock_errno("socket()");
exit(1);
}
/*
*
* Bind my name to this socket so that clients on the network can
* send me messages. (This allows the operating system to demultiplex
* messages and get them to the correct server)
*
* Set up the server name. The internet address is specified as the
* wildcard INADDR_ANY so that the server can get messages from any
* of the physical internet connections on this host. (Otherwise we
* would limit the server to messages from only one network interface)
*/
server.sin_family = AF_INET; /* Server is in Internet Domain */
server.sin_port = 0; /* Use any available port */
server.sin_addr.s_addr = INADDR_ANY;/* Server's Internet Address */
if (bind(s, (struct sockaddr *)&server, sizeof(server)) < 0)
{
psock_errno("bind()");
exit(2);
}
/* Find out what port was really assigned and print it */
namelen = sizeof(server);
if (getsockname(s, (struct sockaddr *) &server, &namelen) < 0)
{
psock_errno("getsockname()");
exit(3);
}
printf("Port assigned is %d\n", ntohs(server.sin_port));
/*
* Receive a message on socket s in buf of maximum size 32
* from a client. Because the last two paramters
* are not null, the name of the client will be placed into the
* client data structure and the size of the client address will
* be placed into client_address_size.
*/
client_address_size = sizeof(client);
if(recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr *) &client,
&client_address_size) <0)
{
psock_errno("recvfrom()");
exit(4);
}
/*
* Print the message and the name of the client.
* The domain should be the internet domain (AF_INET).
* The port is received in network byte order, so we translate it to
* host byte order before printing it.
* The internet address is received as 32 bits in network byte order
* so we use a utility that converts it to a string printed in
* dotted decimal format for readability.
*/
printf("Received message %s from domain %s port %d internet address %s\n",
buf,
(client.sin_family == AF_INET?"AF_INET":"UNKNOWN"),
ntohs(client.sin_port),
inet_ntoa(client.sin_addr));
/*
* Deallocate the socket.
*/
soclose(s);
}