How to connect client / server in C (Beej Guide to Network Programming)

I work through a simple tutorial for C network programming, which can be found here: http://beej.us/guide/bgnet/output/html/multipage/index.html

After reading this, I planned to implement a test client-server program, and then start playing, changing things, adding material, etc. I implemented the program found here (the first client / server set dealing with TCP connections): http://beej.us/guide/bgnet/output/html/multipage/clientserver.html

Basically, the "server" runs on the same machine, and when the client connects, the server simply sends "Hello, world!" This works fine when I run both on the same computer and connect to localhost.

However, I cannot connect between different machines (both on the Debian server and on OpenBSD, without the iptables / pf ruleset). The connection is just time, and I'm not quite sure why. I can ping and ssh in both.

Can anyone, especially someone familiar with this tutorial, point me in the right direction?

Edit: there is no X on the servers, so there are no screenshots, but netstat -tlnp | grep 3490 gives me nothing.

netstat -an shows listening to tcp connection on 3490.

+6
source share
2 answers

I don’t see your servofin being filled with any where in the code

// The server must allow connections from any IP address

serv_info.sin_addr.s_addr = INADDR_ANY; 

You may also need to complete the family and port.

// Fill in the server address name

  serv_addr.sin_family = AF_INET; 

To get more detailed information, here is a working server that I wrote on the forum http://forum.codecall.net/topic/63924-a-simple-tcp-server-using-linux-c-api/

+1
source

Right before if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { print the value p->ai_addr I would put 127.0.0.1 on it. This will lead to the fact that communication will work fine on one computer, but will not happen anywhere.

To listen on all interfaces, contact 0.0.0.0.

0
source

Source: https://habr.com/ru/post/915469/


All Articles