klient winsock

0

mam taki problem z klientem... na zajęciach wysyłał dane na inny port niż mu podałem a z moim serwerem nie chce się połączyć...
w poradniku było tylko napisane "zatelnetuj się na niego telnet hostname 9034"
1.Nie mam pojęcia co to znaczy...
2.z kąd mam wziąć to hostname(jak)...
Reszta chyba działa ale mam problemy z łączeniem się(tutorial nie pomógł).
Kolega łączył się z serwerem i podawał ip i port... jak to może się odnosić do klienta? czy to hostname ma być ip czy czymś innym?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <iostream>
#include <winsock2.h>
#include <windows.h>
#include <iostream>
#include <cstdlib>
#define MYPORT 9034
using namespace std;
int main(void)
{
    int sockfd;
    struct sockaddr_in their_addr; // informacja o adresie osoby łączącej się
    struct hostent * he;
    int numbytes;
    string hostname="localhost",port="9034";
    if(( he = gethostbyname(hostname.c_str()) ) == NULL ) { // pobierz informacje o hoście
        perror( "gethostbyname" );
        exit( 1 );
    }

    if(( sockfd = socket( AF_INET, SOCK_DGRAM, 0 ) ) == - 1 ) {
        perror( "socket" );
        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 ); // wyzeruj resztę struktury

    if(( numbytes = sendto( sockfd,port.c_str(), strlen(port.c_str() ), 0,
    ( struct sockaddr * ) & their_addr, sizeof( struct sockaddr ) ) ) == - 1 ) {
        perror( "sendto" );
        exit( 1 );
    }

    printf( "sent %d bytes to %s\n", numbytes,
    inet_ntoa( their_addr.sin_addr ) );

    close( sockfd );
    system("pause");
    return 0;
}
 
0
  1. start -> uruchom -> telnet
  2. localhost albo 127.0.0.1
0

Czemu cały czas mi wywala:

gethostbyname No Error 

? nie widzi serwera ale czemu?
Podałem nazwę i port... co jeszcze musze zrobić?

0
  1. gethostbyname radze unikac.
  2. Wypisuj sobie zmienna errno/WSAGetLastError na stderr i sprawdzaj co poszlo nie tak...
1
#include "stdafx.h"

#include <winsock2.h>
#include <cstdio>
#include <cstdlib>

void exit_with_error(const char* msg)
{
    printf("error: %d\tmsg: %s\n", WSAGetLastError(), msg);
    exit(-1);
}

int _tmain(int argc, _TCHAR* argv[])
{
    WSAData wsadata;
    WSAStartup(MAKEWORD(2, 2), &wsadata);

    SOCKET sockfd;
    struct sockaddr_in addr;
    struct hostent* host;

    if ((host = gethostbyname("192.168.0.10")) == NULL) {
        exit_with_error("gethostbyname");
    }

    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(12345);
    addr.sin_addr.s_addr = *(u_long*)host->h_addr_list[0];

    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
        exit_with_error("socket");
    }

    int bytes;
    const char* msg = "jakas tam wiadomosc";
    if ((bytes = sendto(sockfd, msg, strlen(msg), 0, (struct sockaddr*)&addr, sizeof(addr))) < 0) {
        exit_with_error("sendto");
    }

    printf("sent %d bytes to %s\n", bytes, inet_ntoa(addr.sin_addr));

    closesocket(sockfd);
    WSACleanup();

    return 0;
}

Dziala, sprawdzane (VS 2013).

1 użytkowników online, w tym zalogowanych: 0, gości: 1