first commit
This commit is contained in:
19
network more complicated/Makefile
Normal file
19
network more complicated/Makefile
Normal file
@@ -0,0 +1,19 @@
|
||||
# Compiler and flags
|
||||
CXX = g++
|
||||
CXXFLAGS = -Wall -Wextra -O2 -std=c++17
|
||||
|
||||
# Targets
|
||||
all: client server
|
||||
|
||||
# Client build
|
||||
client: client.cpp
|
||||
$(CXX) $(CXXFLAGS) client.cpp -o client
|
||||
|
||||
# Server build
|
||||
server: server.cpp
|
||||
$(CXX) $(CXXFLAGS) server.cpp -o server
|
||||
|
||||
# Clean build artifacts
|
||||
clean:
|
||||
rm -f client server
|
||||
|
||||
BIN
network more complicated/client
Executable file
BIN
network more complicated/client
Executable file
Binary file not shown.
86
network more complicated/client.cpp
Normal file
86
network more complicated/client.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
/*this network test uses more complicated network functionality but doesnt use threads or ncurses.
|
||||
that means you have to wait for the other end to send a message before getting to write a new message
|
||||
to run, do ./client.cpp 127.0.0.1 8080
|
||||
this one has args
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/uio.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/wait.h>
|
||||
#include <fcntl.h>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
//Client side
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
//we need 2 things: ip address and port number, in that order
|
||||
if(argc != 3)
|
||||
{
|
||||
cerr << "Usage: ip_address port" << endl; exit(0);
|
||||
} //grab the IP address and port number
|
||||
char *serverIp = argv[1]; int port = atoi(argv[2]);
|
||||
//create a message buffer
|
||||
char msg[1500];
|
||||
//setup a socket and connection tools
|
||||
struct hostent* host = gethostbyname(serverIp);
|
||||
sockaddr_in sendSockAddr;
|
||||
bzero((char*)&sendSockAddr, sizeof(sendSockAddr));
|
||||
sendSockAddr.sin_family = AF_INET;
|
||||
sendSockAddr.sin_addr.s_addr =
|
||||
inet_addr(inet_ntoa(*(struct in_addr*)*host->h_addr_list));
|
||||
sendSockAddr.sin_port = htons(port);
|
||||
int clientSd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
//try to connect...
|
||||
int status = connect(clientSd,
|
||||
(sockaddr*) &sendSockAddr, sizeof(sendSockAddr));
|
||||
if(status < 0)
|
||||
{
|
||||
cout<<"Error connecting to socket!"<<endl;// break;
|
||||
}
|
||||
cout << "Connected to the server!" << endl;
|
||||
int bytesRead, bytesWritten = 0;
|
||||
struct timeval start1, end1;
|
||||
gettimeofday(&start1, NULL);
|
||||
while(1)
|
||||
{
|
||||
cout << ">";
|
||||
string data;
|
||||
getline(cin, data);
|
||||
memset(&msg, 0, sizeof(msg));//clear the buffer
|
||||
strcpy(msg, data.c_str());
|
||||
if(data == "exit")
|
||||
{
|
||||
send(clientSd, (char*)&msg, strlen(msg), 0);
|
||||
break;
|
||||
}
|
||||
bytesWritten += send(clientSd, (char*)&msg, strlen(msg), 0);
|
||||
cout << "Awaiting server response..." << endl;
|
||||
memset(&msg, 0, sizeof(msg));//clear the buffer
|
||||
bytesRead += recv(clientSd, (char*)&msg, sizeof(msg), 0);
|
||||
if(!strcmp(msg, "exit"))
|
||||
{
|
||||
cout << "Server has quit the session" << endl;
|
||||
break;
|
||||
}
|
||||
cout << "Server: " << msg << endl;
|
||||
}
|
||||
gettimeofday(&end1, NULL);
|
||||
close(clientSd);
|
||||
cout << "********Session********" << endl;
|
||||
cout << "Bytes written: " << bytesWritten <<
|
||||
" Bytes read: " << bytesRead << endl;
|
||||
cout << "Elapsed time: " << (end1.tv_sec- start1.tv_sec)
|
||||
<< " secs" << endl;
|
||||
cout << "Connection closed" << endl;
|
||||
return 0;
|
||||
}
|
||||
BIN
network more complicated/server
Executable file
BIN
network more complicated/server
Executable file
Binary file not shown.
117
network more complicated/server.cpp
Normal file
117
network more complicated/server.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
/*this network test uses more complicated network functionality but doesnt use threads or ncurses.
|
||||
that means you have to wait for the other end to send a message before getting to write a new message
|
||||
to run, do ./server.cpp 8080
|
||||
this one has args
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/uio.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/wait.h>
|
||||
#include <fcntl.h>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
//Server side
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
//for the server, we only need to specify a port number
|
||||
if(argc != 2)
|
||||
{
|
||||
cerr << "Usage: port" << endl;
|
||||
exit(0);
|
||||
}
|
||||
//grab the port number
|
||||
int port = atoi(argv[1]);
|
||||
//buffer to send and receive messages with
|
||||
char msg[1500];
|
||||
|
||||
//setup a socket and connection tools
|
||||
sockaddr_in servAddr;
|
||||
bzero((char*)&servAddr, sizeof(servAddr));
|
||||
servAddr.sin_family = AF_INET;
|
||||
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
servAddr.sin_port = htons(port);
|
||||
|
||||
//open stream oriented socket with internet address
|
||||
//also keep track of the socket descriptor
|
||||
int serverSd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(serverSd < 0)
|
||||
{
|
||||
cerr << "Error establishing the server socket" << endl;
|
||||
exit(0);
|
||||
}
|
||||
//bind the socket to its local address
|
||||
int bindStatus = bind(serverSd, (struct sockaddr*) &servAddr,
|
||||
sizeof(servAddr));
|
||||
if(bindStatus < 0)
|
||||
{
|
||||
cerr << "Error binding socket to local address" << endl;
|
||||
exit(0);
|
||||
}
|
||||
cout << "Waiting for a client to connect..." << endl;
|
||||
//listen for up to 5 requests at a time
|
||||
listen(serverSd, 5);
|
||||
//receive a request from client using accept
|
||||
//we need a new address to connect with the client
|
||||
sockaddr_in newSockAddr;
|
||||
socklen_t newSockAddrSize = sizeof(newSockAddr);
|
||||
//accept, create a new socket descriptor to
|
||||
//handle the new connection with client
|
||||
int newSd = accept(serverSd, (sockaddr *)&newSockAddr, &newSockAddrSize);
|
||||
if(newSd < 0)
|
||||
{
|
||||
cerr << "Error accepting request from client!" << endl;
|
||||
exit(1);
|
||||
}
|
||||
cout << "Connected with client!" << endl;
|
||||
//lets keep track of the session time
|
||||
struct timeval start1, end1;
|
||||
gettimeofday(&start1, NULL);
|
||||
//also keep track of the amount of data sent as well
|
||||
int bytesRead, bytesWritten = 0;
|
||||
while(1)
|
||||
{
|
||||
//receive a message from the client (listen)
|
||||
cout << "Awaiting client response..." << endl;
|
||||
memset(&msg, 0, sizeof(msg));//clear the buffer
|
||||
bytesRead += recv(newSd, (char*)&msg, sizeof(msg), 0);
|
||||
if(!strcmp(msg, "exit"))
|
||||
{
|
||||
cout << "Client has quit the session" << endl;
|
||||
break;
|
||||
}
|
||||
cout << "Client: " << msg << endl;
|
||||
cout << ">";
|
||||
string data;
|
||||
getline(cin, data);
|
||||
memset(&msg, 0, sizeof(msg)); //clear the buffer
|
||||
strcpy(msg, data.c_str());
|
||||
if(data == "exit")
|
||||
{
|
||||
//send to the client that server has closed the connection
|
||||
send(newSd, (char*)&msg, strlen(msg), 0);
|
||||
break;
|
||||
}
|
||||
//send the message to client
|
||||
bytesWritten += send(newSd, (char*)&msg, strlen(msg), 0);
|
||||
}
|
||||
//we need to close the socket descriptors after we're all done
|
||||
gettimeofday(&end1, NULL);
|
||||
close(newSd);
|
||||
close(serverSd);
|
||||
cout << "********Session********" << endl;
|
||||
cout << "Bytes written: " << bytesWritten << " Bytes read: " << bytesRead << endl;
|
||||
cout << "Elapsed time: " << (end1.tv_sec - start1.tv_sec)
|
||||
<< " secs" << endl;
|
||||
cout << "Connection closed..." << endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user