• Advertisement
Sign in to follow this  

Help with my chat/server C++ application

This topic is 2774 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need this problem fixed thanks!xoxo


//Client
WSADATA wsaData;
WORD version;
int error;
version=MAKEWORD(2,0);
SOCKET client;
client=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN sin;
memset(&sin, 0, sizeof sin );
sin.sin_family=AF_INET;
sin.sin_port=htons(5447);
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
connect(client,(sockaddr *)&sin,sizeof(sin));
char buffer[1024]="Test";
recv(client,(char*)&buffer,sizeof(buffer),0);
MessageBox(NULL,buffer,"Sent???",MB_OK);
closesocket(client);
WSACleanup();



//Server
WSADATA wsaData;
WORD version;
int error;

version = MAKEWORD( 2, 0 );

error = WSAStartup( version, &wsaData );

if ( error != 0 )
{
Console::WriteLine("Error");
return FALSE;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||HIBYTE( wsaData.wVersion ) != 0 )
{
Console::WriteLine("Error");
WSACleanup();
return FALSE;
}
SOCKET server;

server = socket( AF_INET, SOCK_STREAM, 0 );

SOCKADDR_IN sin;
Console::WriteLine("Creating Socketadress");
memset( &sin, 0, sizeof sin );

sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(5447);

if ( bind( server, (sockaddr*)&sin, sizeof sin ) == SOCKET_ERROR )
{
Console::WriteLine("Error Binding Socket");
return FALSE;
}
Console::WriteLine("Starting to listen");
while ( listen( server, 20) == SOCKET_ERROR );
SOCKET client;
int length;

length = sizeof sin;
if(client = accept( server,(sockaddr *)&sin, &length ))->DOESNT DO ANYTHING!!!!
{
Console::WriteLine("Accepted!!");
}
char buffer[1024]="stuff";
if(send(client,buffer,strlen(buffer),0))
{
Console::WriteLine("Sent Message!!");
}
closesocket( server );
closesocket(client);
WSACleanup();
Console::ReadKey();

Share this post


Link to post
Share on other sites
Advertisement
It's generally helpful to say what the problem is.

EDIT:
Two problems I see:
1. This line:
recv(client,(char*)&buffer,sizeof(buffer),0);
should be:
recv(client,(char*)buffer,sizeof(buffer),0);
Or you'll stamp all over your stack

2. You're not sending a null terminated string (You're sending 5 bytes, 's', 't', 'u', 'f', 'f'), but you're trying to display the result with MessageBox, which takes a null terminated string. Either send() strlen(buffer)+1 bytes, or manually null terminate the string in the client:

int len = recv(client,(char*)buffer,sizeof(buffer),0);
if(len > 0)
buffer[len] = 0;
MessageBox(NULL,buffer,"Sent???",MB_OK);


[Edited by - Evil Steve on July 16, 2010 5:52:10 AM]

Share this post


Link to post
Share on other sites
And by this i mean that if you look at my server code where the client gets accepted it never shows this message:(

Share this post


Link to post
Share on other sites
This line is incorrect:
if(client = accept( server,(sockaddr *)&sin, &length ))
That is the same as:

client = accept( server,(sockaddr *)&sin, &length );
if(client != 0)

But 0 is a valid value for a socket, INVALID_SOCKET isn't. The code should be:

client = accept( server,(sockaddr *)&sin, &length );
if(client != INVALID_SOCKET)


In any case, because you've not set the socket into non-blocking mode, it'll hang on that line until a client connects. And if you never get past there, it means that a client hasn't connected, which means that there's probably an issue with the client. Does it work if you run the server, then open a command prompt and type "telnet 127.0.0.1 5447" (no quotes)?

Share this post


Link to post
Share on other sites
Okay thanks for the help so far i tried the telnet way and yes the server does send a message saying "stuff" and it has been succesfully sent therefore the problem must be with the client:S

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement