gpt4 book ai didi

c++ - 套接字编程 简单聊天 C++

转载 作者:行者123 更新时间:2023-11-28 00:07:05 26 4
gpt4 key购买 nike

我正在尝试使用套接字编程实现一个简单的聊天,其中“输入”键将响应从客户端发送到服务器,反之亦然。下面的代码按预期使用特殊字符(例如“*”)作为缓冲区,但是当我尝试使用“\n”时,消息永远不会发送。我一定是错误地实现了“输入”部分。关于如何解决此问题的任何想法?

服务器:

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

using namespace std;

int main()
{

int client, server;
int portNum = 1500;
bool isExit = false;
int bufsize = 1024;
char buffer[bufsize];

struct sockaddr_in server_addr;
socklen_t size;

client = socket(AF_INET, SOCK_STREAM, 0);

if (client < 0)
{
cout << "\nError establishing socket..." << endl;
exit(1);
}

cout << "\n=> Socket server has been created..." << endl;

server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htons(INADDR_ANY);
server_addr.sin_port = htons(portNum);


if ((bind(client, (struct sockaddr*)&server_addr,sizeof(server_addr))) < 0)
{
cout << "=> Error binding connection, the socket has already been established..." << endl;
return -1;
}


size = sizeof(server_addr);
cout << "=> Looking for clients..." << endl;

listen(client, 1);


int clientCount = 1;
server = accept(client,(struct sockaddr *)&server_addr,&size);

// first check if it is valid or not
if (server < 0)
cout << "=> Error on accepting..." << endl;

while (server > 0)
{
strcpy(buffer, "=> Server connected...\n");
send(server, buffer, bufsize, 0);
cout << "=> Connected with the client #" << clientCount << ", you are good to go..." << endl;
cout << "\n=> Enter # to end the connection\n" << endl;

cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');

do {
cout << "\nServer: ";
do {
cin >> buffer;
send(server, buffer, bufsize, 0);
if (*buffer == '#') {
send(server, buffer, bufsize, 0);
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');

cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');
} while (!isExit);

cout << "\n\n=> Connection terminated with IP " << inet_ntoa(server_addr.sin_addr);
close(server);
cout << "\nGoodbye..." << endl;
isExit = false;
exit(1);
}

close(client);
return 0;
}

客户:

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

using namespace std;

int main()
{

int client;
int portNum = 1500; // NOTE that the port number is same for both client and server
bool isExit = false;
int bufsize = 1024;
char buffer[bufsize];
char* ip = "192.168.0.8";

struct sockaddr_in server_addr;

client = socket(AF_INET, SOCK_STREAM, 0);

if (client < 0)
{
cout << "\nError establishing socket..." << endl;
exit(1);
}


cout << "\n=> Socket client has been created..." << endl;

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(portNum);

if (connect(client,(struct sockaddr *)&server_addr, sizeof(server_addr)) == 0)
cout << "=> Connection to the server port number: " << portNum << endl;


cout << "=> Awaiting confirmation from the server..." << endl; //line 40
recv(client, buffer, bufsize, 0);
cout << "=> Connection confirmed, you are good to go...";

cout << "\n\n=> Enter # to end the connection\n" << endl;

do {
cout << "Client: ";
do {
cin >> buffer;
send(client, buffer, bufsize, 0);
if (*buffer == '#') {
send(client, buffer, bufsize, 0);
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');

cout << "Server: ";
do {
recv(client, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer = '\n';
isExit = true;
}

} while (*buffer != '\n');
cout << endl;

} while (!isExit);

cout << "\n=> Connection terminated.\nGoodbye...\n";

close(client);
return 0;
}

编辑:我做了下面答案中引用的更改,但我仍然没有得到预期的结果。

这是我运行服务器和客户端时发生的情况: enter image description here

当我按 Enter 时,消息没有发送到服务器。但是,当我使用“buffer = '*'”运行相同的代码时,它按预期工作。

最佳答案

我不确定你的代码应该做什么。

*buffer == '\n';

这是你的错误吗?

它位于服务器代码中:

        cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer == '\n';
isExit = true;
}
} while (*buffer != '\n');

我想你的意思是:

        cout << "Client: ";
do {
recv(server, buffer, bufsize, 0);
cout << buffer << " ";
if (*buffer == '#') {
*buffer = '\n';
isExit = true;
}
} while (*buffer != '\n');

看看测试驱动开发这个主题。

关于c++ - 套接字编程 简单聊天 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35019684/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com