home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
viscobv6.zip
/
vac22os2
/
ibmcobol
/
samples
/
toolkit
/
tcpiptk
/
socket
/
tcps.c
< prev
next >
Wrap
Text File
|
1996-11-19
|
4KB
|
134 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 Files.
*/
#include <stdio.h>
#include <stdlib.h>
#include <types.h>
#include <netinet\in.h>
#include <sys\socket.h>
/*
* Server Main.
*/
main(int argc, char *argv[])
{
unsigned short port; /* port server binds to */
char buf[12]; /* buffer for sending and receiving data */
struct sockaddr_in client; /* client address information */
struct sockaddr_in server; /* server address information */
int s; /* socket for accepting connections */
int ns; /* socket connected to client */
int namelen; /* length of client name */
/*
* Check arguments. Should be only one: the port number to bind to.
*/
if (argc != 2)
{
fprintf(stderr, "Usage: %s port\n", argv[0]);
exit(1);
}
/*
* Initialize with sockets.
*/
sock_init();
/*
* First argument should be the port.
*/
port = (unsigned short) atoi(argv[1]);
/*
* Get a socket for accepting connections.
*/
if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
psock_errno("Socket()");
exit(2);
}
/*
* Bind the socket to the server address.
*/
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = INADDR_ANY;
if (bind(s, (struct sockaddr *)&server, sizeof(server)) < 0)
{
psock_errno("Bind()");
exit(3);
}
/*
* Listen for connections. Specify the backlog as 1.
*/
if (listen(s, 1) != 0)
{
psock_errno("Listen()");
exit(4);
}
/*
* Accept a connection.
*/
namelen = sizeof(client);
if ((ns = accept(s, (struct sockaddr *)&client, &namelen)) == -1)
{
psock_errno("Accept()");
exit(5);
}
/*
* Receive the message on the newly connected socket.
*/
if (recv(ns, buf, sizeof(buf), 0) == -1)
{
psock_errno("Recv()");
exit(6);
}
/*
* Send the message back to the client.
*/
if (send(ns, buf, sizeof(buf), 0) < 0)
{
psock_errno("Send()");
exit(7);
}
soclose(ns);
soclose(s);
printf("Server ended successfully\n");
exit(0);
}