从客户端到服务器获取输入

时间:2013-11-15 12:36:10

标签: c++ sockets

我想我再次需要帮助..我正在尝试在我的客户端和服务器之间建立连接,如果它成功,用户可以

1) - 输入消息,消息将被发送到服务器 - 服务器会将“我收到你的消息”发回客户端。 - 用户可以输入他/她想要的消息,直到他/她输入“Q”返回主菜单。

2)退出计划。

现在我面临的问题是

1)在1.Input消息 客户端 - 服务器只读取来自客户端输入的第一条消息,之后它不会收到任何消息,而在客户端,它将严格退出程序后退出程序 我的第三个输入以及第二个和第三个输入未发送到服务器。

服务器端 - 服务器一旦传回“我得到你的消息”给客户端 收到了客户的留言。

2)并且我意识到在1.Input消息 当我在第一次输入时按Q时,它将被视为我向服务器发送消息而不是返回主菜单。如果IQ是我的第二或第三个输入,我将什么也不做并将退出程序在我的第三次输入之后

客户端输出(基于我的代码)

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
ron@ron-VirtualBox:~/Desktop/program$ <--exit the program after third input

服务器输出(基于我的代码)

 Here is the msg: a <-- only receive first message

现在,如果我重新启动程序并输入Q. 客户端输出(基于我的代码)

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
ron@ron-VirtualBox:~/Desktop/program$ <--exit the program after third input

服务器输出(基于我的代码)

 Here is the msg: Q <--treated as a message instead of going to main menu.

预期的客户输出

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:

预期的服务器输出

Here is the msg: a
Here is the msg: b
Here is the msg: c

my codes(shown below)

请帮助我,因为我真的很震惊。谢谢你提前

client.cpp

#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("-------------------------\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();
        }
   }
}

从我现在所理解的情况来看,它正在导致它现在的输出,因为它停止了 换行后阅读。 所以我需要以某种方式将'\ n'更改为'\ 0'; 我试过这样的东西,但它仍然没有用。

void print() {
    size_t length;
    std::cout << "Please enter the message: \n" << std::endl;
    bzero(buffer,256);
    while(fgets(buffer,256,stdin)) {
            printf("User Input: %s",buffer);
            length = strlen(buffer);
            /* Send message to the server */
            if(buffer[length - 1] == '\n') {
                buffer[length -1] == '\0';
                n = write(sockfd,buffer,strlen(buffer));
            }
            if (n < 0)  {
                perror("ERROR writing to socket");
                exit(1);
            }
   }
}

1 个答案:

答案 0 :(得分:1)

好吧,假设字符串缓冲区包含'Hello \ n \ 0'它的长度(由strlen()返回)是6.要发送此字符串,使用它的换行符和null,您需要发送7个字符,所以n = write(sockfd,buffer,strlen(buffer)+1);