Thread: socket, udp,Halflife Server

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    2

    socket, udp,Halflife Server

    First of all, hi all

    Well, i coded a udp socket with a tutorial, which i want to connect to an HL server to get some Infos(POlayers,map,...)

    my program:
    ---------------------------------------------------------------------------------------
    #include <winsock2.h>
    #include <stdio.h>
    #include <stdlib.h>

    //Prototypen
    int startWinsock(void);
    int main()
    {
    long rc;
    int a= -1;
    SOCKET s;
    char buf[256];
    SOCKADDR_IN addr;
    SOCKADDR_IN remoteAddr;
    int remoteAddrLen=sizeof(SOCKADDR_IN);

    rc=startWinsock();
    if(rc!=0)
    {
    printf("Fehler: startWinsock, fehler code: %d\n",rc); return 1;
    sleep(1000);
    }
    else
    {
    printf("Winsock gestartet!\n");
    sleep(1000);
    }

    //UDP Socket erstellen
    s=socket(AF_INET,SOCK_DGRAM,0);
    if(s==INVALID_SOCKET)
    { printf("Fehler: Der Socket konnte nicht erstellt werden, fehler code: %d\n",WSAGetLastError());
    sleep(1000);
    return 1;
    }
    else
    {printf("UDP Socket erstellt!\n");
    sleep(1000);
    }
    // addr vorbereiten
    addr.sin_family=AF_INET;
    addr.sin_port=htons(27016);
    addr.sin_addr.s_addr=inet_addr("194.112.0.163");

    rc=connect(s,(SOCKADDR*)&addr,sizeof(SOCKADDR_IN)) ;

    //rc=sendto (s,buf,strlen(buf),0,(SOCKADDR*)&addr,sizeof(SOCKA DDR_IN));
    rc=send(s,buf,strlen(buf),0);
    if(rc==SOCKET_ERROR)
    {
    printf("Fehler: send, fehler code: %d\n",WSAGetLastError());
    sleep(1000);
    return 1;
    }
    else
    {
    printf("%d Bytes gesendet!\n", rc);
    sleep(1000);
    }
    //rc=recvfrom(s,buf,256,0,(SOCKADDR*)&remoteAddr,&re moteAddrLen);
    rc=recv(s,buf,256,0);
    if(rc==SOCKET_ERROR)
    {
    printf("Fehler: recvfrom, fehler code: %d\n",WSAGetLastError());
    sleep(1000);
    return 1;
    }
    else
    {
    printf("%d Bytes empfangen!\n", rc);
    sleep(1000);
    buf[rc]='\0';
    printf("Empfangene Daten: %s\n",buf);
    sleep(1000);
    }
    WSACleanup();
    closesocket(s);
    return 0;
    }

    int startWinsock(void)
    {WSADATA wsa;
    return WSAStartup(MAKEWORD(2,0),&wsa);
    }
    ---------------------------------------------------------------------------------------here is a short snippet from hl server protocol txt:

    Remote App sends a UDP packet to the server on the server's port (e.g., 127.0.0.1:27015):

    The packet should start with 4 consecutive bytes of 255 (32-bit integer -1) and the string:

    "challenge rcon\n".

    The server will respond to the requesting system on the purported remote IP address and port with four 255's and:

    "challenge rcon number\n" where number is an unsigned int32 number.

    full txt file at :
    http://www.chatbear.com/cgi-bin/boar...2&view=flatold

    ---------------------------------------------------------------------------------------

    well, i dunno really what is ment ot how to sent it since im a beginner, i would appreciate your help alot, but if you are willing to help plz make it detailed, best would be to show whats to be done with the code

    Thx in advance


    Tolpan

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > rc=recv(s,buf,256,0);
    This does not guarantee that you'll receive all your bytes in one message

    If you're expecting "hello world", you might on various runs of your program receive
    "hello world"

    "hello" " world"

    "h" "e" "llo world"

    etc etc
    You need to re-assemble your expected message from several message fragments, if necessary

    > The packet should start with 4 consecutive bytes of 255 (32-bit integer -1) and the string:
    > "challenge rcon\n".
    So start with a buffer containing
    char expected[] = "0xFF" "0xFF" "0xFF" "0xFF" "challenge rcon\n"

    Then you would do
    Code:
    if ( memcmp( buff, expected, 19 ) == 0 ) {
        // got the message!
    }
    This is but one way to test for the arrival of your expected message.

    It's up to you to decide what to do if you don't get this message.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    2
    Thx alot for your help, i will try this tonight/tomorrow and im almost sure im gonna drop by in the near future with other stuff

    Thx again


    Tolpan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket Server
    By carrotcake1029 in forum Windows Programming
    Replies: 2
    Last Post: 07-21-2008, 11:46 AM
  2. Server Architecture
    By coder8137 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-29-2008, 11:21 PM
  3. Non-Blocking Server help.
    By unkownname in forum Networking/Device Communication
    Replies: 7
    Last Post: 07-27-2007, 05:04 PM
  4. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 08:04 PM
  5. socket newbie, losing a few chars from server to client
    By registering in forum Linux Programming
    Replies: 2
    Last Post: 06-07-2003, 11:48 AM