WinSock server is not receiving 2nd message...

Here is the source for the server

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "First.h"

using namespace std;

//Globals
string ip = "127.0.0.1";
int port = 7742;
string sending;
char message[200];
string strmessage;
SOCKET sConnect;

//Protos
void sendthis(string sending);
void printme(string str);
void btokens(char str[]);

int doit(int, char **)
{
    char ac[80];
    if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR) {
        cerr << "Error " << WSAGetLastError() <<
                " when getting local host name." << endl;
        return 1;
    }
    cout << "User: " << ac << "." << endl;

    struct hostent *phe = gethostbyname(ac);
    if (phe == 0) {
        cerr << "Yow! Bad host lookup." << endl;
        return 1;
    }

    for (int i = 0; phe->h_addr_list[i] != 0; ++i) {
        struct in_addr addr;
        memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr));
        cout << "Local Address: " << inet_ntoa(addr) << endl;
    }

	cout<<"Hosting Address: "<<ip<<endl;

    return 0;
}

//Main!!

void main(int argc, char *argv[]){

	long answer;
	WSAData wsaData;
	WORD version;

	version = MAKEWORD(2,1);

	answer = WSAStartup( version, &wsaData );

	SOCKADDR_IN addr;

	int addrlen = sizeof(addr);

	//Creating socket listeners and connectors
	SOCKET sListen;

	sConnect = socket(AF_INET, SOCK_STREAM, NULL);

	//Structure
	//IP address
	addr.sin_addr.s_addr = inet_addr(ip.c_str());
	addr.sin_family = AF_INET;
	//Choose any port between 1000-6000
	addr.sin_port = htons(port);

	//Setting up sListen
	sListen = socket(AF_INET, SOCK_STREAM, NULL);
	//bind() is binding the port and IP address
	bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
	listen(sListen, SOMAXCONN);
	//SOMAXCONN means the server is listening without any limit

	//What the server should do if a connection is found
    int retval = doit(argc, argv);
	
	//Skip a line
	cout<<""<<endl;

	//Creating endless loop
	for(;;){
		printme("Waiting for incoming connections");
		//If connection is found
		if(sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen)){
			answer = getpeername(sConnect, (SOCKADDR*)&addr, &addrlen);
			cout<<"Connection found @: "<<answer<<endl;
			//Receiving
			answer = recv(sConnect, message, sizeof(message), NULL);
			//convert message to string
			strmessage = message;
			//print message
			printme(strmessage);
			if(strmessage == "Login needed"){
				sendthis("Please login");
			} else if(strmessage.find("<!!splithere!!>")){
				printme(strmessage);
				btokens(message); 
			} else {
					printme("Error");
				}
		}

	}
};

			//Printing client data
			//cout<<"Connection found @: "<<answer<<endl;

void sendthis(string sending){
				send(sConnect, sending.c_str(), sizeof(sending)+1, NULL);
};

void printme(string str){
	cout<<str<<endl;
};

void btokens(char str[]){
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str,"<!!splithere!!>");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, "<!!splithere!!>");
  }
};


Now here is the clients main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "First.h"

using namespace std;

//Globals
string ip = "127.0.0.1";
int port = 7742;
string sending;
string sending1;
char message[200];
string strmessage;
SOCKET sConnect;

//Protos
void sendthis(string sending);
void printme(string str);

//Main function
void main(){

	long answer;
	WSAData wsaData;
	WORD DLLVersion;
	DLLVersion = MAKEWORD(2,1);
	answer = WSAStartup(DLLVersion, &wsaData);

	//Structure
	SOCKADDR_IN addr;

	int addrlen = sizeof(addr);

	//Setting up socket

	sConnect = socket(AF_INET, SOCK_STREAM, NULL);
	//IP address
	addr.sin_addr.s_addr = inet_addr(ip.c_str());
	addr.sin_family = AF_INET;
	//Ports
	addr.sin_port = htons(port);

	//Start game
	connect(sConnect, (SOCKADDR*)&addr, sizeof(addr));
	//Asking for login message
	sendthis("Login needed");

	while(1){
	//Recieving login message
	answer = recv(sConnect, message, sizeof(message), NULL);
	strmessage = message;

	if(strmessage == "Please login"){
		printme("Please login: \nUsername: ");
		cin>>sending;
		printme("Password: ");
		cin>>sending1;
		string send12 = sending + "<!!splithere!!>" + sending1;
		sendthis(send12);
	} else {
		cout<<"Error"<<endl;
	}
	}
};

void sendthis(string sending){
	send(sConnect, sending.c_str(), sizeof(sending)+1, NULL);
};

void printme(string str){
	cout<<str<<endl;
};
Why oh why are you using global variables?

It would help you if you declared variables where they're used.

It's impossible to debug a client/server app without seeing both sides.

Have you thought about confirming what is sent and is received?

Finally, this:
1
2
3
void sendthis(string sending){
	send(sConnect, sending.c_str(), sizeof(sending)+1, NULL);
};

should be this:
1
2
3
4
void sendthis(const string &sending)
{
	send(sConnect, sending.c_str(), sending.size(), NULL);
}
Last edited on
Line 90 in your server should be outside your for loop. Your "server" application will wait on a call to accept() until the "client" attempts to connect by calling the connect() function, my guess is that this is why you don't see your second message. In the same vein, the peer name won't change either so move Line 91 outside of the loop as well.

You may or may not find it easier to use the substr() function (http://www.cplusplus.com/reference/string/basic_string/substr/) then to split strings like you are with that btokens() function. YMMV

Line 84 Server App: The output to "skip a line" is '\n' not "". I'm not sure why you want to do this here but there it is.

EDIT: I can see why you want to use global variables here, but you're not doing yourself any favors. It will become a nightmare if you try to make that server application multithreaded, which the overwhelming majority of server applications like these are.

EDIT2: Don't forget to call WSACleanup(): http://msdn.microsoft.com/en-us/library/windows/desktop/ms741549(v=vs.85).aspx this is just good practice.
Last edited on
Topic archived. No new replies allowed.