3 Replies - 298 Views - Last Post: 10 June 2013 - 11:16 AM Rate Topic: -----

#1 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Server Not Displaying Client Message

Posted 10 June 2013 - 09:55 AM

For some reason, it isn't printing out the message that the client sends to the server.
I've checked all over the source code.

//Server:

#include "ReqLibs.h"
void SockPrep(std::string addr, int Sockport, int lanport);
void fileLogs(std::string SndLog);
int main()
{
    int port, lanport;
    std::string targetip;
    SetConsoleTitle("DarkMatter Chat - Server");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
    std::cout << "\t\t\tDarkMatter Chat - Server\n\t\t\t   DarkMatter@null.net\n\t\t\tCoded in C++ by DarkMatter\n";
    std::cout << "Enter the IP address of whom's server you'll be connecting to: ";
    getline(std::cin, targetip);
    std::cout << "\nEnter the port in which he is listening for your connection on: ";
    std::cin >> port;
    std::cout << "\nEnter the port thats open on your modem.";
    std::cin >> lanport;
    std::cout << "\nThank You.\n";
    SockPrep(targetip, port, lanport);
}
void SockPrep(std::string addr, int Sockport, int lanport)
{
    WSAData WSA;
    bool random = true;
    WORD SockVer = MAKEWORD(2,1);
    long WSAStart = WSAStartup(SockVer, &WSA);
    std::string snd, rcv;
    char msg[4000];
    SOCKADDR_IN SockAttr;
    SOCKADDR_IN ListenAttr;
    SOCKET sock_ = socket(AF_INET, SOCK_STREAM, NULL);
    SOCKET listen_ = socket(AF_INET, SOCK_STREAM, NULL);
    SockAttr.sin_addr.s_addr = inet_addr(addr.c_str());
    SockAttr.sin_family = AF_INET;
    SockAttr.sin_port = htons(Sockport);
    ListenAttr.sin_addr.s_addr = inet_addr("127.0.0.1");
    ListenAttr.sin_family = AF_INET;
    ListenAttr.sin_port = htons(lanport);
    int reverse__ = 0;
    while(random == true)
    {
        if(reverse__ != 0)
        {
        std::cout << "\nEnter a message you would like to send do the server(Max 4000 characters).\n\"";
        getline(std::cin, snd);
        std::cout << "\"\n";
        if(send(sock_, snd.c_str(), sizeof(snd.c_str()), NULL) != -1)
        {
            std::cout << "Sent Successfully!\n";
            fileLogs(snd);
            reverse__ = 0;
        } else {std::cout << "An error occured, unable to send.\n";}
        }
        if(reverse__ != 1)
        {

            bind(listen_, (SOCKADDR*)&ListenAttr, sizeof(ListenAttr));
            listen(listen_, 2);
            std::cout << "Listening for messages...\n";
            while(reverse__ != 1)
            {
                if(recv(listen_, msg, 1024, NULL) != -1)
                {
                    std::cout << "Message Received: \"" << msg << "\"";
                    reverse__ = 1;
                }
            }
        }

    }

}
void fileLogs(std::string SndLog)
{
    std::ofstream writeLog("Logs.txt");
    writeLog << "\n" << SndLog;
    writeLog.close();
}
/*
By DarkMatter/Ambitious
darkmatter@null.net*/


//ReqLibs.h
#ifndef REQLIBS_H_INCLUDED
#define REQLIBS_H_INCLUDED
#include <iostream>
#include <WinSock2.h>
#include <string>
#include <Windows.h>
#include "Color.h"
#include <ctime>
#include <fstream>
#endif // REQLIBS_H_INCLUDED


//Client
#include "ReqLibs.h"
void SockPrep(std::string addr, int Sockport, int lanport);
void fileLogs(std::string SndLog);
int main()
{
    int port, lanport;
    std::string targetip;
    SetConsoleTitle("DarkMatter Chat - Client");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
    std::cout << "\t\t\tDarkMatter Chat - Client\n\t\t\t   DarkMatter@null.net\n\t\t\tCoded in C++ by DarkMatter\n";
    std::cout << "Enter the IP address of whom's server you'll be connecting to: ";
    getline(std::cin, targetip);
    std::cout << "\nEnter the port in which he is listening for your connection on: ";
    std::cin >> port;
    std::cout << "\nEnter the port thats open on your modem.";
    std::cin >> lanport;
    std::cout << "\nThank You.\n";
    SockPrep(targetip, port, lanport);
}
void SockPrep(std::string addr, int Sockport, int lanport)
{
    WSAData WSA;
    WORD SockVer = MAKEWORD(2,1);
    long WSAStart = WSAStartup(SockVer, &WSA);
    std::string snd, rcv;
    char msg[4000];
    SOCKADDR_IN SockAttr;
    SOCKADDR_IN ListenAttr;
    SOCKET sock_ = socket(AF_INET, SOCK_STREAM, NULL);
    SOCKET listen_ = socket(AF_INET, SOCK_STREAM, NULL);
    SockAttr.sin_addr.s_addr = inet_addr(addr.c_str());
    SockAttr.sin_family = AF_INET;
    SockAttr.sin_port = htons(Sockport);
    ListenAttr.sin_addr.s_addr = inet_addr("127.0.0.1");
    ListenAttr.sin_family = AF_INET;
    ListenAttr.sin_port = htons(lanport);
    int reverse__ = 1;
    if(connect(sock_, (SOCKADDR*)&SockAttr, sizeof(msg)) != -1)
    {
        time_t t = time(0);
        std::cout << "Connected to " << addr << " on port " << Sockport << " @ " << t;
        if(reverse__ != 0)
        {
            std::cout << "\nEnter a message you would like to send do the server(Max 4000 characters).\n";
            getline(std::cin, snd);
            getline(std::cin, snd);
            //std::cout << "\n";
            while(reverse__ == 1)
            {
                if(send(sock_, snd.c_str(), sizeof(snd.c_str()), NULL) != -1)
                {
                    std::cout << "\nSent Successfully!\n";
                    fileLogs(snd);
                    reverse__ = 0;
                } else {std::cout << "An error occured, unable to send, press RETURN to exit.\n"; std::cin.get();}
            }
        }
        if(reverse__ != 1)
        {
            bind(listen_, (SOCKADDR*)&ListenAttr, sizeof(ListenAttr));
            listen(listen_, 2);
            std::cout << "Listening for messages...\n";
            while(reverse__ == 0)
            {
                if(recv(listen_, msg, 4000, NULL ) != -1)
                {
                    std::cout << "Message Received: \"" << msg << "\"";
                    reverse__ = 1;
                }
            }

        }

    } else {std::cout << "Unable to connect to the server, press RETURN to exit.\n"; std::cin.get();}

}
void fileLogs(std::string SndLog)
{
    std::ofstream writeLog("Logs.txt");
    writeLog << "\n" << SndLog;
    writeLog.close();
}
/*
By DarkMatter/Ambitious
darkmatter@null.net*/


This post has been edited by macosxnerd101: 10 June 2013 - 10:29 AM
Reason for edit:: Please use a descriptive title


Is This A Good Question/Topic? 0
  • +

Replies To: Server Not Displaying Client Message

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3650
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: Server Not Displaying Client Message

Posted 10 June 2013 - 10:12 AM

Did you check what WSAGetLastError() returns when the recv() call fails? That may lead you to the problem.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10781
  • View blog
  • Posts: 40,152
  • Joined: 27-December 08

Re: Server Not Displaying Client Message

Posted 10 June 2013 - 10:30 AM

Please use a descriptive title, not "Wtf? This is killing me."
Was This Post Helpful? 0
  • +
  • -

#4 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: Server Not Displaying Client Message

Posted 10 June 2013 - 11:16 AM

View PostSkydiver, on 10 June 2013 - 10:12 AM, said:

Did you check what WSAGetLastError() returns when the recv() call fails? That may lead you to the problem.

Sorry Macosnerd.
And, we might be on to something?
Posted Image
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1