gpt4 book ai didi

C++ 奇怪的套接字数据

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:08:55 25 4
gpt4 key购买 nike

大家好,这是我的代码。

int main() { 

char buffer[BUFSIZE];

// define our address structure, stores our port
// and our ip address, and the socket type, etc..
struct sockaddr_in addrinfo;
addrinfo.sin_family = AF_INET;
addrinfo.sin_port = htons(PORT);
addrinfo.sin_addr.s_addr = INADDR_ANY;


// create our socket.
int sock;
if ( (sock = socket(addrinfo.sin_family, SOCK_STREAM, 0)) < 0) {
cout << "Error in creating the socket.";
}

// bind our socket to the actual adress we want
if (bind(sock, (struct sockaddr*)&addrinfo, sizeof(addrinfo)) != 0) {
cout << "Error in binding.";
}

// open the socket up for listening
if (listen(sock, 5) != 0) {
cout << "Error in opening listener.";
}
cout << "Waiting for connections...." << endl;

char *msg = "Success! You are connected.\r\n";

// continuously accept new connections.. but no multithreading.. yet
while(1) {

struct sockaddr_in client_addr;
socklen_t sin_size = sizeof(client_addr);

if(int client = accept(sock, (struct sockaddr*)&client_addr, &sin_size)) {
cout << "Recived new connection from " << inet_ntoa(client_addr.sin_addr) << endl;
send(client, msg, strlen(msg), 0);
while(1) {
send(client, buffer, recv(client, buffer, BUFSIZE, 0), 0);

cout << buffer << endl;
strcpy(buffer, "");
}

} else {
cout << "Error in accepting new connection." << endl;
}

}

close(sock);
return 0;
}

现在,我对套接字还很陌生,我只是想对它们有所了解,但我确实有一些使用 PHP 的套接字的经验。我在我的 linux 机器上通过 putty 使用 telnet 来测试这个,我不知道这是否会导致任何问题,但服务器正在输出一些奇怪的字符,我不知道为什么。我认为这与缓冲区有关,但我不太确定。我可以通过 telnet 将诸如“hi”之类的内容发送到服务器,它会很好地输出它们并将它们发回给我,但是当我发送诸如“hoobla”之类的内容时,它会启动一些古怪的字符内容。任何的意见都将会有帮助!

提前致谢!

最佳答案

因为 recv 没有空终止你的缓冲区,你得到了垃圾打印。

下面代码中的重要部分是:

int num = recv(client,buffer,BUFSIZE,0);
if (num < 1) break;

send(client, ">> ", 3, 0); // <<-- Nice to have.
send(client, buffer, num, 0);

buffer[num] = '\0'; // <<-- Really important bit!

if (buffer[num-1] == '\n') // <<-- Nice to have.
buffer[num-1] = '\0'; // <<-- Nice to have.

cout << buffer << endl;

这将在尝试打印之前正确终止您的缓冲区,并删除尾随的换行符(如果存在)(并允许客户端区分输入行和回显行)。

这个(一个完整的程序)效果更好一些:

using namespace std;
#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>

#define BUFSIZE 1000
#define PORT 1234

int main() {
char buffer[BUFSIZE];

// define our address structure, stores our port
// and our ip address, and the socket type, etc..
struct sockaddr_in addrinfo;
addrinfo.sin_family = AF_INET;
addrinfo.sin_port = htons(PORT);
addrinfo.sin_addr.s_addr = INADDR_ANY;

// create our socket.
int sock;
if ( (sock = socket(addrinfo.sin_family, SOCK_STREAM, 0)) < 0) {
cout << "Error in creating the socket.";
return -1;
}

// bind our socket to the actual adress we want
if (bind(sock, (struct sockaddr*)&addrinfo, sizeof(addrinfo)) != 0) {
cout << "Error in binding.";
return -1;
}

// open the socket up for listening
if (listen(sock, 5) != 0) {
cout << "Error in opening listener.";
return -1;
}

char *msg = "Success! You are connected.\r\n";

// continuously accept new connections.. but no multithreading.. yet
while(1) {
cout << "Waiting for connections...." << endl;

struct sockaddr_in client_addr;
socklen_t sin_size = sizeof(client_addr);

if(int client =
accept(sock, (struct sockaddr*)&client_addr, &sin_size))
{
cout << "Recieved new connection from "
<< inet_ntoa(client_addr.sin_addr) << endl;
send(client, msg, strlen(msg), 0);
while(1) {
int num = recv(client,buffer,BUFSIZE,0);
if (num < 1) break;
send(client, ">> ", 3, 0);
send(client, buffer, num, 0);

buffer[num] = '\0';
if (buffer[num-1] == '\n')
buffer[num-1] = '\0';
cout << buffer << endl;
strcpy(buffer, "");
}
} else {
cout << "Error in accepting new connection." << endl;
}
}
close(sock);
return 0;
}

在客户端:

$ telnet 127.0.0.1 1234
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Success! You are connected.
hello
>> hello
my name is pax
>> my name is pax
and you?
>> and you?
<CTRL-D>
Connection closed by foreign host.

并且,在服务器端:

$ ./testprog
Waiting for connections....
Recived new connection from 127.0.0.1
hello
my name is pax
and you?
Waiting for connections....

关于C++ 奇怪的套接字数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3455351/

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