Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am working through a simple tutorial for C network programming found here: https://beej.us/guide/bgnet/html/multi/index.html

After reading through it, my plan was to implement a testing client/server program, and then start to play around, modify things, add stuff, etc. I implemented the program found here (the first client/server set dealing with TCP connections): https://beej.us/guide/bgnet/html/multi/clientserver.html

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

However, I am unable to connect between different machines (tried on both a Debian and OpenBSD server, no iptables/pf rulesets). The connection just times out, and I'm not really sure why. I can ping and ssh into both.

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

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

netstat -an shows tcp connection listening on 3490.

like image 449
nik Avatar asked May 11 '12 05:05

nik


People also ask

Is C good for network programming?

Network programming enables processes to communicate with each other over a computer network, but it is a complex task that requires programming with multiple libraries and protocols. With its support for third-party libraries and structured documentation, C is an ideal language to write network programs.

What is meant by network programming?

Network Programming involves writing programs that communicate with other. programs across a computer network. There are many issues that arise when doing network programming which do not appear when doing single program applications. However, JAVA makes networking applications simple due to the easy-to-use libraries.

What is socket programming?

A socket is a communications connection point (endpoint) that you can name and address in a network. Socket programming shows how to use socket APIs to establish communication links between remote and local processes.


2 Answers

I don't see your servinfo being filled any where in the code

// Server should allow connections from any ip address

  serv_info.sin_addr.s_addr = INADDR_ANY;

Also you might need to fill family and port

// Fill server's address family

  serv_addr.sin_family = AF_INET;

To need more details, here is a working server i wrote on a forum http://forum.codecall.net/topic/63924-a-simple-tcp-server-using-linux-c-api/

like image 79
fkl Avatar answered Oct 01 '22 18:10

fkl


Right before if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { print out the value of p->ai_addr I'd bet it's 127.0.0.1. This would cause communication to work fine on one computer, but not happen anywhere else.

To listen on all interfaces, bind on 0.0.0.0.

like image 27
Dave Avatar answered Oct 01 '22 17:10

Dave