-5

Possible Duplicate:
Connection from linux to windows via tcp

I have a TCP client on windows/c# and another one on linux/c++ Also I have a TCP server on both.

My TCP client on windows/c# works ok, the problem is the TCP client on linux/c++

when I connect to a TCP server on linux/c++ it works perfectly, but when i try to connect to a TCP server on windows/c# it doesn't connect, but the ping is ok.

this is the part of the code where it fails:

int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;  
struct in_addr addr={0};
char buffer[256];
if (argc < 3) {
   fprintf(stderr,"usage %s hostname port\n", argv[0]);
   exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) 
    error("ERROR opening socket");
addr.s_addr=inet_addr(argv[1]);
server = gethostbyaddr((char *) &addr, 6, AF_INET);
if (server == NULL) {
    fprintf(stderr,"ERROR, no such host\n");
    exit(0);

the problem is the line:

server = gethostbyaddr((char *) &addr, 6, AF_INET);

it returns null, so it prints "ERROR,no such host"

I have tried every combination:

-TCP client(linux) to TCP Server(linux)

-TCP client(linux) to TCP Server(windows)

-TCP client(windows) to TCP Server(linux)

-TCP client(windows) to TCP Server(windows)

and everything works except: TCP client(linux) to TCP server(windows)

I also tried the TCP client netcat(included on linux) to TCP server(windows) and it works.

I have called ping both from windows to linux and also from linux to windows, and it recieves 100%

I don't know why my TCP client(linux) doesn't connect to TCP server(linux)

Community
  • 1
  • 1
  • 1
    If you want to make a TCP connection and you have the IP address, why do need to perform a reverse DNS lookup? – dtb May 09 '12 at 23:56
  • what???, i didn't understand...what is reverse DNS lookup??? – Francisco Gutiérrez May 10 '12 at 00:10
  • 1
    You call `gethostbyaddr`. Why? – dtb May 10 '12 at 00:11
  • i don't really know much about TCP, so that i got that code from the web. what i understand is that "gethostbyaddr" returns the ip in correct order of bytes and I need it for the assignment of server: server = gethostbyaddr((char *) &addr, 6, AF_INET); and what i don't know is why it works on a connection linux/linux, but not linux/windows – Francisco Gutiérrez May 10 '12 at 00:23
  • Please spend some effort on trying to understand what you're doing. Don't copy random code off the Internet and expect us to make it work for you. – dtb May 10 '12 at 00:33
  • @dtb don't ya just love it when folks come here with code form the net/shady books? The fact that there's a 'Homework' flag on SO is bad enough – johnathan May 10 '12 at 02:59
  • -1 for horrible, nasty code probably written by some crummudgeony C guy 40 years ago. – John Dibling May 10 '12 at 14:13

1 Answers1

0

You have to have some sort of name service, such as DNS, in order for gethostbyaddr() to determine the hostname for a given address. There is apparently a problem with this when you are going Windows to linux. In general, I have had a lot of reliability problems myself with resolving hostnames this way and suggest that you not use it. As DTB pointed out, you only need the IP address to connect. If you need the hostname for some other reason, then the safest thing is to have your server send your client its hostname when your client first connects (or vice versa)

pnswdv
  • 181
  • 2
  • 8