socket

question:
i have wrote a program of chat, the program work in normale way; the problem, when i use to send from the client to the server , the buffer shows a whole sentence, in exemple you can understand my problem;
exemple;
client sent : HEY
server recive : HEYoi le mot de passe
so i must initializ the buffer don't show other characters;
i think i must use the loop '' for '' so i made : for( i=0;i<32;i++);
{
sendbuf[i]=0;
}
#include <iostream>
#include "winsock2.h"
using namespace std;
void main()
{
int bytesSent, bytesRecv = SOCKET_ERROR;
char sendbuf[32] = "envoi le mot de passe";
char recvbuf[32] ="";
char passbuf[6]="lmdrt";

//Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )cout<<" Erreur dans WSAStartup()\n";
//Create a socket;
SOCKET m_socket;
m_socket = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( m_socket == INVALID_SOCKET )
{
cout<<"Erreur dans socket():\n" <<WSAGetLastError();
WSACleanup();
return;
}
// bind the socket.
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr( "127.0.0.1" );
service.sin_port = htons( 27015 );

if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR )
{
cout<<"erreur dans bind().\n";
closesocket(m_socket);
return;
}
//listen on the socket.
if ( listen( m_socket, 1 ) == SOCKET_ERROR )
cout<<" Erreur dans listen() .\n";

//Aceept connections.
SOCKET AcceptSocket;
cout<<"attente de la connexion des clients...\n" ;
while (1) {
AcceptSocket = SOCKET_ERROR;
while ( AcceptSocket == SOCKET_ERROR )
{

AcceptSocket = accept(m_socket, NULL , NULL );
}
m_socket = AcceptSocket;
break;
}
cout<<"il faut le mot de passe pour se connecter \n";
bytesSent = send (m_socket, sendbuf, strlen(sendbuf), 0 );
bytesRecv = recv (m_socket, recvbuf, 32, 0 );
cout<<"message recu: "<<recvbuf <<"\n";
while(1)
{
cout<<"client:" <<recvbuf <<"\n";
cin >>sendbuf;
bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
bytesRecv = recv (m_socket, recvbuf, 32, 0 );
cout<<"text recu:"<< sendbuf << "\n" ;
}
int pass=0;
for (int i=0;i<5;i++)
{
if (passbuf [i]==recvbuf[i])pass++;
}
if(pass==5) cout<<"client Connecte.\n";
else
{
cout<<"le mot de passe est faut, le Client n'est pas connecte.\n";
closesocket(m_socket);
}
}
You should use the code format tags to format your code.

I've formatted and changed it a little, but once you establish a client connection, it's all wrong.

The server should just route messages between clients, so it must accept client connections and do stuff with it. There's an onging similar project here: http://www.cplusplus.com/forum/unices/147355/
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
#include <iostream>
#include <winsock2.h>

using namespace std;

int main()
{
	int bytesSent, bytesRecv = SOCKET_ERROR;
	const char sendbuf[] = "envoi le mot de passe";
	char recvbuf[32] = {};
	const char passbuf[] ="lmdrt";

	//Initialize Winsock.
	WSADATA wsaData;
	int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
	if (iResult != NO_ERROR)
	{
		cout << " Erreur dans WSAStartup()\n";
		return 1;
	}

	//Create a socket;
	SOCKET m_socket = socket ( AF_INET, SOCK_STREAM, IPPROTO_TCP );
	if (m_socket == INVALID_SOCKET)
	{
		cout << "Erreur dans socket():" << WSAGetLastError() << endl;
		WSACleanup();
		return 1;
	}

	// bind the socket.
	sockaddr_in service;
	service.sin_family = AF_INET;
	service.sin_addr.s_addr = inet_addr( "127.0.0.1" );
	service.sin_port = htons( 27015 );
	if (bind(m_socket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR)
	{
		cout<<"erreur dans bind()" << endl;
		closesocket(m_socket);
		return 1;
	}

	//listen on the socket.
	if (listen( m_socket, 1) == SOCKET_ERROR)
		cout<<" Erreur dans listen()\n" << endl;

	//Aceept connections.
	SOCKET AcceptSocket;
	cout << "attente de la connexion des clients..." << endl;
	while (1)
	{
		AcceptSocket = SOCKET_ERROR;
		while (AcceptSocket == SOCKET_ERROR)
		{
			sockaddr_in service;
			socklen_t sz = sizeof(service);
			AcceptSocket = accept(m_socket, (SOCKADDR*)&service, &sz);
		}
		m_socket = AcceptSocket;
		break;
	}
	cout << "il faut le mot de passe pour se connecter" << endl;

	// kbw: It's all wrong from here
	bytesSent = send(m_socket, sendbuf, strlen(sendbuf), 0);
	bytesRecv = recv(m_socket, recvbuf, 32, 0);

	cout << "message recu: " << recvbuf << endl;
	while (1)
	{
		cout << "client:" << recvbuf << endl;
		cin >> sendbuf;		// kbw: server reading stdin?
		bytesSent = send(m_socket, sendbuf, strlen(sendbuf), 0);
		bytesRecv = recv(m_socket, recvbuf, 32, 0);
		cout<<"text recu:"<< sendbuf << endl;
	}
	int pass=0;
	for (int i=0;i<5;i++)
	{
		if (passbuf [i]==recvbuf[i])
			pass++;
	}

	if (pass==5)
	{
		cout<<"client Connecte." << endl;
	}
	else
	{
		cout << "le mot de passe est faut, le Client n'est pas connecte" << endl;
		closesocket(m_socket);
	}
}

Topic archived. No new replies allowed.