10 Replies - 1174 Views - Last Post: 03 October 2012 - 10:20 AM Rate Topic: -----

#1 computerguy3100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-June 12

C++ Instant Messenger Client Crashes

Posted 25 September 2012 - 01:54 AM

Hello everyone, I have written a command line IM program for Windows 8 using Visual C++ 2010, and what happens is that the client connects to the server ok, and sends the first message, however when I try to send a message from the server back to the client it does not work.

Here is the code for the client:
#include "stdafx.h"
#include "YourName.h"
#include <string>

using namespace std;

	int x;
void main( )
{
string confirm;
string strmessage;
char message[200];
	long answer;
	WSAData wsaData;
	WORD DLLVERSION;
	DLLVERSION = MAKEWORD (2,1);

	answer = WSAStartup(DLLVERSION, &wsaData);

	SOCKADDR_IN addr;

	int addrlen = sizeof(addr);
	char text[20];
	SOCKET sListen;

	std::cout << "Please enter the IPv4 address of your chat mate." << std::endl;
	SOCKET sConnect;
	sConnect = socket(AF_INET,SOCK_STREAM,NULL);
	addr.sin_addr.s_addr = inet_addr (gets(text));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(80);

	sListen = socket(AF_INET,SOCK_STREAM,NULL);
	bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
	listen(sListen, SOMAXCONN);

	std::cout << "Searching for your chat buddy." << std::endl;
	for(;;)/>
	{
		
		
		if(sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen))
		{
			std::cout << "Connected! To send a message, type it and then press enter to send." << std::endl;
			answer  = send(sConnect,		gets(text), 12, NULL);
			answer = recv(sConnect,		message, sizeof(message), NULL);
			
		}

	}
}



And here is the code for the server:
#include "stdafx.h"
#include "EnterYourName.h"
#include <string>

using namespace std;

void main( )
{
	string confirm;
	string strmessage;
	char message[200];
	long answer;
	WSAData wsaData;
	WORD DLLVersion;
	DLLVersion = MAKEWORD(2,1);
	answer = WSAStartup(DLLVersion, &wsaData);

	SOCKADDR_IN addr;
	int addrlen = sizeof(addr);

	std::cout << "Please enter the internal IP address of the person you'd like to chat with." << std::endl;

	SOCKET sConnect;
	char text[200];
	sConnect = socket(AF_INET,		SOCK_STREAM,NULL);
	addr.sin_addr.s_addr = inet_addr(gets(text));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(80);

	for(;;)/>	
	connect(sConnect,		(SOCKADDR*)&addr, sizeof(addr));
	answer = recv(sConnect,		message, sizeof(message), NULL);
	strmessage = message;
	std::cout << strmessage << std::endl;

	getchar();
			
			
	}


Any help would be appreciated. Thank you!

Is This A Good Question/Topic? 0
  • +

Replies To: C++ Instant Messenger Client Crashes

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3172
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: C++ Instant Messenger Client Crashes

Posted 25 September 2012 - 02:48 AM

Where does it crash in the client? All you said was that it doesn't work, and hinted in your subject title that it crashes.
Was This Post Helpful? 0
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1578
  • View blog
  • Posts: 3,005
  • Joined: 30-May 10

Re: C++ Instant Messenger Client Crashes

Posted 25 September 2012 - 02:50 AM

1. Stop mixing C I/O and C++ I/O on the same stream.
2. STOP using gets()
3. Your main must return an int, even if you have a "forever" loop in your code.
4. One of the for(;;) loops needs some extra { } to make it obvious what you're doing.

Edit:
Dang smilies in inline code

This post has been edited by Salem_c: 25 September 2012 - 02:51 AM

Was This Post Helpful? 1
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3172
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: C++ Instant Messenger Client Crashes

Posted 25 September 2012 - 02:54 AM

Also, your client code looks suspiciously like server code with its use of accept(), while your server code looks awfully like client code where all it does is try to connect and receive.

This post has been edited by Skydiver: 25 September 2012 - 02:56 AM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5958
  • View blog
  • Posts: 23,227
  • Joined: 23-August 08

Re: C++ Instant Messenger Client Crashes

Posted 25 September 2012 - 03:11 AM

Learn to check the return values of functions before blindly continuing on assuming they succeeded.
Was This Post Helpful? 0
  • +
  • -

#6 computerguy3100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-June 12

Re: C++ Instant Messenger Client Crashes

Posted 25 September 2012 - 11:04 AM

View PostSkydiver, on 25 September 2012 - 02:54 AM, said:

Also, your client code looks suspiciously like server code with its use of accept(), while your server code looks awfully like client code where all it does is try to connect and receive.


Here is the code where the client connects to the server
std::cout << "Connected! To send a message, type it and then press enter to send." << std::endl;
            answer  = send(sConnect,        gets(text), 12, NULL);
            answer = recv(sConnect,     message, sizeof(message), NULL);



And here is the code where the server is supposed to send a message back to the client:
for(;;)/>
    connect(sConnect,       (SOCKADDR*)&addr, sizeof(addr));
    answer = recv(sConnect,     message, sizeof(message), NULL);
    strmessage = message;
    std::cout << strmessage << std::endl;



thanks!
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3172
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: C++ Instant Messenger Client Crashes

Posted 25 September 2012 - 11:41 AM

You are still missing the the curly braces around your for-ever loop for your "server" as Salem_c originally noted.

Your definition of a client and of a server seems to be different from the common industry understanding of the terms. Typically a server offers up open ports that it listens for incoming connections. See this blog post for an example of a very simple echo server: http://www.paulgriff.../c/echoserv.php
Was This Post Helpful? 0
  • +
  • -

#8 computerguy3100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-June 12

Re: C++ Instant Messenger Client Crashes

Posted 25 September 2012 - 01:02 PM

View PostSkydiver, on 25 September 2012 - 11:41 AM, said:

You are still missing the the curly braces around your for-ever loop for your "server" as Salem_c originally noted.

Your definition of a client and of a server seems to be different from the common industry understanding of the terms. Typically a server offers up open ports that it listens for incoming connections. See this blog post for an example of a very simple echo server: http://www.paulgriff.../c/echoserv.php


I tried putting { } around my for(;;) and it didn't help. Could it be that I have not properly put a send command in the client code?

View Postcomputerguy3100, on 25 September 2012 - 01:01 PM, said:

View PostSkydiver, on 25 September 2012 - 11:41 AM, said:

You are still missing the the curly braces around your for-ever loop for your "server" as Salem_c originally noted.

Your definition of a client and of a server seems to be different from the common industry understanding of the terms. Typically a server offers up open ports that it listens for incoming connections. See this blog post for an example of a very simple echo server: http://www.paulgriff.../c/echoserv.php


I tried putting { } around my for(;;) and it didn't help. Could it be that I have not properly put a send command in the client code?

Was This Post Helpful? 0
  • +
  • -

#9 computerguy3100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-June 12

Re: C++ Instant Messenger Client Crashes

Posted 02 October 2012 - 01:02 AM

View Postcomputerguy3100, on 25 September 2012 - 01:02 PM, said:

View PostSkydiver, on 25 September 2012 - 11:41 AM, said:

You are still missing the the curly braces around your for-ever loop for your "server" as Salem_c originally noted.

Your definition of a client and of a server seems to be different from the common industry understanding of the terms. Typically a server offers up open ports that it listens for incoming connections. See this blog post for an example of a very simple echo server: http://www.paulgriff.../c/echoserv.php


I tried putting { } around my for(;;) and it didn't help. Could it be that I have not properly put a send command in the client code?

View Postcomputerguy3100, on 25 September 2012 - 01:01 PM, said:

View PostSkydiver, on 25 September 2012 - 11:41 AM, said:

You are still missing the the curly braces around your for-ever loop for your "server" as Salem_c originally noted.

Your definition of a client and of a server seems to be different from the common industry understanding of the terms. Typically a server offers up open ports that it listens for incoming connections. See this blog post for an example of a very simple echo server: http://www.paulgriff.../c/echoserv.php


I tried putting { } around my for(;;) and it didn't help. Could it be that I have not properly put a send command in the client code?


thanks I got it working. Now, does anybody know how I could add encryption into this?

This post has been edited by computerguy3100: 02 October 2012 - 01:03 AM

Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3172
  • View blog
  • Posts: 9,610
  • Joined: 05-May 12

Re: C++ Instant Messenger Client Crashes

Posted 02 October 2012 - 09:05 AM

You can as simple as using ROT13, to using the characters of the user name as source for XOR'ing the data, to synchronizing a random number generator on the server and client to build a one time pad for XOrs, and the way to as complicated as negotiating a session key between the client and server and then using that to use a AES.
Was This Post Helpful? 0
  • +
  • -

#11 computerguy3100  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-June 12

Re: C++ Instant Messenger Client Crashes

Posted 03 October 2012 - 10:20 AM

View PostSkydiver, on 02 October 2012 - 09:05 AM, said:

You can as simple as using ROT13, to using the characters of the user name as source for XOR'ing the data, to synchronizing a random number generator on the server and client to build a one time pad for XOrs, and the way to as complicated as negotiating a session key between the client and server and then using that to use a AES.


Thank you. Do you know where I could find a tutorial for AES encryption and server keys?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1