Revert back to cleaner copy of server code
This commit is contained in:
parent
83b9990bcc
commit
dcb9d68fb4
3 changed files with 21 additions and 18 deletions
|
@ -17,9 +17,9 @@ set( SOURCES
|
||||||
)
|
)
|
||||||
|
|
||||||
# Set defaults for ttrts variables
|
# Set defaults for ttrts variables
|
||||||
set( TTRTS_MAPS "/usr/local/share/ttrts/maps/" )
|
set( TTRTS_MAPS "/usr/local/share/ttrts/maps/" )
|
||||||
set( TTRTS_GAMES "/tmp/" )
|
set( TTRTS_GAMES "/tmp/" )
|
||||||
set( TTRTS_PORT 11715 )
|
set( TTRTS_PORT 11715 )
|
||||||
|
|
||||||
# define these defaults in code
|
# define these defaults in code
|
||||||
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTTRTS_MAPS=${TTRTS_MAPS}" )
|
set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTTRTS_MAPS=${TTRTS_MAPS}" )
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#include "client.h"
|
#include "client.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -49,6 +51,8 @@ int runClient(int argc, char* argv[])
|
||||||
if (server == NULL)
|
if (server == NULL)
|
||||||
error("ERROR, no such host");
|
error("ERROR, no such host");
|
||||||
|
|
||||||
|
std::cout<<"Connecting to "<<argv[1]<<std::endl;
|
||||||
|
|
||||||
// Empty the server address struct
|
// Empty the server address struct
|
||||||
memset(&serv_addr,0, sizeof(serv_addr));
|
memset(&serv_addr,0, sizeof(serv_addr));
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -9,21 +11,21 @@
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
|
||||||
int runServer(int argc, char* argv[])
|
int runServer(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
// Server side information
|
sockaddr_in serv_addr; // Server address
|
||||||
int sockfd; // socket File descriptor
|
int sockfd; // socket File descriptor
|
||||||
sockaddr_in serv_addr; // Server address
|
int portno = TTRTS_PORT; // Port number
|
||||||
int portno; // Port number
|
|
||||||
|
|
||||||
// information for each client
|
struct sockaddr_in cli_addr; // Client address
|
||||||
sockaddr_in cli_addr; // Client address
|
int newsockfd; // new socket File descriptor
|
||||||
socklen_t clilen; // length of client address
|
socklen_t clilen; // length of client address
|
||||||
int clientsockfd; // new socket File descriptor
|
|
||||||
|
|
||||||
|
clilen = sizeof(sockaddr_in);
|
||||||
|
|
||||||
int n = 0; // return value for read and write calls
|
int n = 0; // return value for read and write calls
|
||||||
|
|
||||||
|
@ -32,7 +34,7 @@ int runServer(int argc, char* argv[])
|
||||||
|
|
||||||
|
|
||||||
// Create a new socket
|
// Create a new socket
|
||||||
// AF_INET is general internet socket domain
|
// AF_INET is general internetsocked domain
|
||||||
// SOCK_STREAM as messages will be read in on this socket, SOCK_DGRAM would be for packets
|
// SOCK_STREAM as messages will be read in on this socket, SOCK_DGRAM would be for packets
|
||||||
// 0 is for default protocol
|
// 0 is for default protocol
|
||||||
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
@ -41,10 +43,7 @@ int runServer(int argc, char* argv[])
|
||||||
|
|
||||||
// empty the server address
|
// empty the server address
|
||||||
memset(&serv_addr,0, sizeof(serv_addr));
|
memset(&serv_addr,0, sizeof(serv_addr));
|
||||||
|
|
||||||
// Grab the port number
|
|
||||||
portno = TTRTS_PORT;
|
|
||||||
|
|
||||||
// Set the server address family to AF_INET
|
// Set the server address family to AF_INET
|
||||||
serv_addr.sin_family = AF_INET;
|
serv_addr.sin_family = AF_INET;
|
||||||
|
|
||||||
|
@ -69,8 +68,8 @@ int runServer(int argc, char* argv[])
|
||||||
// accept waits for a connection from a client
|
// accept waits for a connection from a client
|
||||||
// it returns a new socket file descriptor for this connection
|
// it returns a new socket file descriptor for this connection
|
||||||
// client information will be stored in cli_addr
|
// client information will be stored in cli_addr
|
||||||
clientsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
|
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
|
||||||
if (clientsockfd < 0)
|
if (newsockfd < 0)
|
||||||
error("ERROR on accept");
|
error("ERROR on accept");
|
||||||
|
|
||||||
// loop
|
// loop
|
||||||
|
@ -82,7 +81,7 @@ int runServer(int argc, char* argv[])
|
||||||
// Read in the new socket
|
// Read in the new socket
|
||||||
// read will block until the client has called write
|
// read will block until the client has called write
|
||||||
// up to the full size of the buffer
|
// up to the full size of the buffer
|
||||||
n = read(clientsockfd,buffer,sizeof(buffer)-1);
|
n = read(newsockfd,buffer,sizeof(buffer)-1);
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
error("ERROR reading from socket");
|
error("ERROR reading from socket");
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue