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

标签 c++ sockets

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

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

2)退出程序。

现在我遇到的问题是

1)在1.输入消息 客户端 - 服务器只从客户端输入中读取第一条消息,之后它不会收到任何消息,在客户端,它会在之后直接退出程序 我的第三个输入以及第二个和第三个输入没有发送到服务器。

服务器端 - 服务器不会将“我收到你的消息”回传给客户端 已收到客户端的消息。

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

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

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)

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

客户端.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();
        }
   }
}

据我现在的理解,是 fgets 导致它现在输出当前输出,因为它停止了 换行后阅读。 所以我需要以某种方式将“\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);
            }
   }
}

最佳答案

好的,假设字符串缓冲区包含 'Hello\n\0' 它的长度,如 strlen() 返回的那样,是 6。要发送这个带有换行符和 null 的字符串,您需要发送 7 个字符,所以 n =写(sockfd,缓冲区,strlen(缓冲区)+ 1);

关于c++ - 从客户端获取输入到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20001244/

相关文章:

c++ - 实例化/模板函数专门化

c++ - MSVC中<>和 ""的区别

c++ - 这个构造是什么意思?

c++ - 从字符串中删除所有非字母字符

c++ - 使用 IOCP 时 OVERLAPPED 结构的用途是什么?

c - g_io_channel + socket = server ,仍然没有收到正确的数据,只得到一个客户端?用C语言

java 连接到 jabber

c - 如何在 C 的套接字上发送/接收字符串数组

异步回调中的 C++ boost-asio-network ,哪一个更好?使用 lambda 或 boost::bind()

c++ - TCP连接失败