连接一个没有监听的端口结果会怎样

标签: tcp

一句话总结:消息可达的情况下服务端直接抛个RST过来


服务端:

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

using namespace std;
int main()
{
	struct sockaddr_in serAddr;
	int listenSock, cnntSock;
	listenSock = socket(AF_INET, SOCK_STREAM, 0);
	serAddr.sin_family = AF_INET;
	serAddr.sin_addr.s_addr = INADDR_ANY;
	serAddr.sin_port = htons(1248);

	bind(listenSock, (const struct sockaddr *)&serAddr, sizeof(sockaddr_in));
	listen(listenSock, 10);

	struct sockaddr_in cliAddr;
	socklen_t len = sizeof(sockaddr_in);
	cnntSock = accept(listenSock, (struct sockaddr*)&cliAddr, &len);
	cout<< "IP:" <<inet_ntoa(cliAddr.sin_addr) <<", port:"<<cliAddr.sin_port<<endl;
	unsigned int total = 0;
	while(1)
	{
		getchar();
		cout <<"read msg..."<<endl;
		char buf[6000] = {0};
		int ret = recv(cnntSock, buf, sizeof(buf), 0);
		total += ret;
		cout << "msg:" <<endl;
		cout << buf;
		cout << "recv msg size:" << ret << "total size:" << total <<endl;
		break;
	}
	close(cnntSock);
	close(listenSock);
	return 0;
}

看结果,客户端发送syn给服务端,服务端直接回了个RST。



版权声明:本文为Nick_666原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Nick_666/article/details/78341126