Sie sind nicht angemeldet.

1

08.11.2008, 22:25

Netzwerk Kommunikation

Im Unterricht programmieren wir ein Poker-Spiel.
Ich sollte nun das clientseitige Protokoll schreiben. Der folgende Code soll eine Verbindung zum Server (Host: EiMi, Port: 8000) herstellen.
Da keine Verbindung zustande kommt, ist die Frage, ob das Ganze richtig implementiert ist? Kann der fehlerhafte Verbindungsaufbau auch daher kommen, dass mein Programm unter Linux läuft und das serverseitige unter Windows?

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <syslog.h>
#include <sys/time.h>


#include <arpa/inet.h>


#include <errno.h>
#include <string.h>
#include <iostream>
using namespace std;



typedef struct {
  struct sockaddr_in sin;
  unsigned int sinlen;
  int bindflag;
  int sd;
} SOCKET;


int sclient(SOCKET *sp, char *name, int port)
{
  struct hostent *hostent;
  struct in_addr addr;

  addr.s_addr = inet_addr("192.168.1.1");
  
if((hostent=gethostbyaddr((char*)&addr, sizeof(addr), AF_INET))==0)
{
	return -2;	
}
  //if  ((hostent=gethostbyaddr((char*)&addr, sizeof(addr), AF_INET))==0)
  //  return -1;



  sp->sin.sin_family=(short)hostent->h_addrtype;
  sp->sin.sin_port=htons((unsigned short)port);
  sp->sin.sin_addr.s_addr=*(unsigned long *)hostent->h_addr;
  //connect()
  if  ((connect(sp->sd,(struct sockaddr *)&sp->sin, sp->sinlen))==-1){
    syslog(LOG_INFO,"client-socket: cannot connect to: %s at %d",name,port);

    return -1;
  }

  return sp->sd;
}



int main(void)
{
  printf("hello");
  SOCKET socket1;
  char host[] = "EiMi";
  
  int a=sclient(&socket1,host,8000);
  cout<<a;
  

  
  return 0;
}

Thema bewerten