Receiving garbage winsock

Hello every one
I'm trying to send "Hello" from Client to server but the server receive garbage instead of "Hello". i tested my Client and with telnet and telnet receives Hello, so the problem is with my server. But i cant seem to figure out what is wrong with it. can you guys please look at it and tell me what did i do wrong ?
thanks

Server.h:
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
#pragma once
#include <winsock2.h>
#include <stdio.h>
#include <iostream>

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT 666
const int STRLEN = 256;

class Server
{
public:
	Server(void);
	~Server(void);

	void Listening();
	void Bind();
	void Accept();
	void Recv(char *RecvBuffer, int size);

private:
	WSADATA wsaData;
	SOCKET ListenSocket;
	SOCKET AcceptSocket;
	sockaddr_in service;
	int iResult;
};


Server.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
#include "Server.h"

Server::Server(void)
{
	//----------------------
	// Initialize Winsock.

	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != NO_ERROR) 
	{
		wprintf(L"WSAStartup failed with error: %ld\n", iResult);
	}


	//----------------------
	// Create a SOCKET for listening for
	// incoming connection requests.

	ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (ListenSocket == INVALID_SOCKET) 
	{
		wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
		WSACleanup();
	}
}

Server::~Server(void)
{
	WSACleanup();
}

void Server::Bind()
{
	//----------------------
	// The sockaddr_in structure specifies the address family,
	// IP address, and port for the socket that is being bound.

	service.sin_family = AF_INET;
	service.sin_addr.s_addr = inet_addr("127.0.0.1");
	service.sin_port = htons(DEFAULT_PORT);

	if ( bind(ListenSocket,(SOCKADDR *) & service, sizeof (service)) == SOCKET_ERROR) 
	{
		wprintf(L"bind failed with error: %ld\n", WSAGetLastError());
		closesocket(ListenSocket);
		WSACleanup();
    }
}

void Server::Listening()
{
	//----------------------
	// Listen for incoming connection requests.
	// on the created socket
    
	if (listen(ListenSocket, 1) == SOCKET_ERROR) 
	{
		wprintf(L"listen failed with error: %ld\n", WSAGetLastError());
		closesocket(ListenSocket);
		WSACleanup();
	}
}

void Server::Accept()
{
	//----------------------
	// Create a SOCKET for accepting incoming requests.

    wprintf(L"Waiting for client to connect...\n");

	//----------------------
	// Accept the connection.
	
	AcceptSocket = accept(ListenSocket, NULL, NULL);
	if (AcceptSocket == INVALID_SOCKET) 
	{
		wprintf(L"accept failed with error: %ld\n", WSAGetLastError());
		closesocket(ListenSocket);
		WSACleanup();

    }
	else
		wprintf(L"Client connected.\n");
}

void Server::Recv(char *RecvBuffer, int size)
{
	recv( ListenSocket, RecvBuffer, size, 0);
}


Main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Server.h"

int main()
{
	 char recMessage[STRLEN];

	Server Server;

	Server.Bind();
	Server.Listening();
	Server.Accept();
	Server.Recv(recMessage, STRLEN);

	std::cout << recMessage;

	system("PAUSE");
	return 0;
}
Last edited on
Sounds like a little endian big endian issue
Topic archived. No new replies allowed.