Reputation: 5
The following is the code I've done with the message, but for some reason I keep getting a message saying "Problem connecting to the socket! Sorry!!" followed by this GET message:
Server says:
GET / HTTP/1.1
Host: 006010fb-d2cb-4a56-9e26-21bf35548134.id.replitusercontent.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36
Accept: text/html,application/xhtml+xml,apP�s�
Can anyone look over this and tell me what I'm missing/doing wrong? This is my first time doing server/client coding.
/**
* @brief Main function of the program. It is called like this "program <port> <encryptedMessage>",
* where <port> means the address of the webserver and <encryptedMessage> is the message to show
*
* @param argc number of arguments
* @param argv pointer to arguments
* @return int 0 if everything is ok
*/
//Basic TCP Client: socket() creation > connect() > receive() > display > close
#include <stdio.h> //Standard library
#include <stdlib.h> //Standard library
#include <sys/socket.h> //API and definitions for the sockets
#include <sys/types.h> //more definitions
#include <netinet/in.h> //Structures to store address information
#include <unistd.h> //close function
#include <string.h>
#define PORT 8080
int main()
{
char tcp_server_message[256] = " Hello, I am the TCP Server you successfully connected to!! \n\n Bye Bye!!!\n\n";
//create the server socket
int tcp_server_socket; //variable for the socket descriptor
tcp_server_socket = socket(AF_INET, SOCK_STREAM, 0); //calling the socket function. Params: Domain of the socket (Internet in this case), type of socket stream (TCP), Protocol (default, 0 for TCP)
//creating the TCP socket
int tcp_client_socket; //Socket descriptor
tcp_client_socket = socket(AF_INET, SOCK_STREAM, 0); //Calling the socket function - args: socket domain, socket stream type, TCP protocol (default)
//specify address and port of the remote socket
struct sockaddr_in tcp_server_address; //declaring a structure for the address
tcp_server_address.sin_family = AF_INET; //Structure Fields' definition: Sets the address family of the address the client would connect to
tcp_server_address.sin_port = htons(PORT); //Specify and pass the port number to connect - converting in right network byte order
tcp_server_address.sin_addr.s_addr = INADDR_ANY; //Connecting to 0.0.0.0
// binding the socket to the IP address and port
bind(tcp_server_socket, (struct sockaddr *) &tcp_server_address, sizeof(tcp_server_address)); //Params: which socket, cast for server address, its size
//listen for simultaneous connections
listen(tcp_server_socket, 5); //which socket, how many connections
// server socket to interact with client, structure like before - if you know - else NULL for local connection
tcp_client_socket = accept(tcp_server_socket, NULL, NULL);
//send data stream
send(tcp_client_socket, tcp_server_message, strlen(tcp_server_message), 0); // send where, what, how much, flags (optional)
//connecting to the remote socket
int connection_status = connect(tcp_client_socket, (struct sockaddr *) &tcp_server_address, sizeof(tcp_server_address)); //params: which socket, cast for address to the specific structure type, size of address
if (connection_status == -1)
{ //return value of 0 means all okay, -1 means a problem
printf(" Problem connecting to the socket! Sorry!! \n");
}
char tcp_server_response[256];
recv(tcp_client_socket, &tcp_server_response, sizeof(tcp_server_response), 0); // params: where (socket), what (string), how much - size of the server response, flags (0)
//Output, as received from Server
printf("\n\n Server says: %s \n", tcp_server_response);
//closing the socket
close(tcp_client_socket);
return 0;
}
run.bash
#!/bin/bash
# DO NOT MODIFY THIS FILE
# This script compiles and runs the webserver.
c++ -o src/serveMessage src/main.cpp
src/serveMessage 8080 "We will meet at midnight"
Upvotes: 0
Views: 897
Reputation: 126
You need to separate the program into a server part and a client part. Perform the server program first and make it listening to the client's access. And then perform the client program. This is the server part (Server.cpp).
:
:
int main()
{
char tcp_server_message[256] = " Hello, I am the TCP Server you successfully connected to!! \n\n Bye Bye!!!\n\n";
//create the server socket
int tcp_server_socket; //variable for the socket descriptor
tcp_server_socket = socket(AF_INET, SOCK_STREAM, 0); //calling the socket function. Params: Domain of the socket (Internet in this case), type of socket stream (TCP), Protocol (default, 0 for TCP)
//creating the TCP socket
int tcp_client_socket; //Socket descriptor
//tcp_client_socket = socket(AF_INET, SOCK_STREAM, 0); //Calling the socket function - args: socket domain, socket stream type, TCP protocol (default)
//specify address and port of the remote socket
struct sockaddr_in tcp_server_address; //declaring a structure for the address
tcp_server_address.sin_family = AF_INET; //Structure Fields' definition: Sets the address family of the address the client would connect to
tcp_server_address.sin_port = htons(PORT); //Specify and pass the port number to connect - converting in right network byte order
tcp_server_address.sin_addr.s_addr = INADDR_ANY; //Connecting to 0.0.0.0
// binding the socket to the IP address and
//Params: which socket, cast for server address, its size
if (bind(tcp_server_socket, (struct sockaddr *) &tcp_server_address, sizeof(tcp_server_address)) < 0)
{
printf("Error binding\n");
}
else
{
printf("Bind successfully!\n");
}
//listen for simultaneous connections
if(listen(tcp_server_socket, 5) < 0) { // listen for incoming connections
printf("Error listening\n");
}
else
{
printf("Success in listening\n");
}
// server socket to interact with client, structure like before - if you know - else NULL for local connection
if((tcp_client_socket=accept(tcp_server_socket, NULL, NULL)) < 0) { // accept one
printf("Error accepting\n");
}
else
{
printf("Success in accepting\n");
//send data stream
send(tcp_client_socket, tcp_server_message, strlen(tcp_server_message), 0); // send where, what, how much, flags (optional)
}
//closing the socket
close(tcp_server_socket);
return 0;
}
This is the client part (Client.cpp).
:
:
int main()
{
//creating the TCP socket
int tcp_client_socket; //Socket descriptor
tcp_client_socket = socket(AF_INET, SOCK_STREAM, 0); //Calling the socket function - args: socket domain, socket stream type, TCP protocol (default)
//specify address and port of the remote socket
struct sockaddr_in tcp_server_address; //declaring a structure for the address
tcp_server_address.sin_family = AF_INET; //Structure Fields' definition: Sets the address family of the address the client would connect to
tcp_server_address.sin_port = htons(PORT); //Specify and pass the port number to connect - converting in right network byte order
tcp_server_address.sin_addr.s_addr = INADDR_ANY; //Connecting to 0.0.0.0
//connecting to the remote socket
int connection_status = connect(tcp_client_socket, (struct sockaddr *) &tcp_server_address, sizeof(tcp_server_address)); //params: which socket, cast for address to the specific structure type, size of address
if (connection_status == -1)
{ //return value of 0 means all okay, -1 means a problem
printf(" Problem connecting to the socket! Sorry!! \n");
}
else
{
char tcp_server_response[256];
recv(tcp_client_socket, &tcp_server_response, sizeof(tcp_server_response), 0); // params: where (socket), what (string), how much - size of the server response, flags (0)
//Output, as received from Server
printf("\n\n Server says: %s \n", tcp_server_response);
}
//closing the socket
close(tcp_client_socket);
return 0;
}
Upvotes: 1
Reputation: 43278
You forgot to bind your client socket. tcp_client_socket
has been initialize with socket()
but not yet bound. Typically you bind to 0 IP and 0 port for outbound, and HTTP doesn't say otherwise so that's what you do.
Upvotes: 0