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