Changeset aee34b9 in network-game for client


Ignore:
Timestamp:
Nov 20, 2012, 1:19:44 AM (12 years ago)
Author:
dportnoy <dmp1488@…>
Branches:
master
Children:
a845faf
Parents:
7d7df47
Message:

Cleaned up the client a bit and made it more c++-like and added to the readme

File:
1 edited

Legend:

Unmodified
Added
Removed
  • client/Client/main.cpp

    r7d7df47 raee34b9  
    33
    44#include <winsock2.h>
     5#include <WS2tcpip.h>
    56
    67#include <stdio.h>
     
    3233int main(int argc, char *argv[])
    3334{
    34    int sock, n;
    35    int length;
    36    struct sockaddr_in server, from;
    37    struct hostent *hp;
    38    char buffer[256];
    39    
     35        int sock, n;
     36        struct sockaddr_in server, from;
     37        struct hostent *hp;
     38        char buffer[256];
     39
    4040        if (argc != 3) {
    41                 printf("Usage: server port\n");
     41                cout << "Usage: server port" << endl;
    4242                exit(1);
    4343        }
     
    5151       
    5252        if (wsaerr != 0) {
    53                 printf("The Winsock dll not found.\n");
     53                cout << "The Winsock dll not found." << endl;
    5454                exit(1);
    5555        }else
    56                 printf("The Winsock dll was found.\n");
     56                cout << "The Winsock dll was found." << endl;
    5757       
    5858        sock= socket(AF_INET, SOCK_DGRAM, 0);
    59         if (sock < 0) error("socket");
     59        if (sock < 0)
     60                error("socket");
    6061
    6162        server.sin_family = AF_INET;
    6263        hp = gethostbyname(argv[1]);
    63         if (hp==0) error("Unknown host");
     64        if (hp==0)
     65                error("Unknown host");
    6466
    65         memcpy((char *)&server.sin_addr,
    66                         (char *)hp->h_addr,
    67                         hp->h_length);
    68    server.sin_port = htons(atoi(argv[2]));
    69    length=sizeof(struct sockaddr_in);
    70    printf("Please enter the message: ");
    71    memset(buffer, 0, 256);
    72    fgets(buffer,255,stdin);
    73    n=sendto(sock,buffer,
    74             strlen(buffer),0,(const struct sockaddr *)&server,length);
    75         if (n < 0) error("Sendto");
    76         n = recvfrom(sock,buffer,256,0,(struct sockaddr *)&from, &length);
    77         if (n < 0) error("recvfrom");
     67        memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
     68        server.sin_port = htons(atoi(argv[2]));
     69        cout << "Please enter the message: ";
     70        memset(buffer, 0, 256);
     71        fgets(buffer,255,stdin);
    7872
     73        socklen_t fromlen = sizeof(server);
     74
     75        n=sendto(sock,buffer, strlen(buffer), 0, (const struct sockaddr *)&server, fromlen);
     76        if (n < 0)
     77                error("Sendto");
     78
     79        n = recvfrom(sock, buffer, 256, 0, (struct sockaddr *)&from, &fromlen);
     80        if (n < 0)
     81                error("recvfrom");
     82       
    7983        buffer[n] = '\0';
    8084        cout << "Got an ack: " << endl;
Note: See TracChangeset for help on using the changeset viewer.