You are not logged in.
I'm making a graphical frontend to an old MUD, the hope is to attract more players.
My main problem is that I can't find any clear telnet code and seeings as i know NOTHING about socket programming on
Micro$ft Windoze...
I'm stuck before i've even GOT to the graphics.
Last edited by Iknow (2011-02-05 02:32:38)
Offline
Telnet really shouldn't be the problem. MUD clients usually don't even implement the full telnet protocol but only very basic features such as for example sending and receiving plain text.
I find socket programming with the pure WinAPI to be quite ugly, so you might want to consider using portable libraries such as boost, specifically boost.asio, which offers a nice and modern way for network and IO programming in C++.
Also, you could simply use a framework like Qt which is really great for GUI programming in C++ and additionally offers everything you need for cross-platform network programming.
Offline
I can give you a short example. Wrote it a few years ago but it seems still to work (even on win 7^^).
#include <stdio.h>
#include <string.h>
#include <winsock2.h>
int startWinsock(void);
long getAddrFromString(char* hostnameOrIp, SOCKADDR_IN* addr)
{
long rc;
unsigned long ip;
HOSTENT* he;
/* Parameter prüfen */
if(hostnameOrIp==NULL || addr==NULL)
return SOCKET_ERROR;
/* eine IP in hostnameOrIp ? */
ip=inet_addr(hostnameOrIp);
/* bei einem fehler liefert inet_addr den Rückgabewert INADDR_NONE */
if(ip!=INADDR_NONE)
{
addr->sin_addr.s_addr=ip;
return 0;
}
else
{
/* Hostname in hostnameOrIp auflösen */
he=gethostbyname(hostnameOrIp);
if(he==NULL)
{
return SOCKET_ERROR;
}
else
{
/*die 4 Bytes der IP von he nach addr kopieren */
memcpy(&(addr->sin_addr),he->h_addr_list[0],4);
}
return 0;
}
}
int main()
{
SOCKADDR_IN addr;
char * stringHost= (char *) malloc(1024);
long rc;
printf("Bitte Host angeben\n");
fflush(stdin);
stringHost=gets(stringHost);
rc=startWinsock();
if(rc!=0)
{
printf("Fehler: startWinsock, fehler code: %d\n",rc);
return 1;
}
else
{
printf("Winsock gestartet!\n");
}
int sockfd = socket (AF_INET, SOCK_STREAM, 0);
if (sockfd == INVALID_SOCKET) {
printf("Fehler: Der Socket konnte nicht erstellt werden, fehler code: %d\n",WSAGetLastError());
}
memset(&addr,0,sizeof(SOCKADDR_IN)); // zuerst alles auf 0 setzten
addr.sin_family=AF_INET;
addr.sin_port=htons(80); // wir verwenden mal port 80
//addr.sin_addr.s_addr=inet_addr("88.198.38.248"); // zielrechner
rc=getAddrFromString(stringHost,&addr);
if(rc==SOCKET_ERROR)
{
printf("IP für %s konnte nicht aufgeloest werden\n", stringHost);
return 1;
}
else
{
printf("IP aufgeloest!\n");
}
rc=connect(sockfd,(SOCKADDR*)&addr,sizeof(SOCKADDR));
if(rc==SOCKET_ERROR)
{
printf("Fehler: connect gescheitert, fehler code: %d\n",WSAGetLastError());
}
else
{
printf("Verbunden mit %s\n\n", stringHost);
}
char csend[] = "GET / HTTP/1.0\nUser-Agent: www.taldril.ch webscanner\n\n";
/*
printf("%s", csend);
printf("\n");
*/
char * buf = (char *) malloc(1024);
for (int i = 0; i < 1024; i++)
{
buf[i] = 0;
}
char * buf2 = (char *) malloc(1024);
for (int i = 0; i < 1024; i++)
{
buf2[i] = 0;
}
rc=send(sockfd, csend, sizeof(csend), NULL);
if(rc==SOCKET_ERROR)
{
printf("Fehler: senden gescheitert, fehler code: %d\n",WSAGetLastError());
}
else
{
printf("Gesendet\n");
}
recv(sockfd, buf, 1024, NULL);
recv(sockfd, buf2, 1024, NULL);
printf("%s", buf);
printf("%s", buf2);
system("PAUSE");
WSACleanup();
}
int startWinsock(void)
{
WSADATA wsa;
return WSAStartup(MAKEWORD(2,0),&wsa);
}
EDIT: i noticed you wanted a c++ example... i gave you C. Sorry. And there are some german comments... but i am too lazy to go through my code and translate them.
But to make your life easier take a look at the boost c++ libraries
Last edited by Vermillion (2010-11-26 08:49:32)
Offline
For cross platform C++, I would go with Qt.
Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
Sometimes it is the people no one can imagine anything of who do the things no one can imagine. -- Alan Turing
---
How to Ask Questions the Smart Way
Offline
Thanks for the replies guys, you've been a real help.
I'll check out boost and implement it as soon as i can, and Vermillion thanks for the code anyway i can translate the comments with google and my compiler has no problem with C so i'll probably be able to get the gist. I'll look up Qt but doubt i'll be using it because it is a group effort as of 2 hours after the post and i"m not responsible for graphics anymore (yay).
If i have anymore problems then i'll post them here.
ps thanks again that was a faster response than any other forum i've asked on!
Offline