Hello fine community at stackoverflow! I've been lurking around using the site for about a year now, and just have come to the need to post a question.
I'm a bit of a klutz when it comes to coding, so go easy on me.
Here's the code (most of it is the sample winsock MSDN code :P):
Client:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
void clarify(char *recvdata);
char mdata[7];
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "10150"
int main(int argc, char **argv)
{
WSADATA wsaData;
SOCKET ConnectSocket = INVALID_SOCKET;
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
char recvbuf[DEFAULT_BUFLEN];
int iResult;
int recvbuflen = DEFAULT_BUFLEN;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed with error: %d\n", iResult);
return 1;
}
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// Resolve the server address and port
iResult = getaddrinfo("173.21.56.58", DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) {
printf("getaddrinfo failed with error: %d\n", iResult);
WSACleanup();
return 1;
}
// Attempt to connect to an address until one succeeds
for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
// Create a SOCKET for connecting to server
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype,
ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// Connect to server.
iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);
if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}
// Send an initial buffer
char sendbuf[512];
std::cin.getline(sendbuf, 512);
iResult = send( ConnectSocket, sendbuf, 512, 0 );
if (iResult == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
std::cout<<"sendbuf: "<<sendbuf<<"\n";
std::cout<<"strlen(sendbuf): "<<strlen(sendbuf)<<"\n";
printf("Bytes Sent: %ld\n", iResult);
// Receive until the peer closes the connection
do {
iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
printf("Bytes received: %d\n", iResult);
else if ( iResult == 0 )
printf("Connection closed\n");
else
printf("recv failed with error: %d\n", WSAGetLastError());
std::cout<<"recvbuf: "<<recvbuf<<"\n";
std::cout<<"recvbuflen: "<<recvbuflen<<"\n";
std::cout<<"strlen(recvbuf): "<<strlen(recvbuf)<<"\n";
std::cout<<recvbuf[0]<<"\n";
std::cout<<recvbuf[1]<<"\n";
std::cout<<recvbuf[2]<<"\n";
std::cout<<recvbuf[3]<<"\n";
std::cout<<recvbuf[4]<<"\n";
std::cout<<recvbuf[5]<<"\n";
std::cout<<recvbuf[6]<<"\n";
std::cout<<recvbuf[7]<<"\n";
std::cout<<recvbuf[8]<<"\n";
std::cout<<recvbuf[9]<<"\n";
std::cout<<recvbuf[10]<<"\n";
std::cout<<recvbuf[11]<<"\n";
std::cout<<recvbuf[12]<<"\n";
clarify(recvbuf);
std::cout<<"mdata(main()): "<<mdata<<"\n";
if (mdata == "anarchy") {std::cout<<"This is Anarchy. :)";}
else {std::cout<<"Nope. :( ";}
} while( iResult > 0 );
std::cin.ignore();
// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
// cleanup
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
void clarify(char *recvdata)
{
std::cout<<"recvdata: "<<recvdata<<"\n";
for (int i=0; i<(strlen(recvdata)); i++) {
mdata[i]=recvdata[i];
std::cout<<mdata[i]<<"\n";
}
std::cout<<"mdata(clarify()): "<<mdata<<"\n";
}
And the server code is the sample MSDN winsock code.
I realize the code has a bunch of sloppy extras added in, but rest assured, those are for my own thoughts and reminders. So, please don't bother telling me other places I could clean up. I'll take care of that when I get closer to finishing my project (a long way away :) ).
So, I'm having issues comparing the "mdata" variable with the characters "anarchy".
Even when I send "anarchy" through winsock, it comes back as "anarchy", and I run it through "clarify()" just for good measure, it still doesn't seem to equal "anarchy".
I'm sure it's a noob mistake I'm making here, so please go easy on me...
EDIT:
Here's the output after typing "anarchy" for the "sendbuf" input:
anarchy
sendbuf: anarchy
strlen(sendbuf): 7
bytes sent: 512
bytes recieved: 512
recvbuf: anarchy
recvbuflen: 512
strlen(recvbuf): 7
a
n
a
r
c
h
y
f
4
i
w
,
recvdata: anarchy
a
n
a
r
c
h
y
mdata(clarify()): anarchy
mdata(main()): anarchy
Nope :(