2
I am using the codes below to make a client-server connection. The client I am making myself and the server for the time being picked up on the internet. The connection between them is made and the server sends a message, the client receives the message and prints on the terminal, but when I try to send a message from the client to the server the two end the connection.
Customer class:
#ifndef socket_hpp
#define socket_hpp
#include <string>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
namespace Socket {
class Exception {
private:
std::string _message;
public:
Exception(std::string error) { this->_message = error; }
virtual const char* what() { return this->_message.c_str(); }
};
class TCP_IPV4 {
private:
int port;
std::string address;
int buffer_len;
char *buffer;
bool binded;
struct sockaddr_in server;
int socketfd;
public:
TCP_IPV4(int port, std::string address, int buffer_len);
~TCP_IPV4();
char* getMessage();
int _connect ();
int _recv(char* message);
void _send(char* message);
void _close();
int _bind();
};
TCP_IPV4::TCP_IPV4(int port, std::string address, int buffer_len) {
if ((socketfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
throw Exception("[Constructor] Cannot create socket");
this->port = port;
this->address = address;
this->buffer_len = buffer_len;
buffer = new char[buffer_len];
binded = false;
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(address.c_str());
memset(server.sin_zero, 0x0, 8);
}
TCP_IPV4::TCP_IPV4::~TCP_IPV4() {
delete buffer;
}
int TCP_IPV4::_connect() {
int connection = connect(socketfd, (struct sockaddr*) &server, sizeof(server));
if (connection == -1)
throw Exception("[Connect] Cannot connect socket");
return connection;
}
int TCP_IPV4::_recv(char* message) {
memset(buffer, 0x0, buffer_len);
int len = recv(socketfd, buffer, buffer_len, 0);
buffer[len + 1] = '\0';
strcpy(message, buffer);
return len;
}
void TCP_IPV4::_send(char* message) {
send(socketfd, message, strlen(message), 0);
}
void TCP_IPV4::_close() {
close(socketfd);
}
}
#endif
And this part uses the client I’m doing:
#include "./socket.hpp"
#include <iostream>
using namespace std;
int main () {
try {
Socket::TCP_IPV4 socket(4242, "127.0.0.1", 4242);
socket._connect();
char message[4242];
while(1) {
cout << "message: ";
cin >> message;
if(strcmp(message, "exit"))
break;
socket._send(message);
cout << "message has been sended" << endl;
socket._recv(message);
cout << "_recv message: " << message << endl;
}
}
catch(Socket::Exception &e) {
cout << e.what() << endl;
}
return 0;
}
Server I picked up on the Internet:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
/* Server port */
#define PORT 4242
/* Buffer length */
#define BUFFER_LENGTH 4096
/*
* Main execution of the server program of the simple protocol
*/
int
main(void) {
/* Client and Server socket structures */
struct sockaddr_in client, server;
/* File descriptors of client and server */
int serverfd, clientfd;
char buffer[BUFFER_LENGTH];
fprintf(stdout, "Starting server\n");
/* Creates a IPv4 socket */
serverfd = socket(AF_INET, SOCK_STREAM, 0);
if(serverfd == -1) {
perror("Can't create the server socket:");
return EXIT_FAILURE;
}
fprintf(stdout, "Server socket created with fd: %d\n", serverfd);
/* Defines the server socket properties */
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
memset(server.sin_zero, 0x0, 8);
/* Handle the error of the port already in use */
int yes = 1;
if(setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR,
&yes, sizeof(int)) == -1) {
perror("Socket options error:");
return EXIT_FAILURE;
}
/* bind the socket to a port */
if(bind(serverfd, (struct sockaddr*)&server, sizeof(server)) == -1 ) {
perror("Socket bind error:");
return EXIT_FAILURE;
}
/* Starts to wait connections from clients */
if(listen(serverfd, 1) == -1) {
perror("Listen error:");
return EXIT_FAILURE;
}
fprintf(stdout, "Listening on port %d\n", PORT);
socklen_t client_len = sizeof(client);
if ((clientfd=accept(serverfd,
(struct sockaddr *) &client, &client_len )) == -1) {
perror("Accept error:");
return EXIT_FAILURE;
}
/* Copies into buffer our welcome messaage */
strcpy(buffer, "Hello, client!\n\0");
/* Sends the message to the client */
if (send(clientfd, buffer, strlen(buffer), 0)) {
fprintf(stdout, "Client connected.\nWaiting for client message ...\n");
/* Communicates with the client until bye message come */
do {
/* Zeroing buffers */
memset(buffer, 0x0, BUFFER_LENGTH);
/* Receives client message */
int message_len;
if((message_len = recv(clientfd, buffer, BUFFER_LENGTH, 0)) > 0) {
buffer[message_len - 1] = '\0';
printf("Client says: %s\n", buffer);
}
/* 'bye' message finishes the connection */
if(strcmp(buffer, "bye") == 0) {
send(clientfd, "bye", 3, 0);
} else {
send(clientfd, "yep\n", 4, 0);
}
} while(strcmp(buffer, "bye"));
}
/* Client connection Close */
close(clientfd);
/* Close the local socket */
close(serverfd);
printf("Connection closed\n\n");
return EXIT_SUCCESS;
}
at a glance it seems that the code works, but first thing to do is put error handling on receive and send to know what is REALLY going on
– zentrunix