Jump to content

Problems with chat program

- - - - -

  • Please log in to reply
1 reply to this topic

#1
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Hi, I've been working on a chat program (or should I say two) that uses UDP. It's pretty basic, to use it you run the two programs on each computer (one of them receives message from the other computer and the other sends them to the other). I ran in to a problem though: Whenever I send a message to the receiver that has more than one word, with a space between, it only displays the first word on the receiver. As an added bonus, it makes the client look screwy. Here is the code for the two programs:
Receiver:

#include <windows.h>

#include <iostream>

#include <winsock2.h>

#include <string>


using namespace std;


int main()

{

	WSADATA wsaData;

	SOCKET RecvSocket;

	sockaddr_in RecvAddr;

	int Port = 2310;

	char msg[2048];

	sockaddr_in SenderAddr;

	int sas = sizeof(SenderAddr);

	WSAStartup(MAKEWORD(2,2),&wsaData);

	RecvSocket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);

	RecvAddr.sin_family = AF_INET;

	RecvAddr.sin_port = htons(Port);

	RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);

	bind(RecvSocket,(SOCKADDR*)&RecvAddr,sizeof(RecvAddr));

	cout << "Please wait...\n";

	recvfrom(RecvSocket,msg,2048,0,(SOCKADDR*)&SenderAddr,&sas);

	cout << "Finished Recieving. Message:\n" << msg << "\n";

	closesocket(RecvSocket);

	WSACleanup();

	cout << "Goodbye!\n";

	cin.get();

	return 0;

}

Sender:

#include <string>

#include <windows.h>

#include <winsock2.h>

using namespace std;



int main()

{

	WSADATA wsaData;

	SOCKET SendSocket;

	sockaddr_in RecvAddr;

	int Port = 2310;

	string msg;

	char temp[2048];

	string name = "mebob";

	int size;

	WSAStartup(MAKEWORD(2,2),&wsaData);

	SendSocket = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);

	RecvAddr.sin_family = AF_INET;

	RecvAddr.sin_port = htons(Port);

	RecvAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

	while(true)

	{

		msg = name + " > ";

		cout << name << " > ";

		cin >> temp;

		msg += temp;

		if ( msg == "@EXIT!" )

		{

			msg = "Exited";

		}

		size = msg.size() + name.size() + 3;

		sendto(SendSocket,msg.c_str(),size,0,(SOCKADDR*)&RecvAddr,sizeof(RecvAddr));

	}

	exit:

	closesocket(SendSocket);

	WSACleanup();

	cin.get();

	return 0;

}

Each input line on the client displays the name, a space a > character, then another space, and after that is where you enter the text.
Here is an example of the outputs:
Input to sender (after mebob > ):
testing
Output on receiver:
mebob > testing
That went as planned, but:
Input to sender:
testing testing
Output on receiver:
mebob > testing
It only displays the first word. And the added bonus is the next line on the client looks like this:
mebob > mebob > 
Any help would be appreciated.
Latinamne loqueris?

#2
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Looks like I solved my own problem. I had to load the input into an array first using cin.getline, then set a string equal to it. Why I felt like I needed to use the string class, I have no clue....
Latinamne loqueris?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users