Quake 2 server info
Hi I wan't to get the details of quake 2 server, such as players , frags etc. But I don't know how , I am going to do that. I can work with winsock , and have experience with irc servers. So I am trying to connect to game server, that's ok. But sending message failes. Can anyone one help me out ? Thanks.
Ok great thank's I am going to try it , and see the results :)
Here is how I am doing this:
I can hardly guess what is the problem :(
[Edited by - DMINATOR on July 22, 2005 4:21:20 AM]
Here is how I am doing this:
//creating socket - all ok sockfd = 1900sockfd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);//then i am making connection - result = 0connect(sockfd, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr))//but the problem is that I can't send the text to the server somehow , the message is stored in the buffermemset(buffer,0,BUFSIZE); //let's add finishing messagesprintf(buffer,"%c%c%c%c%s",c,c,c,c,temp); //let's use this text ("ÿÿÿÿgetstatus") and send it to the server//here is the errorif(send(smsg, (char*)(buffer),strlen(buffer), 0) == SOCKET_ERROR)MessageBox(NULL,buffer,"Can't send",0);
I can hardly guess what is the problem :(
[Edited by - DMINATOR on July 22, 2005 4:21:20 AM]
<old post removed>
Ok I found out how to use udp sockets
well here is the code I am using right now
The Problem is in this line, once I call it the program freeses:
numbytes=recvfrom(sockfd,buf, 4047, 0,(struct sockaddr *)&dest_addr, &addr_len))
I understand that my program is waiting for the packet to come , but I don't think it should wait for this forever, maybe I am doing something wrong.
Can anyone help me ? What can be the problem ?
[Edited by - DMINATOR on July 25, 2005 2:40:44 PM]
Ok I found out how to use udp sockets
well here is the code I am using right now
//Socket initialization routineint InitializeSock(char* dadress,int dport){ //value used for getting hostname by ip adress unsigned int addr; //now we create destip and desthostname destip = (char*)malloc(sizeof(char) * 255); desthostname = (char*)malloc(sizeof(char) * 255); // THIS SHOULD BE DONE BEFORE ANYTHIN ELSE if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) { return(-1); } //now this is a hard part we need to find out from //the input did we receve hostname or ip adress //for this we use special function //is ip ? if(IsIP(dadress)) { //working with ip addr = inet_addr(dadress); if((servinfo = gethostbyaddr((char *)&addr, 4, AF_INET) ) == NULL) { return -1; } //putting values sprintf(desthostname,"%s",servinfo->h_name); sprintf(destip,"%s",dadress); } else { //working with hostname if((servinfo = gethostbyname(dadress)) == NULL) { return -1; } //putting values sprintf(destip,"%s",inet_ntoa(*((struct in_addr *)servinfo->h_addr))); sprintf(desthostname,"%s",dadress); } char buffer[30]; char buf[4048]; char c = 0; int numbytes; int addr_len; if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { AW_ShowError(); exit(1); } dest_addr.sin_family = AF_INET; // host byte order dest_addr.sin_port = htons((short)destport); // short, network byte order dest_addr.sin_addr.s_addr = inet_addr(destip); memset(&(dest_addr.sin_zero), '\0', 8); // zero the rest of the struct sprintf(buffer,"%c%c%c%cgetinfo",c,c,c,c); if ((numbytes=sendto(sockfd,buffer, 12, 0, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr))) == -1) { AW_ShowError(); return -1; } sprintf(buffer,"sent %d bytes to %s\n", numbytes, inet_ntoa(dest_addr.sin_addr)); addr_len = sizeof(struct sockaddr); //here is the porblem it causes my program to freese ? if ((numbytes=recvfrom(sockfd,buf, 4047, 0, (struct sockaddr *)&dest_addr, &addr_len)) == -1) { sprintf(buffer,"receaved %d bytes from %s\n", numbytes, inet_ntoa(dest_addr.sin_addr)); MessageBox(NULL,buffer,"SENDED",0); AW_ShowError(); exit(1); } return 0;}
The Problem is in this line, once I call it the program freeses:
numbytes=recvfrom(sockfd,buf, 4047, 0,(struct sockaddr *)&dest_addr, &addr_len))
I understand that my program is waiting for the packet to come , but I don't think it should wait for this forever, maybe I am doing something wrong.
Can anyone help me ? What can be the problem ?
[Edited by - DMINATOR on July 25, 2005 2:40:44 PM]
Ok I finally did it :). I think the problem was by calling "status" instead of "getstatus" , and using first bytes ff not 0 :). So everything is good :).
Here Is the code if anyone is interested:
The only thing that I need to add is timeout check :)
Here Is the code if anyone is interested:
#include <winsock.h>#include <stdio.h>#include <stdlib.h>//SOCKET REQUIRED INFORMATIONWSADATA wsaData; //wsock32.libchar hostname[80] = "playground.ru";char message[2048];#define MYPORT 27910 // standart quake 2 portvoid AW_ShowError(){ printf("Error - %d\n",WSAGetLastError());}int main(int argc, char *argv[]) { int sockfd; struct sockaddr_in their_addr; // connector's address information struct hostent *he; int numbytes; char c = 0; // THIS SHOULD BE DONE BEFORE ANYTHIN ELSE if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) { AW_ShowError(); return(-1); } //here is the message that caused so much trouble sprintf(message,"\xff\xff\xff\xffstatus"); if ((he=gethostbyname(hostname)) == NULL) { // get the host info AW_ShowError(); exit(1); } if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { AW_ShowError(); exit(1); } their_addr.sin_family = AF_INET; // host byte order their_addr.sin_port = htons(MYPORT); // short, network byte order their_addr.sin_addr = *((struct in_addr *)he->h_addr); memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct if ((numbytes=sendto(sockfd, message, 11, 0, (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) { perror("sendto"); exit(1); } printf("sent %d [%s] bytes to %s:%d\n", numbytes,message, inet_ntoa(their_addr.sin_addr),MYPORT); numbytes = recv(sockfd, message, sizeof(message), 0); printf("[%d],%s\n",numbytes,message); AW_ShowError(); closesocket(sockfd); return 0; }
The only thing that I need to add is timeout check :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement