Hi, I've figured out a lot from before. As it turns out, the telnet client is just getting in the way, and blocking the port I needed to listen to. So after not logging into telnet, I made some improvements to my code, and here should be a compilable version:
Code:
#include <winsock2.h>
#include <stdio.h>
#define DEFAULT_BUFLEN 100
#define DEFAULT_PORT "0xCAFE"
int main(){
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
int iResult;
SOCKET ConnectSocket;
WSADATA wsaData;
struct sockaddr_in clientService;
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
memset(recvbuf, 0, DEFAULT_BUFLEN);
system("cls");
//create SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(ConnectSocket == INVALID_SOCKET){
printf("ERROR Invalid Socket: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
printf("CLIENT: Socket created...\n");
//bind port: address family, IP address, and port
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
clientService.sin_port = htons(0xCAFE);
printf("CLIENT: Port bound...\n");
//connect to server
iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
if(iResult == SOCKET_ERROR){
closesocket (ConnectSocket);
printf("ERROR Cannot Connect: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
printf("CLIENT: Connected to Firefly 2D...\n");
//receive messages
while(TRUE){
iResult = recv(ConnectSocket,recvbuf,recvbuflen,0);
if(iResult > 0){
printf("Bytes received: %d\n", iResult);
printf("Recieved: %s\n", &recvbuf);
}
else if(iResult == 0){
printf("Connection closed.\n");
}
else{
printf("ERROR Message Lost: %d\n", WSAGetLastError());
}
//printf("Reached end!");
}
//shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket,SD_SEND);
if(iResult == SOCKET_ERROR){
printf("ERROR Closing Connection: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
//cleanup
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
You need to add Ws2_32.lib as an Additional Dependancy under the Linker options in VS2005 to compile it. This code mostly works, it recieves the data I want, the only issue is, it recieves lots of it! In an infinite loop, as shown in this screenshot:
http://b.imagehost.org/view/0221/output.jpg
Does anyone know why? The sending code definitely only sends the message shown in the screenshot once.
Edit: the code will simply quit the cmd window if that port does not exist