Connection from linux to windows via tcp

只愿长相守 提交于 2019-12-31 07:14:06

问题


I have a tcp server application on windows (c#) wich accepts any connection by the port 3000 I have a tcp client application on linux(ubuntu)(c++) wich send a simple text by the port 3000

I also have a client on windows and a server on linux, i works perfectly sending the text: from linux to linux from windows to windows from windows to linux

the problem is that when i try to send from linux client to windows server my c++ application on linux tells me that the host doesn't exist

i already check the ip adress and it is correct i also tried to do it with hostname

but it doesn't work

does anybody know why it happens???

this is my code on the client(linux-c++):

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=gethostbyname(argv[1]);
server = gethostbyaddr((char *) &addr, 4, AF_INET);
if (server == NULL) {
    fprintf(stderr,"ERROR, no such host\n");
    exit(0);
}

i call ping and everything is ok

I run my server on windows and open port 3000 to any conection

I try to run my client(code above) with the windowsIP/windowsHostName and the port 3000(Already tried another port)

and the problem is in the line:

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

server gets null so it prints "ERROR, no such host"

but the ip is correct.

When i use the same code for conecting with a server on linux(c++) it works


回答1:


The most probable reason is that you have your Windows Firewall blocking incoming connections to port 3000.

Go to Control Panel and Disable your firewall and test it again. If this is the problem you'll have to add a rule to allow incoming connections to your 3000 port. You should also include in the rule the allowed host IP (your Linux IP) to avoid problems with unexpected remote connections.

You can try making a telnet connection form linux to your server IP Address and 3000 port. It will probably be rejected by the firewall.




回答2:


You need to set the network type in Virtual box as "NAT". The above problem occurs if it is in bridged adapter. A detailed discussion can be found here: http://ubuntuforums.org/archive/index.php/t-1786307.html



来源:https://stackoverflow.com/questions/10506788/connection-from-linux-to-windows-via-tcp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!