Sign in to follow this  
FantasyVI

TCP server receiving grabage

Recommended Posts

Hello everyone
i have a problem with my server, it receives garbage instead of a string.
i look into someone eles code and found that he added this line of code that fix this problem but i dont know how or why it fixes this problem.
can someone please explain this line of code

[code]ListenSocket = AcceptSocket;[/code]

Server.h
[code]
private:
WSADATA wsaData;

SOCKET ListenSocket;
SOCKET AcceptSocket;
SOCKET myBackup;

sockaddr_in service;
int iResult;
[/code]

[code]
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");
ListenSocket = AcceptSocket;
}
[/code]

Share this post


Link to post
Share on other sites
That doesn't make any sense whatsoever, and if that "fixes" it the code might have more problems. Sounds like perhaps you're trying to receive data from the listen socket later while you should receive on the accept socket.

Share this post


Link to post
Share on other sites
[quote name='Erik Rufelt' timestamp='1318688167' post='4872847']
That doesn't make any sense whatsoever, and if that "fixes" it the code might have more problems. Sounds like perhaps you're trying to receive data from the listen socket later while you should receive on the accept socket.
[/quote]

here is the full code:-

server.h
[code]#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;
SOCKET myBackup;

sockaddr_in service;
int iResult;
};

[/code]

server.cpp
[code]#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");
ListenSocket = AcceptSocket; //THIS IS THE CODE THAT FIXED THE PROBLEM !!
}

void Server::Recv(char *RecvBuffer, int size)
{
int i = recv( ListenSocket, RecvBuffer, size, 0 );
RecvBuffer[i] = '\0';
}

[/code]

main.cpp
[code]
#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;
}
[/code]

Share this post


Link to post
Share on other sites
Replace ListenSocket with AcceptSocket in recv() instead, and remove that line again as it won't be needed anymore. The listen socket only accepts connections, and then you receive data on the accepted socket.

Share this post


Link to post
Share on other sites
[quote name='Erik Rufelt' timestamp='1318691500' post='4872866']
Replace ListenSocket with AcceptSocket in recv() instead, and remove that line again as it won't be needed anymore. The listen socket only accepts connections, and then you receive data on the accepted socket.
[/quote]

thanks that actually work :D. the only problem left is when the client type a message for example "How are you doing today sir?" it only prints "How" in the server side, which is weird.

Share this post


Link to post
Share on other sites
Probably not that weird. How does your client work?
There are many things to think about when sending things over the network.
If you send "1234", the server could get first "1", then "234" separately.
If you send "123" and then "456", the server could get "12345" at once, and then "6".
You need to treat incoming data as a stream and display it as it comes. As of now, you only do one recv() on the server and then display that and exit. Do recv() in a loop until the client disconnects instead.

Another possible problem is that the client input handling cuts the string at a space, so you only send "How", and if you ran the same code again the next time would send "are".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this