/* simple-client.c * * Copyright (c) 2000 Sean Walton and Macmillan Publishers. Use may be in * whole or in part in accordance to the General Public License (GPL). * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /*****************************************************************************/ /*** client.c ***/ /*** ***/ /*****************************************************************************/ /************************************************************************** * This is a simple client socket reader. It opens a socket, connects * to a server, reads the message, and closes. **************************************************************************/ #include #include #include #include // Needed for htons #include #include #include // Needed for inet_aton #include #include #include #include using namespace std; #define PORT 9999 #define SERVER_ADDR "127.0.0.1" /* localhost */ #define MAXBUF 1024 int main() { int sockfd; struct sockaddr_in dest; char buffer[MAXBUF]; /*---Open socket for streaming---*/ if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 ) { perror("Socket"); exit(errno); } /*---Initialize server address/port struct---*/ bzero(&dest, sizeof(dest)); dest.sin_family = AF_INET; dest.sin_port = htons(PORT); if ( inet_aton(SERVER_ADDR, (in_addr*)&dest.sin_addr.s_addr) == 0 ) { perror(SERVER_ADDR); exit(errno); } /*---Connect to server---*/ if ( connect(sockfd, (struct sockaddr*)&dest, sizeof(dest)) != 0 ) { perror("Connect "); exit(errno); } // Send a message to the server string message = "This is my message. Have a nice day!\n"; send( sockfd, message.c_str(), message.length(), 0 ); // Receive the message back from the server bzero(buffer, MAXBUF); int bytes = recv(sockfd, buffer, sizeof(buffer), 0); buffer[bytes] = '\0'; cout << buffer << endl; /*---Clean up---*/ close(sockfd); return 0; }