home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.hitl.washington.edu
/
ftp.hitl.washington.edu.tar
/
ftp.hitl.washington.edu
/
pub
/
people
/
peter
/
ER
/
SoundClient.cxx
< prev
next >
Wrap
C/C++ Source or Header
|
1998-07-07
|
2KB
|
99 lines
#include "SoundClient.h"
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/socket.h>
#include <errno.h>
int SoundClient::DefaultPort = 2500;
// The default port to use to connect to the sound server.
SoundClient::SoundClient()
{
// The socket starts out uninitialized.
socket_FD_ = -1;
}
SoundClient::~SoundClient()
{
}
/*
** Function name : OpenSocket(char* host, int port )
**
** Description : opens a socket
** Input : host - name of host to connect to
** port - port to connect to
** Output : true if the socket was successfully opened
** false otherwise
*/
bool
SoundClient::OpenSocket(char* hostName, int port)
{
struct sockaddr_in clientAddr;
struct hostent* host;
// Create the socket.
socket_FD_ = socket(AF_INET, SOCK_STREAM, 0);
if(socket_FD_ < 0) {
cerr << "SoundClient: Cannot create sound socket to host " <<
hostName << " port " << port << endl;
return false;
}
// Clear struct.
bzero((char*)&clientAddr, sizeof(clientAddr));
// Fill it in.
clientAddr.sin_family = AF_INET;
clientAddr.sin_port = htons(port);
host = gethostbyname(hostName);
if (host)
clientAddr.sin_addr.s_addr = ((struct in_addr*)(host->h_addr_list[0]))->s_addr;
else {
cerr << "SoundClient: Unable to resolve ip number for " <<
hostName << ". Check /etc/hosts" << endl;
return false;
}
// Connect.
if (connect(socket_FD_, (struct sockaddr*)&clientAddr, sizeof(clientAddr)) < 0) {
cerr << "SoundClient: Cannot connect." << endl;
return false;
}
return true;
}
/*
** Function name : sendSound(int fd, char* sound )
**
** Description : writes the name of the sound to the socket
** Input : sound -- the name of the sound file to play, without the
** extention
** Output : the number of bytes written
*/
int
SoundClient::SendSound(char* sound)
{
// Make sure it's a valid socket.
if (socket_FD_) {
cerr << "SoundClient: Sending " << sound << endl;
return write(socket_FD_, sound, strlen(sound));
} else {
cerr << "SoundClient: Socket was not opened successfully, so cannot send "
<< sound << endl;
return 0;
}
}