Troll Sama bin Laden
Troll Sama bin Laden

Reputation: 71

cant connect to server in local network

I have 2 Computers. My Server is running on the Debian one while my Client is running on the Windows PC. The IP of my Server is 192.168.2.113, the one of my Client is 192.168.2.122.

When I telnet 127.0.0.1 or 192.168.2.113 on the Server Machine there is no problem. The Problem is that I cant connect to the Server from my Client.

I opened the port 50002 via iptables with the comment:

sudo iptables -A INPUT -p tcp --dport 50002 --jump ACCEPT

The main.cpp of my Server:

#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):

telnet>o www.google.com 80

I only get 400-badrequest error

help is very appriciated.

Upvotes: 0

Views: 101

Answers (1)

Bogdan_G
Bogdan_G

Reputation: 21

Suggestions:

Step 1) establish that you can ping your server from windows. This way we eliminate potential network configuration issues. Make changes to your firewall if needed.

ping 192.168.2.113

Step 2) Ping from server to access windows. Windows firewall can prevent communication as well.

ping 192.168.2.112

Step 3) I compiled your server program and run om Debian server. Your program works as expected. First I connected with telnet on same server to test the program. All good there are two terminals,in this picture

Step 4) I used telnet from Windows 10 to connect to your server. It connected as well, sending encoded data back. I used telnet from Windows 10 to connect to your server.

I used telnet from Windows 10 to connect to your server.

Step 5) I used PuTTY telnet from Windows that worked good as expected. I used PuTTY telnet from Windows

Step 6) Conclusion that you have network configuration problem on your system that is not your program related. It can be firewall as well. start with step 1 and 2. Then if needed troubleshoot your network. Regards Bogdan.

Upvotes: 1

Related Questions