TCP connecting over internet C | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

TCP connecting over internet C

Hello I have simple client-server tcp code. it only works on one device. How to edit it so i can access the server from different device and from different network? Here is the code: TCP_CLIENT: #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> int main(int argc, char const *argv[]) { int network_socket = 0; network_socket = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in server_address; server_address.sin_family = AF_INET; server_address.sin_port = htons(9002); server_address.sin_addr.s_addr = INADDR_ANY; int connection_status = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address)); if (connection_status == -1) { printf("Connection Error!"); return 0; } char server_response[256]; recv(network_socket, server_response, sizeof(server_response), 0); printf("Data from server %s\n", server_response); close(network_socket); return 0; } TCP_SERVER: #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> int main(int argc, char const *argv[]) { char server_message[256] = "You have reached the server"; int server_socket = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in server_address; server_address.sin_family = AF_INET; server_address.sin_port = htons(9002); server_address.sin_addr.s_addr = INADDR_ANY; bind(server_socket, (struct sockaddr*) &server_address, sizeof(server_address)); listen(server_socket, 0); int client_socket; client_socket = accept(server_socket, NULL, NULL); send(client_socket, server_message, sizeof(server_message), 0); close(server_socket); return 0; } ----- I have another project that does the same thing but with HTTP, when i run the server, the server loads html and send it to user (browser only). its working only on localhost too. So if anybody knows how to do it nonlocal, i can send it. Thank you for any help

1st Nov 2020, 3:32 PM
Kry$tof
12 Answers
+ 3
I haven't fully grokked the entirety of this code... but I'm assuming that binding to "127.0.0.1" is restricting connections to processes on the same server. I'd imagine you would need to bind to an IP that that's visible and accessible to other devices on the same network. For accessing from outside the network, you'll likely need to use port forwarding to route requests external to the network to the internal server IP. You'll also want to open ports on respective firewalls (PC and router).
1st Nov 2020, 10:57 PM
David Carroll
David Carroll - avatar
+ 2
Kry$tof Windows machines also have firewalls, you can simply use windows search to find it and open port 9002. To get the router's IP address, you can use `ipconfig` in a command prompt. It will be listed under "default gateway".
2nd Nov 2020, 12:22 AM
Schindlabua
Schindlabua - avatar
+ 1
I didn't run your code but it probably is not a code issue. By "localhost" you probably mean your PC at home, running linux (?), where all sorts of firewalls block access from the outside. - Check your router address with `ip route` (it says "default via ... dev eth0") - log into your router in the browser, the password will be written on the underside of your router - Check the router's firewall if it has any. Disabling it is fine for testing but remember to turn it back on. Better is opening a single port. - Check the port forwarding settings and forward the necessary port to your local machine. - Open that port on your linux machine, for example with ufw the command would be `sudo ufw allow 9002`. Depends on your distro of course. After that you should be able to reach your server over the internet, using your public IP. (https://www.whatismyip.com)
1st Nov 2020, 7:15 PM
Schindlabua
Schindlabua - avatar
+ 1
In your TCP client code: server_address.sin_addr.s_addr = INADDR_ANY will make your socket accept all connections. To connect to your server you need to assign your server's IP address in binary format instead of INADDR_ANY In order to do it include ws2tcpip.h header file and use the function inet_pton as "inet_pton(AF_INET, "your server IP", &sin_addr.s_addr)". Try it and point out errors if any.
1st Nov 2020, 7:42 PM
Valmob101
Valmob101 - avatar
0
@Schindlabua I dont have any issue with the code. The server simply sends message when somebody connect. The problem is that it only works on my windows 10 machine. If I try to connect to my server from different PC it just doesnt connect. The server is only available on my win10 PC. I want to know how can I make the server "global". I want to connect to my server from different location and from different PC.
1st Nov 2020, 8:52 PM
Kry$tof
0
@Valmob101 I dont have problem with client. client works fine but it only works when I have server and client on same machine. I want to connect to server from different machine.
1st Nov 2020, 8:55 PM
Kry$tof
0
Kry$tof In order to connect any outside server client must know its IP. Your client code lacks this functionality. The code works on localhost because they are sharing the same INADDR_ANY.
1st Nov 2020, 9:03 PM
Valmob101
Valmob101 - avatar
0
@Valmob101 I added "inet_pton(AF_INET, 127.0.0.1, &sin_addr.s_addr);" to my code right after "server_address.sin_addr.s_addr = INADDR_ANY;" and Im getting lot of errros.
1st Nov 2020, 9:16 PM
Kry$tof
0
Add ip address in double quotes remove the line "server_address.sin.....=....."
1st Nov 2020, 9:20 PM
Valmob101
Valmob101 - avatar
0
@Valmob101 struct sockaddr_in server_address; server_address.sin_family = AF_INET; server_address.sin_port = htons(9002); //server_address.sin_addr.s_addr = INADDR_ANY int in = inet_pton(AF_INET, "127.0.0.1", &server.sin_addr); in = connect(network_socket, (struct sockaddr *) &server_address, sizeof(server_address)); if (in == -1) { printf("Connection Error!"); return 0; } still getting lots of errors, most of them arent even from my source code
1st Nov 2020, 9:26 PM
Kry$tof
0
Your 3rd argument is incorrect. Make sure you have include appropriate header files. <arpa/inet.h> for Linux <ws2tcpip.h> for Windows Also make sure you have Ws2_32.lib linked in your linker if you have windows.
1st Nov 2020, 9:40 PM
Valmob101
Valmob101 - avatar
0
127.0. 0.1 is the loopback Internet protocol which will forward data to your own client IP. Provide your web server IP instead.
2nd Nov 2020, 1:33 AM
Valmob101
Valmob101 - avatar