Socket Not Sending Back To Server C

About programming and getting involved with Linux Mint development
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Post Reply
Slicybtw
Level 1
Level 1
Posts: 10
Joined: Sat Mar 09, 2024 1:44 pm

Socket Not Sending Back To Server C

Post by Slicybtw »

im making a program where the server send a command to the client, executes it and sends back the result to the server
but it doesnt work, the server is stuck on read after it sends the command and client receives it but doesnt send anything back, the command is executed too
i tried sending something else from the client like "Hi" and it works but doesnt work when i want to send a commands result

Server Code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//Sizes
#define BUFFER_MAX 1024
//#define HIGH_BUFFER 11056
//Server DATA
#define server_ip "192.168.1.107"
#define port 7054


int main() {
int sockfd, newsockfd;
int option = 1;

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if(sockfd < 0) {
        perror("Failed Creating Socket: ");
        return EXIT_FAILURE;
}

if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0) {
        perror("Failed Setting Socket Options: ");
        return EXIT_FAILURE;
}

struct sockaddr_in serv_addr, cli_addr;

memset(&serv_addr, 0, sizeof(serv_addr));

socklen_t clilen;

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = inet_addr(server_ip);


if(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        perror("Couldnt Bind IP And PORT: ");
        return EXIT_FAILURE;
}

if(listen(sockfd, 5) < 0) {
        perror("Couldn't Listen For Incoming Connections: ");
        return EXIT_FAILURE;
}

clilen = sizeof(cli_addr);

newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr ,&clilen );

if(newsockfd < 0) {
        perror("Couldn't accept connection from client: ");
        return EXIT_FAILURE;
}

printf("Connection Accpeted (%s)\n\n", inet_ntoa(cli_addr.sin_addr));

while(1) {

jump:
char buffer[BUFFER_MAX] = {0};
char full_response[BUFFER_MAX] = {0};

printf("SecureShell@%s$ ", inet_ntoa(cli_addr.sin_addr));
fgets(buffer, sizeof(buffer), stdin);

send(newsockfd, buffer, strlen(buffer), 0);
if(strncmp("disconnect", buffer, 10) == 0) {
close(sockfd);
close(newsockfd);
printf("\nDisconnecting.");
break;
} else {
recv(newsockfd, full_response, sizeof(full_response), MSG_WAITALL);
printf("%s\n", full_response);
}

memset(buffer, 0, sizeof(buffer));
memset(full_response, 0, sizeof(full_response));
}
return EXIT_SUCCESS;
}

Client Code:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define BUFFER_SIZE 1024
#define FULL_BUFFER 11056

#define server_ip "192.168.1.107"
#define port 7054

int main() {
char buffer[BUFFER_SIZE];
char container[BUFFER_SIZE];
char full_response[BUFFER_SIZE];


FILE *fp;

int sockfd;

sockfd = socket(AF_INET, SOCK_STREAM, 0);

if(sockfd < 0) {
        perror("Couldn't create socket: ");
        return EXIT_SUCCESS;
}


struct sockaddr_in serv_addr;

memset(&serv_addr, 0, sizeof(serv_addr));


serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = inet_addr(server_ip);

//Loop Connection if server is off
while(connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0);

memset(buffer, 0, sizeof(buffer));
memset(full_response, 0, sizeof(full_response));
memset(container, 0, sizeof(container));

if(recv(sockfd, buffer, sizeof(buffer),0) < 0) {
 perror("Error on reading: ");
}

fp = popen(buffer, "r");
if(fp == NULL) {
        perror("Cant Open POPEN: ");
}

while(full_response, sizeof(full_response), fp != NULL) {
        if(write(sockfd, full_response, strlen(full_response)) < 0) {
                perror("Couldnt Write Message: ");
}

}



return EXIT_SUCCESS;
}
Any Help will be appreciated :D
Last edited by karlchen on Thu Aug 29, 2024 8:45 am, edited 1 time in total.
Reason: Thread moved from "Software & applications" to "Programming & Development"
JustBooTwo
Level 1
Level 1
Posts: 7
Joined: Thu Aug 01, 2024 1:35 pm

Re: Socket Not Sending Back To Server C

Post by JustBooTwo »

"Give a man a fish, he eats for a day. Teach a man to fish and he eats for a lifetime." 8)

You don't mention the IDE you are using, but I would definitely use a full featured one with "debugging" capability. The ability to debug your code is invaluable. So a simple editor and command line is much harder to debug with. Eclipse(with the CDT) is a free choice.Or perhaps vscode owned by Dr. Evil.

Set a "break point" in your code. A break point means the program will pause at that point in your code. Search for this term (if not known).
Set a break point on the very first line of code in main(). You can then "step through" your code (go line by line) and "watch" your variables initialize and change / update and observe the execution path of the code itself, over the course of the app running. Look up "watch" and how to set up watch variables. Most IDEs have a way to make them easy to see and... watch.

By debugging your code, line by line, you will be able to see where the code is not doing the right thing. And gain a much deeper understanding of how code and programming in general works. It is in fact, invaluable. Good Luck.
Post Reply

Return to “Programming & Development”