please help me any one
Expand|Select|Wrap|Line Numbers
- server.cpp
- #include "PracticalSocket.h" // For Socket, ServerSocket, and SocketException
- #include <iostream> // For cerr and cout
- #include <cstdlib> // For atoi()
- using namespace std;
- const unsigned int RCVBUFSIZE = 32; // Size of receive buffer
- void HandleTCPClient(TCPSocket *sock); // TCP client handling function
- int main(int argc, char *argv[]) {
- if (argc != 2) { // Test for correct number of arguments
- cerr << "Usage: " << argv[0] << " <Server Port>" << endl;
- exit(1);
- }
- unsigned short echoServPort = atoi(argv[1]); // First arg: local port
- try {
- TCPServerSocket servSock(echoServPort); // Server Socket object
- for (;;) { // Run forever
- HandleTCPClient(servSock.accept()); // Wait for a client to connect
- }
- } catch (SocketException &e) {
- cerr << e.what() << endl;
- exit(1);
- }
- // NOT REACHED
- return 0;
- }
- // TCP client handling function
- void HandleTCPClient(TCPSocket *sock) {
- cout << "Handling client ";
- try {
- cout << sock->getForeignAddress() << ":";
- } catch (SocketException e) {
- cerr << "Unable to get foreign address" << endl;
- }
- try {
- cout << sock->getForeignPort();
- } catch (SocketException e) {
- cerr << "Unable to get foreign port" << endl;
- }
- cout << endl;
- // Send received string and receive again until the end of transmission
- char echoBuffer[RCVBUFSIZE];
- int recvMsgSize;
- while ((recvMsgSize = sock->recv(echoBuffer, RCVBUFSIZE)) > 0) { // Zero means
- // end of transmission
- // Echo message back to client
- sock->send(echoBuffer, recvMsgSize);
- }
- delete sock;
- }
- client.cpp
- #include "PracticalSocket.h" // For Socket and SocketException
- #include <iostream> // For cerr and cout
- #include <cstdlib> // For atoi()
- using namespace std;
- const int RCVBUFSIZE = 32; // Size of receive buffer
- int main(int argc, char *argv[]) {
- if ((argc < 3) || (argc > 4)) { // Test for correct number of arguments
- cerr << "Usage: " << argv[0]
- << " <Server> <Echo String> [<Server Port>]" << endl;
- exit(1);
- }
- string servAddress = argv[1]; // First arg: server address
- char *echoString = argv[2]; // Second arg: string to echo
- int echoStringLen = strlen(echoString); // Determine input length
- unsigned short echoServPort = (argc == 4) ? atoi(argv[3]) : 7;
- try {
- // Establish connection with the echo server
- TCPSocket sock(servAddress, echoServPort);
- // Send the string to the echo server
- sock.send(echoString, echoStringLen);
- char echoBuffer[RCVBUFSIZE + 1]; // Buffer for echo string + \0
- int bytesReceived = 0; // Bytes read on each recv()
- int totalBytesReceived = 0; // Total bytes read
- // Receive the same string back from the server
- cout << "Received: "; // Setup to print the echoed string
- while (totalBytesReceived < echoStringLen) {
- // Receive up to the buffer size bytes from the sender
- if ((bytesReceived = (sock.recv(echoBuffer, RCVBUFSIZE))) <= 0) {
- cerr << "Unable to read";
- exit(1);
- }
- totalBytesReceived += bytesReceived; // Keep tally of total bytes
- echoBuffer[bytesReceived] = '\0'; // Terminate the string!
- cout << echoBuffer; // Print the echo buffer
- }
- cout << endl;
- // Destructor closes the socket
- } catch(SocketException &e) {
- cerr << e.what() << endl;
- exit(1);
- }
- return 0;
- }
- practicalSocket.h
- #ifndef __PRACTICALSOCKET_INCLUDED__
- #define __PRACTICALSOCKET_INCLUDED__
- #include <string> // For string
- #include <exception> // For exception class
- using namespace std;
- class SocketException : public exception
- {
- public:
- SocketException(const string &message, bool inclSysMsg = false) throw();
- ~SocketException() throw();
- const char *what() const throw();
- private:
- string userMessage; // Exception message
- };
- class Socket {
- public:
- ~Socket();
- string getLocalAddress() throw(SocketException);
- unsigned short getLocalPort() throw(SocketException);
- void setLocalPort(unsigned short localPort) throw(SocketException);
- void setLocalAddressAndPort(const string &localAddress,
- unsigned short localPort = 0) throw(SocketException);
- static void cleanUp() throw(SocketException);
- static unsigned short resolveService(const string &service,
- const string &protocol = "tcp");
- private:
- Socket(const Socket &sock);
- void operator=(const Socket &sock);
- protected:
- int sockDesc; // Socket descriptor
- Socket(int type, int protocol) throw(SocketException);
- Socket(int sockDesc);
- };
- class CommunicatingSocket : public Socket
- {
- public:
- void connect(const string &foreignAddress, unsigned short foreignPort)
- throw(SocketException);
- void send(const void *buffer, int bufferLen) throw(SocketException);
- int recv(void *buffer, int bufferLen) throw(SocketException);
- string getForeignAddress() throw(SocketException);
- unsigned short getForeignPort() throw(SocketException);
- protected:
- CommunicatingSocket(int type, int protocol) throw(SocketException);
- CommunicatingSocket(int newConnSD);
- };
- class TCPSocket : public CommunicatingSocket {
- public:
- TCPSocket() throw(SocketException);
- TCPSocket(const string &foreignAddress, unsigned short foreignPort)
- throw(SocketException);
- private:
- // Access for TCPServerSocket::accept() connection creation
- friend class TCPServerSocket;
- TCPSocket(int newConnSD);
- };
- class TCPServerSocket : public Socket {
- public:
- TCPServerSocket(unsigned short localPort, int queueLen = 5)
- throw(SocketException);
- TCPServerSocket(const string &localAddress, unsigned short localPort,
- int queueLen = 5) throw(SocketException);
- TCPSocket *accept() throw(SocketException);
- private:
- void setListen(int queueLen) throw(SocketException);
- };
- void disconnect() throw(SocketException);
- void sendTo(const void *buffer, int bufferLen, const string &foreignAddress,
- unsigned short foreignPort) throw(SocketException);
- int recvFrom(void *buffer, int bufferLen, string &sourceAddress,
- unsigned short &sourcePort) throw(SocketException);
- private:
- void setBroadcast();
- };
- #endif
- procticalsocket.cpp
- #include "PracticalSocket.h"
- #ifdef WIN32
- #include <winsock.h> // For socket(), connect(), send(), and recv()
- typedef int socklen_t;
- typedef char raw_type; // Type used for raw data on this platform
- #else
- #include <sys/types.h> // For data types
- #include <sys/socket.h> // For socket(), connect(), send(), and recv()
- #include <netdb.h> // For gethostbyname()
- #include <arpa/inet.h> // For inet_addr()
- #include <unistd.h> // For close()
- #include <netinet/in.h> // For sockaddr_in
- typedef void raw_type; // Type used for raw data on this platform
- #endif
- #include <errno.h> // For errno
- using namespace std;
- #ifdef WIN32
- static bool initialized = false;
- #endif
- // SocketException Code
- SocketException::SocketException(const string &message, bool inclSysMsg)
- throw() : userMessage(message) {
- if (inclSysMsg) {
- userMessage.append(": ");
- userMessage.append(strerror(errno));
- }
- }
- SocketException::~SocketException() throw() {
- }
- const char *SocketException::what() const throw() {
- return userMessage.c_str();
- }
- // Function to fill in address structure given an address and port
- static void fillAddr(const string &address, unsigned short port,
- sockaddr_in &addr) {
- memset(&addr, 0, sizeof(addr)); // Zero out address structure
- addr.sin_family = AF_INET; // Internet address
- hostent *host; // Resolve name
- if ((host = gethostbyname(address.c_str())) == NULL) {
- // strerror() will not work for gethostbyname() and hstrerror()
- // is supposedly obsolete
- throw SocketException("Failed to resolve name (gethostbyname())");
- }
- addr.sin_addr.s_addr = *((unsigned long *) host->h_addr_list[0]);
- addr.sin_port = htons(port); // Assign port in network byte order
- }
- // Socket Code
- Socket::Socket(int type, int protocol) throw(SocketException) {
- #ifdef WIN32
- if (!initialized) {
- WORD wVersionRequested;
- WSADATA wsaData;
- wVersionRequested = MAKEWORD(2, 0); // Request WinSock v2.0
- if (WSAStartup(wVersionRequested, &wsaData) != 0) { // Load WinSock DLL
- throw SocketException("Unable to load WinSock DLL");
- }
- initialized = true;
- }
- #endif
- // Make a new socket
- if ((sockDesc = socket(PF_INET, type, protocol)) < 0) {
- throw SocketException("Socket creation failed (socket())", true);
- }
- }
- Socket::Socket(int sockDesc) {
- this->sockDesc = sockDesc;
- }
- Socket::~Socket() {
- #ifdef WIN32
- ::closesocket(sockDesc);
- #else
- ::close(sockDesc);
- #endif
- sockDesc = -1;
- }
- string Socket::getLocalAddress() throw(SocketException) {
- sockaddr_in addr;
- unsigned int addr_len = sizeof(addr);
- if (getsockname(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
- throw SocketException("Fetch of local address failed (getsockname())", true);
- }
- return inet_ntoa(addr.sin_addr);
- }
- unsigned short Socket::getLocalPort() throw(SocketException) {
- sockaddr_in addr;
- unsigned int addr_len = sizeof(addr);
- if (getsockname(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
- throw SocketException("Fetch of local port failed (getsockname())", true);
- }
- return ntohs(addr.sin_port);
- }
- void Socket::setLocalPort(unsigned short localPort) throw(SocketException) {
- // Bind the socket to its port
- sockaddr_in localAddr;
- memset(&localAddr, 0, sizeof(localAddr));
- localAddr.sin_family = AF_INET;
- localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- localAddr.sin_port = htons(localPort);
- if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) {
- throw SocketException("Set of local port failed (bind())", true);
- }
- }
- void Socket::setLocalAddressAndPort(const string &localAddress,
- unsigned short localPort) throw(SocketException) {
- // Get the address of the requested host
- sockaddr_in localAddr;
- fillAddr(localAddress, localPort, localAddr);
- if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) {
- throw SocketException("Set of local address and port failed (bind())", true);
- }
- }
- void Socket::cleanUp() throw(SocketException) {
- #ifdef WIN32
- if (WSACleanup() != 0) {
- throw SocketException("WSACleanup() failed");
- }
- #endif
- }
- unsigned short Socket::resolveService(const string &service,
- const string &protocol) {
- struct servent *serv; /* Structure containing service information */
- if ((serv = getservbyname(service.c_str(), protocol.c_str())) == NULL)
- return atoi(service.c_str()); /* Service is port number */
- else
- return ntohs(serv->s_port); /* Found port (network byte order) by name */
- }
- // CommunicatingSocket Code
- CommunicatingSocket::CommunicatingSocket(int type, int protocol)
- throw(SocketException) : Socket(type, protocol) {
- }
- CommunicatingSocket::CommunicatingSocket(int newConnSD) : Socket(newConnSD) {
- }
- void CommunicatingSocket::connect(const string &foreignAddress,
- unsigned short foreignPort) throw(SocketException) {
- // Get the address of the requested host
- sockaddr_in destAddr;
- fillAddr(foreignAddress, foreignPort, destAddr);
- // Try to connect to the given port
- if (::connect(sockDesc, (sockaddr *) &destAddr, sizeof(destAddr)) < 0) {
- throw SocketException("Connect failed (connect())", true);
- }
- }
- void CommunicatingSocket::send(const void *buffer, int bufferLen)
- throw(SocketException) {
- if (::send(sockDesc, (raw_type *) buffer, bufferLen, 0) < 0) {
- throw SocketException("Send failed (send())", true);
- }
- }
- int CommunicatingSocket::recv(void *buffer, int bufferLen)
- throw(SocketException) {
- int rtn;
- if ((rtn = ::recv(sockDesc, (raw_type *) buffer, bufferLen, 0)) < 0) {
- throw SocketException("Received failed (recv())", true);
- }
- return rtn;
- }
- string CommunicatingSocket::getForeignAddress()
- throw(SocketException) {
- sockaddr_in addr;
- unsigned int addr_len = sizeof(addr);
- if (getpeername(sockDesc, (sockaddr *) &addr,(socklen_t *) &addr_len) < 0) {
- throw SocketException("Fetch of foreign address failed (getpeername())", true);
- }
- return inet_ntoa(addr.sin_addr);
- }
- unsigned short CommunicatingSocket::getForeignPort() throw(SocketException) {
- sockaddr_in addr;
- unsigned int addr_len = sizeof(addr);
- if (getpeername(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) {
- throw SocketException("Fetch of foreign port failed (getpeername())", true);
- }
- return ntohs(addr.sin_port);
- }
- // TCPSocket Code
- TCPSocket::TCPSocket()
- throw(SocketException) : CommunicatingSocket(SOCK_STREAM,
- IPPROTO_TCP) {
- }
- TCPSocket::TCPSocket(const string &foreignAddress, unsigned short foreignPort)
- throw(SocketException) : CommunicatingSocket(SOCK_STREAM, IPPROTO_TCP) {
- connect(foreignAddress, foreignPort);
- }
- TCPSocket::TCPSocket(int newConnSD) : CommunicatingSocket(newConnSD) {
- }
- // TCPServerSocket Code
- TCPServerSocket::TCPServerSocket(unsigned short localPort, int queueLen)
- throw(SocketException) : Socket(SOCK_STREAM, IPPROTO_TCP) {
- setLocalPort(localPort);
- setListen(queueLen);
- }
- TCPServerSocket::TCPServerSocket(const string &localAddress,
- unsigned short localPort, int queueLen)
- throw(SocketException) : Socket(SOCK_STREAM, IPPROTO_TCP) {
- setLocalAddressAndPort(localAddress, localPort);
- setListen(queueLen);
- }
- TCPSocket *TCPServerSocket::accept() throw(SocketException) {
- int newConnSD;
- if ((newConnSD = ::accept(sockDesc, NULL, 0)) < 0) {
- throw SocketException("Accept failed (accept())", true);
- }
- return new TCPSocket(newConnSD);
- }
- void TCPServerSocket::setListen(int queueLen) throw(SocketException) {
- if (listen(sockDesc, queueLen) < 0) {
- throw SocketException("Set listening socket failed (listen())", true);
- }
- }
- // UDPSocket Code
- UDPSocket::UDPSocket() throw(SocketException) : CommunicatingSocket(SOCK_DGRAM,
- IPPROTO_UDP) {
- setBroadcast();
- }
- UDPSocket::UDPSocket(unsigned short localPort) throw(SocketException) :
- CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) {
- setLocalPort(localPort);
- setBroadcast();
- }
- UDPSocket::UDPSocket(const string &localAddress, unsigned short localPort)
- throw(SocketException) : CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) {
- setLocalAddressAndPort(localAddress, localPort);
- setBroadcast();
- }
- void UDPSocket::setBroadcast() {
- // If this fails, we'll hear about it when we try to send. This will allow
- // system that cannot broadcast to continue if they don't plan to broadcast
- int broadcastPermission = 1;
- setsockopt(sockDesc, SOL_SOCKET, SO_BROADCAST,
- (raw_type *) &broadcastPermission, sizeof(broadcastPermission));
- }
- void UDPSocket::disconnect() throw(SocketException) {
- sockaddr_in nullAddr;
- memset(&nullAddr, 0, sizeof(nullAddr));
- nullAddr.sin_family = AF_UNSPEC;
- // Try to disconnect
- if (::connect(sockDesc, (sockaddr *) &nullAddr, sizeof(nullAddr)) < 0) {
- #ifdef WIN32
- if (errno != WSAEAFNOSUPPORT) {
- #else
- if (errno != EAFNOSUPPORT) {
- #endif
- throw SocketException("Disconnect failed (connect())", true);
- }
- }
- }
- void UDPSocket::sendTo(const void *buffer, int bufferLen,
- const string &foreignAddress, unsigned short foreignPort)
- throw(SocketException) {
- sockaddr_in destAddr;
- fillAddr(foreignAddress, foreignPort, destAddr);
- // Write out the whole buffer as a single message.
- if (sendto(sockDesc, (raw_type *) buffer, bufferLen, 0,
- (sockaddr *) &destAddr, sizeof(destAddr)) != bufferLen) {
- throw SocketException("Send failed (sendto())", true);
- }
- }
- int UDPSocket::recvFrom(void *buffer, int bufferLen, string &sourceAddress,
- unsigned short &sourcePort) throw(SocketException) {
- sockaddr_in clntAddr;
- socklen_t addrLen = sizeof(clntAddr);
- int rtn;
- if ((rtn = recvfrom(sockDesc, (raw_type *) buffer, bufferLen, 0,
- (sockaddr *) &clntAddr, (socklen_t *) &addrLen)) < 0) {
- throw SocketException("Receive failed (recvfrom())", true);
- }
- sourceAddress = inet_ntoa(clntAddr.sin_addr);
- sourcePort = ntohs(clntAddr.sin_port);
- return rtn;
- }
- void UDPSocket::setMulticastTTL(unsigned char multicastTTL) throw(SocketException) {
- if (setsockopt(sockDesc, IPPROTO_IP, IP_MULTICAST_TTL,
- (raw_type *) &multicastTTL, sizeof(multicastTTL)) < 0) {
- throw SocketException("Multicast TTL set failed (setsockopt())", true);
- }
- }
- void UDPSocket::joinGroup(const string &multicastGroup) throw(SocketException) {
- struct ip_mreq multicastRequest;
- multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str());
- multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);
- if (setsockopt(sockDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP,
- (raw_type *) &multicastRequest,
- sizeof(multicastRequest)) < 0) {
- throw SocketException("Multicast group join failed (setsockopt())", true);
- }
- }
- void UDPSocket::leaveGroup(const string &multicastGroup) throw(SocketException) {
- struct ip_mreq multicastRequest;
- multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str());
- multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);
- if (setsockopt(sockDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP,
- (raw_type *) &multicastRequest,
- sizeof(multicastRequest)) < 0) {
- throw SocketException("Multicast group leave failed (setsockopt())", true);
- }
- }