1

i have an infinite loop problem while im trying to send some messages from client to the server. Client has some commands like login, getusers, alias etc. and i want to check them into server. Here its my code.

CLIENT

#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h> // for inet_addr
#include <string.h>
#include <zconf.h>

int main(int argc, char *argv[]) {
    int  sock;
    struct sockaddr_in server;
    char message[2000], server_reply[2000];

    //Create socket
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
        printf("Could not create socket");
    }

    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family      = AF_INET;
    server.sin_port        = htons(8888);

    //Connect to remote server
    if (connect(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
        perror("Connect failed. Error");
        return 1;
    }

    puts("Connected to server\n");


    //keep communicating with server
    while (1) {

        printf("> ");
        scanf("%[^\n]s", message);
        fflush(stdin);

        //Send some data
        if (send(sock, message, strlen(message), 0) < 0) {
            puts("Send failed");
            return 1;
        }

        //Receive a reply from the server
        if (recv(sock, server_reply, 2000, 0) < 0) {
            puts("recv failed");
            break;
        }

        printf("Server's reply : %s ", server_reply);
    }

    close(sock);
    return 0;
}

SERVER

#include<stdio.h>
#include<string.h>    // for strlen
#include<stdlib.h>
#include<sys/socket.h>
#include<arpa/inet.h> // for inet_addr
#include<unistd.h>    // for write
#include<pthread.h>   // for threading, link with lpthread
#include "server.h"
#include "split.h"

#define MAX_CLIENT_NUMBER 100

void *connection_handler(void *);

struct User {
    char userName[10];
    int  clientSocketNo;
};

struct User users[MAX_CLIENT_NUMBER];

void getUsers() {
    printf("Number %d",userArrayIndex);

    for (int i = 0; i < userArrayIndex; ++i) {
        printf("%s\n", users[i].userName);
    }
}

void addUserToArray(char userName[10], int socketNumber) {
    printf("Client logged in as %s\n", userName);
    strcpy(users[userArrayIndex].userName, userName);
    users[userArrayIndex].clientSocketNo = socketNumber;
    userArrayIndex++;
}

void loginUser(char userName[10], int socketNumber) {
    char *message = "login successful";
    write(socketNumber, message, strlen(message));
    addUserToArray(userName, socketNumber);
}

void *connection_handler(void *socket_desc) {
    //Get the socket descriptor
    char receivedMessage[2000];  //client's message

    int  readControl;
    int  sock            = *((int *) socket_desc);

    while ((readControl = recv(sock, receivedMessage, 2000, 0)) > 0) {
        char **parsedCommand = malloc(100); //parsedClientMessage

        parsing(parsedCommand, receivedMessage, " ");
        printf("MESSAGE %s\n",parsedCommand[0]);

        if (strcmp(parsedCommand[0], "login") == 0) {
            loginUser(parsedCommand[1], sock);
        }

        if (strcmp(parsedCommand[0], "getusers") == 0) {

            getUsers();
        }

        if (strcmp(parsedCommand[0], "exit") == 0) {
            close(sock);
            return 0;
        }

    }

    if (readControl == 0) {
        puts("Client disconnected");
        clientNumber--;
        fflush(stdout);
    } else if (readControl == -1) {
        perror("recv failed");
    }

    //Free the socket pointer
    free(socket_desc);
    return 0;
}

int main(int argc, char *argv[]) {

    int socket_desc, new_socket, c, *new_sock;
    struct sockaddr_in server, client;

    //Create Socket
    socket_desc = socket(AF_INET, SOCK_STREAM, 0);
    if (socket_desc == -1) {
        puts("Could not create socket");
        return 1;
    }

    server.sin_family      = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port        = htons(8888);

    if (bind(socket_desc, (struct sockaddr *) &server, sizeof(server)) < 0) {
        puts("Binding failed");
        return 1;
    }

    listen(socket_desc, 3);

    puts("Server started");
    c                  = sizeof(struct sockaddr_in);
    while ((new_socket = accept(socket_desc, (struct sockaddr *) &client, (socklen_t *) &c)) &&
           clientNumber < MAX_CLIENT_NUMBER) {
        pthread_t sniffer_thread/*[MAX_CLIENT_NUMBER]*/;
        new_sock = malloc(1);
        *new_sock = new_socket;

        if (pthread_create(&sniffer_thread/*[clientNumber]*/, NULL, connection_handler,
                           (void *) new_sock) < 0) {
            perror("Could not create thread");
            return 1;
        } else {
            clientNumber++;
        }

        puts("Client connected");
    }


    if (new_socket < 0) {
        perror("accept failed");
        return 1;
    }

    return 0;
}

Its not full of my code but i think our problem in these parts. I dont understand why it happens. When i insert a break command into connection_handler's while loop, i cant send commands to the server anymore. Thanks...

16
  • 1
    You must describe your problem, not just say you have one. Commented Dec 27, 2016 at 16:50
  • In client side, running should be stop in scanf for receive new commands but it doesnt stop Commented Dec 27, 2016 at 16:53
  • 1
    As is always the case in 'running' code, try a debugger Commented Dec 27, 2016 at 17:00
  • @Berkin, how be sure that "it doesnt stop" ? When simulate that line scanf("%[^\n]s", message);, no problem detected = stop until <return>. Commented Dec 27, 2016 at 17:20
  • I added full of my code, can you check it again Commented Dec 27, 2016 at 18:00

2 Answers 2

1

I solved it. when i malloc the messages or replies, i give +1 to strlen(messsage).

So its strlen(message)+1 solved my problem

Sign up to request clarification or add additional context in comments.

Comments

0

Because the '\n' is not removed from the input, the scanf() function is able to reuse that last character for a next entry.

The easiest solution to solve that problem is to clear the input buffer fflush(stdin);.

while (1) {
    printf("> ");
    scanf("%[^\n]s", message);
    fflush(stdin); // to clear \n from the buffer
    //Send some data
    if (send(sock, message, strlen(message), 0) < 0) {

1 Comment

I added full of my code, can you check it again. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.