From b4240cf1c80caf5e64dd444baf0f31429bb923d5 Mon Sep 17 00:00:00 2001 From: mdiluzio Date: Fri, 2 Jan 2015 15:13:05 +0000 Subject: [PATCH] pull in very simple client/server demo code inspired by http://www.linuxhowtos.org/C_C++/socket.htm --- source/client/client.cpp | 78 +++++++++++++++++++++++++++++++++++ source/client/client.h | 2 - source/client/main.cpp | 4 +- source/client/net.h | 9 +++++ source/client/server.cpp | 87 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 176 insertions(+), 4 deletions(-) diff --git a/source/client/client.cpp b/source/client/client.cpp index fdfeeb1..3e846de 100644 --- a/source/client/client.cpp +++ b/source/client/client.cpp @@ -1,7 +1,85 @@ #include "client.h" +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include "net.h" int runClient(int argc, char* argv[]) { + int sockfd; // socket File descriptor + int portno; // Port number + int n = 0; // return value for read and write calls + + struct sockaddr_in serv_addr; // Server address + + struct hostent *server; // pointer to host information + + char buffer[1028]; // buffer for socked read + memset(buffer,0,sizeof(buffer)); + + // must provide information + if (argc < 2) + { + fprintf(stderr,"usage %s hostname\n", argv[0]); + exit(0); + } + + // Get port number + portno = 11715; + + // Create a new socket + // AF_INET is general internetsocked domain + // SOCK_STREAM as messages will be read in on this socket, SOCK_DGRAM would be for packets + // 0 is for default protocol + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) + error("ERROR opening socket"); + + // Get the hostent information for the host by name + server = gethostbyname(argv[1]); + if (server == NULL) + error("ERROR, no such host"); + + // Empty the server address struct + memset(&serv_addr,0, sizeof(serv_addr)); + + // Set the server to AF_INET + serv_addr.sin_family = AF_INET; + + // copy the server address into our server_addr struct + memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length); + + // Set our server address port to the port number provided + serv_addr.sin_port = htons(portno); + + // Attempt to connect to the server using the socket and server address info + if (connect(sockfd, (const sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) + error("ERROR connecting"); + + while ( n >= 0 ) + { + // Get the message to send + printf("Please enter the message: "); + memset(buffer,0,sizeof(buffer)); + fgets(buffer,sizeof(buffer)-1,stdin); + + // Place a test message into the buffer + strncpy(buffer,buffer,sizeof(buffer)); + + // Write to the socket with the buffer + n = write(sockfd,buffer,strlen(buffer)); + if (n < 0) + error("ERROR writing to socket"); + } + return 0; } \ No newline at end of file diff --git a/source/client/client.h b/source/client/client.h index ab1e336..201c0d9 100644 --- a/source/client/client.h +++ b/source/client/client.h @@ -1,8 +1,6 @@ #ifndef _TTRTS_CLIENT_H_ #define _TTRTS_CLIENT_H_ -#include - int runClient(int argc, char* argv[]); #endif \ No newline at end of file diff --git a/source/client/main.cpp b/source/client/main.cpp index 218cab5..cfa69e3 100644 --- a/source/client/main.cpp +++ b/source/client/main.cpp @@ -24,9 +24,9 @@ int main(int argc, char* argv[]) // Either run the client, the server, or from local filesystem if( arg1 == "client" ) - return runClient(argc,argv+1); + return runClient(argc-1,argv+1); else if ( arg1 == "server" ) - return runServer(argc,argv+1); + return runServer(argc-1,argv+1); else return runFromFilesystem(argc,argv); diff --git a/source/client/net.h b/source/client/net.h index 26fdbf0..613486f 100644 --- a/source/client/net.h +++ b/source/client/net.h @@ -1,4 +1,13 @@ #ifndef _TTRTS_NET_H_ #define _TTRTS_NET_H_ +#include +#include + +inline void error(const char *msg) +{ + perror(msg); + exit(1); +} + #endif \ No newline at end of file diff --git a/source/client/server.cpp b/source/client/server.cpp index be07929..dfac689 100644 --- a/source/client/server.cpp +++ b/source/client/server.cpp @@ -1,6 +1,93 @@ #include "server.h" +#include +#include +#include +#include + +#include +#include + +#include + +#include "net.h" + int runServer(int argc, char* argv[]) { + int sockfd; // socket File descriptor + int newsockfd; // new socket File descriptor + + int portno; // Port number + socklen_t clilen; // length of client address + clilen = sizeof(sockaddr_in); + + int n = 0; // return value for read and write calls + + char buffer[1028]; // buffer for socked read + memset(buffer,0,sizeof(buffer)); + + struct sockaddr_in serv_addr; // Server address + struct sockaddr_in cli_addr; // Client address + + // Create a new socket + // AF_INET is general internetsocked domain + // SOCK_STREAM as messages will be read in on this socket, SOCK_DGRAM would be for packets + // 0 is for default protocol + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) + error("ERROR opening socket"); + + // empty the server address + memset(&serv_addr,0, sizeof(serv_addr)); + + // Grab the port number + portno = 11715; + + // Set the server address family to AF_INET + serv_addr.sin_family = AF_INET; + + // Set the port number + // htons swaps from host byte order to network byte order + serv_addr.sin_port = htons(portno); + + // The host for this address is this current machines's IP + // INADDR_ANY fetches this + serv_addr.sin_addr.s_addr = INADDR_ANY; + + // bind out socket to this server address + if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) + error("ERROR on binding"); + + // Listen on the socket for messages + // Second param is length of backlog queue, the maximum number of connections + // that can be waiting while the process is handling a single connection + // max is usually set to 5 + listen(sockfd,5); + + // accept waits for a connection from a client + // it returns a new socket file descriptor for this connection + // client information will be stored in cli_addr + newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); + if (newsockfd < 0) + error("ERROR on accept"); + + // loop + while( n >= 0 ) + { + // empty the buffer + memset(buffer,0,sizeof(buffer)); + + // Read in the new socket + // read will block until the client has called write + // up to the full size of the buffer + n = read(newsockfd,buffer,sizeof(buffer)-1); + if (n < 0) + error("ERROR reading from socket"); + + // print the message recieved + printf("%s",buffer); + } + + // Return return 0; } \ No newline at end of file