Help.passing input from client to server-socket programming

I am new to socket programming and I am trying to establish a connection between my client and server and if it's successful the user can

1)
-Input message, the message will be send to the server
-the server will send a "I got your msg" back to the client.
-The user can input as many messages as he/she wants until he/she has enter "Q" to go back to the main menu.

2)exit from program.

the problem I faced was

1)At 1.Input Message
Client side - the server only reads the first message from the client input, after that it won't receive any messages, and at client side, it will striaght away exit the program after
my third input and the second and third input is not send to the server.

Server Side - server does not pass back "I got your msg" to the client once it
have received the message from the client.

2) and also I realized that At 1.Input Message
when I press Q on my first input, It will be treated as I am sending a message to the server instead of going to back to the main menu.if I Q was my second or third input, I will do nothing and will exit the program after my third input as well

client output(base on my codes)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Main Menu!

1. Input Message
2. Exit
Enter your choice:
1
Please enter the message :
a
User Input: a 
b
User Input: b
c 
User Input: c
//exit the program after third input
roberts@roberts-VirtualBox:~/Desktop/program$ 


server output(base on my codes)
1
2
3
//only a is received
Here is the msg: a


Now if I restart my program and just enter Q
client output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Main Menu!

1. Input Message
2. Exit
Enter your choice:
1
Please enter the message :
Q
User Input: Q
Q
User Input: Q
Q
User Input: Q
//exit the program after third input
roberts@roberts-VirtualBox:~/Desktop/program$ 


server output
1
2
//treated as a message instead of going to main menu.
Here is the msg: Q 


expected client output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Main Menu!

1. Input Message
2. Exit
Enter your choice:
1
Please enter the message :
a
User Input: a 
I got your msg
b
User Input: b
I got your msg
c
User Input: c
I got your msg
Q
//goes back to main menu after pressing Q
Main Menu!

1. Input Message
2. Exit
Enter your choice:


expect server output
1
2
3
Here is the msg: a
Here is the msg: b
Here is the msg: c


my codes(shown below)

please help me as I am really struck.

One last thing, I apologize because my codes is mixed with c and c++ style of codings.
will be changing it to c++ way and thus will be looking for a solution more towards c++.

thanks in advance

server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <stdbool.h>
void doPrcoessing(int sock) {
    int n;
    char buffer[255];
    bzero(buffer,256);
    n =read(sock,buffer,255);
    if(n < 0) {
        perror("Error from reading socket");
        exit(1);
    }
    if(n > 0) {
        printf("Here is the msg: %s\n",buffer);
        n=write(sock,"I got your msg",18);
    }
    if(n<0) {
        perror("Error writing to socket");
        exit(1);
    }
}
int main( int argc, char *argv[] )
{
    int sockfd,pid,newsockfd,portno,clientn;
    struct sockaddr_in serv_addr,cli_addr;
    char buffer[256];
    bool connected;
    int n;
    socklen_t len;
    /*call socket() function*/
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0)
    {
    perror("Error opening socket");
    exit(1);
    }
   // printf("socket retrieve success\n");
   /*Initialize socket structure*/
    bzero((char *) &serv_addr, sizeof(serv_addr));
    portno = atoi(argv[1]);
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);
    /*bind the host address using bind() call*/
    if(bind(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
    perror("Error on binding!");
    exit(1);
    }
    int yes =1;
    if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&yes, sizeof(yes)) == -1) {
    perror("setsockopt");
    exit(1);
    }
    /*start listening for clients, will go into sleep mode and wait for incoming connection*/
    listen(sockfd,5);
    len = sizeof(cli_addr);  
     while(connected == false) {
        newsockfd = accept(sockfd,(struct sockaddr*)&cli_addr,&len);
        if(newsockfd < 0) {
            perror("Error on accept!");
            exit(1);
            }
        /*Create chlid process*/
        pid =fork();
        if(pid < 0)
        {
            perror("Error on fork!");
            exit(1);
        }
        if(pid == 0)
        {
            /*Client process*/
            close(sockfd);
            doPrcoessing(newsockfd);
            return 0;   
        }
        else
        {
            close(newsockfd);
        }
    }
    return 0;
}


client.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netdb.h>
#include <stdbool.h>
#include <signal.h>
#include <iostream>
#include <sstream>
void msg(void);
void mainMenu();
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
bool loop = false; 
char options[100];
char buffer[255];
int choice;
char c;
int main(int argc, char *argv[])
{ 
    while(loop==false) {
        if (argc < 3) {
            fprintf(stderr,"usage %s hostname port\n", argv[0]);
            exit(0);
        }
        portno = atoi(argv[2]);
     /* Create a socket point */
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0)  {
            perror("ERROR opening socket");
            exit(1);
      }
     server = gethostbyname(argv[1]);
     if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
     }
     bzero((char *)&serv_addr, sizeof(serv_addr));
     serv_addr.sin_family = AF_INET;
     bcopy((char *)server->h_addr,(char *)&serv_addr.sin_addr.s_addr,
                server->h_length);
        serv_addr.sin_port = htons(portno);
     /* connect to the server */
        if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0) {
            perror("Error connecting");
            exit(1);
     }
        mainMenu();
    }
    close(sockfd);
        return 0;
}
void mainMenu() {
    std::cout << "\n" << std::endl;
    printf("Main Menu!\n");
 
    printf(" 1. Input Message \n");
    printf(" 2. Exit \n");
    printf("Enter your choice\n");
    scanf("%d",&choice);
    getchar();
        switch(choice) {
            case 1: msg();
                break;
            case 2: std::cout << "Exiting";
                loop = true;
                exit(0);
                break;
            default: 
                printf("%s","Invalid choice!\n");
            break;  
        }
}
void msg(void)  {
    std::cout << "Press Q to Quit" << std::endl;
    std::cout << " " << std::endl;
     /*ask for a message from the user, this message will be read by server */
    std::cout << "Please enter the message : \n" <<std::endl;       
    bzero(buffer,256);
    while(fgets(buffer,255,stdin)) {
        printf("User Input: %s",buffer);
        /* Send message to the server */
            n = write(sockfd,buffer,strlen(buffer));
        if (n < 0)  {
                perror("ERROR writing to socket");
                exit(1);
            }
        /* read server response */
            bzero(buffer,256);
            n = read(sockfd,buffer,256);
            if (n < 0)  {
                perror("ERROR reading from socket");
                exit(1);
            }
            if(strncmp(buffer,"Q",1)==0) {
                mainMenu();
            }
    }
}
Topic archived. No new replies allowed.