C++ C++;奇怪的套接字数据
嘿,伙计们,这是我的密码C++ C++;奇怪的套接字数据,c++,sockets,C++,Sockets,嘿,伙计们,这是我的密码 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
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;
}
int main(){
字符缓冲区[BUFSIZE];
//定义我们的地址结构,存储我们的端口
//以及我们的ip地址和套接字类型等。。
addrinfo中的结构sockaddr_;
addrinfo.sin_family=AF_INET;
addrinfo.sinu port=htons(端口);
addrinfo.sin\u addr.s\u addr=INADDR\u ANY;
//创建我们的套接字。
int袜子;
如果((sock=socket(addrinfo.sin_族,sock_流,0))<0){
cout问题是buffer
不保证包含以字符串结尾的空字符。在cout之前添加行buffer[BUFSIZE-1]='\0'
问题是buffer
不保证包含以字符串结尾的空字符。添加行buffer[BUFSIZE-1]='\0'
就在您的cout之前,因为recv
不为null,所以您的缓冲区被打印出来
以下代码中的重要部分是:
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;
由于recv
不为null并终止缓冲区,所以打印出来的内容都是垃圾
以下代码中的重要部分是:
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;
嗯,我理解你对空字符的看法。但它似乎不起作用。我仍然得到奇怪的字符输出。我肯定不会用四个字母的字符串溢出缓冲区变量,是吗?你需要使用recv
的返回值来存储nul字节,否则就是“hello”的recv后跟另一个“x”将给出“xello”(在这两种情况下,加上缓冲区中超过第五个字节(直到末尾)的垃圾).hmm我理解你对空字符的看法。但它似乎不起作用。我仍然得到奇怪的字符输出。我肯定不会用四个字母的字符串溢出缓冲区变量,对吗?你需要使用recv
的返回值来存储nul字节,否则就是“hello”的recv后跟另一个“x”将给出“xello”(在这两种情况下,加上缓冲区中超过第五个字节(直到末尾)的垃圾)。是,如果另一端关闭连接,则错误为-1;如果另一端关闭连接,则错误为0;如果另一端关闭连接,则错误为-1;如果另一端关闭连接,则错误为-0;如果另一端关闭连接,则错误为-1;如果另一端关闭连接,则错误为-1;如果另一端关闭连接,则错误为-0;如果另一端关闭连接,则错误为-1;如果另一端关闭连接。
$ 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....