[英]cant connect to server in local network
I have 2 Computers.我有2台电脑。 My Server is running on the Debian one while my Client is running on the Windows PC.我的服务器在 Debian 上运行,而我的客户端在 Windows PC 上运行。 The IP of my Server is 192.168.2.113, the one of my Client is 192.168.2.122.我的服务器的 IP 是 192.168.2.113,我的客户端是 192.168.2.122。
When I telnet 127.0.0.1 or 192.168.2.113 on the Server Machine there is no problem.当我在服务器机器上 telnet 127.0.0.1 或 192.168.2.113 时没有问题。 The Problem is that I cant connect to the Server from my Client.问题是我无法从客户端连接到服务器。
I opened the port 50002 via iptables with the comment:我通过 iptables 打开了端口 50002,并带有以下注释:
sudo iptables -A INPUT -p tcp --dport 50002 --jump ACCEPT
The main.cpp of my Server:我的服务器的 main.cpp:
#include<stdio.h>
#include<string.h> //strlen
#include<sys/socket.h>
#include<arpa/inet.h> //inet_addr
#include<unistd.h> //write
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , read_size;
struct sockaddr_in server , client;
char client_message[2000];
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
if (socket_desc == -1)
{
printf("Could not create socket");
}
puts("Socket created");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons( 50002 );
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
//Receive a message from client
while( (read_size = recv(client_sock , client_message , 2000 , 0)) > 0 )
{
//Send the message back to client
write(client_sock , client_message , strlen(client_message));
}
if(read_size == 0)
{
puts("Client disconnected");
fflush(stdout);
}
else if(read_size == -1)
{
perror("recv failed");
}
return 0;
}
Something strange i noticed is that I can connect to www.google.com in firefox but not in telnet (on my client pc):我注意到的一些奇怪的事情是,我可以在 firefox 中连接到 www.google.com 但不能在 telnet 中(在我的客户端 pc 上):
telnet>o www.google.com 80远程登录>o www.google.com 80
I only get 400-badrequest error我只收到 400-badrequest 错误
help is very appriciated.帮助是非常appriciated。
Suggestions:建议:
Step 1) establish that you can ping your server from windows.步骤 1) 确定您可以从 windows ping 您的服务器。 This way we eliminate potential network configuration issues.通过这种方式,我们消除了潜在的网络配置问题。 Make changes to your firewall if needed.如果需要,对防火墙进行更改。
ping 192.168.2.113平 192.168.2.113
Step 2) Ping from server to access windows.步骤 2) 从服务器 ping 以访问 windows。 Windows firewall can prevent communication as well. Windows 防火墙也可以阻止通信。
ping 192.168.2.112平 192.168.2.112
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.