* SocketDemo.cpp
/*****************************************************************************************************************************
*1、加载套接字库,创建套接字(WSAStartup() / socket());
*2、绑定套接字到一个IP地址和一个端口上(bind());
*3、将套接字设置为监听模式等待连接请求;
* 4、请求到来之后,接受连接请求,返回一个新的对应于此次连接的套接字(accept());
*5、用返回的套接字和客户端进行通信(send() / recv());
*6、返回,等待另一个连接请求
* 7、关闭套接字,关闭加载的套接字库(closesocket() / WSACleanup());
*****************************************************************************************************************************/
#include <iostream>
#include <WinSock2.h>
#include <ws2tcpip.h>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
void reverse(char* p, char* q) {
if (p > q) { swap(p, q); }
char t = NULL;
for (; p < q; p++, q--) {
t = *p;
*p = *q;
*q = t;
}
}
void ntoa(char *addr, ULONG ip) {
UINT8 a, b, c, d;
char* p = addr, * q = NULL;
d = ip >> 24;
c = (ip & 0x00ffffff) >> 16;
b = (ip & 0x0000ffff) >> 8;
a = (ip & 0x000000ff);
q = p;
do {
*p = a % 10 + 48;
a /= 10;
p++;
} while (a > 0);
reverse(q, p - 1);
*p++ = '.';
q = p;
do {
*p = b % 10 + 48;
b /= 10;
p++;
} while (b > 0);
reverse(q, p - 1);
*p++ = '.';
q = p;
do {
*p = c % 10 + 48;
c /= 10;
p++;
} while (c > 0);
reverse(q, p - 1);
*p++ = '.';
q = p;
do {
*p = d % 10 + 48;
d /= 10;
p++;
} while (d > 0);
reverse(q, p - 1);
*p = '\0';
}
int main()
{
//初始化WSA
WORD sockVersion = MAKEWORD(2, 2);
WSADATA wsaData;//WSADATA结构体变量的地址值
const int port = 8090;
//int WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData);
//成功时会返回0,失败时返回非零的错误代码值
if (WSAStartup(sockVersion, &wsaData) != 0)
{
cout << "WSAStartup() error!" << endl;
return 0;
}
//创建套接字
SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (slisten == INVALID_SOCKET)
{
cout << "socket error !" << endl;
return 0;
}
//绑定IP和端口
sockaddr_in sin;//ipv4的指定方法是使用struct sockaddr_in类型的变量
sin.sin_family = AF_INET;
sin.sin_port = htons(port);//设置端口。htons将主机的unsigned short int转换为网络字节顺序
sin.sin_addr.S_un.S_addr = INADDR_ANY;//IP地址设置成INADDR_ANY,让系统自动获取本机的IP地址
//bind函数把一个地址族中的特定地址赋给scket。
if (bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
{
cout << "bind error !" << WSAGetLastError() << endl;
}
//开始监听
if (listen(slisten, SOMAXCONN) == SOCKET_ERROR)
{
cout << "listen error !" << endl;
return -1;
}
//循环接收数据
SOCKET sclient;
sockaddr_in remoteAddr;//sockaddr_in常用于socket定义和赋值,sockaddr用于函数参数
int nAddrlen = sizeof(remoteAddr);
char revData[255];
char addr[16];
while (true)
{
cout << "等待连接... 0.0.0.0:" << port << endl;
sclient = accept(slisten, (sockaddr*)&remoteAddr, &nAddrlen);
if (sclient == INVALID_SOCKET)
{
cout << "accept error !" << endl;
continue;
}
// inet_ntop(AF_INET, (sockaddr*)&remoteAddr, ipAddr, 16);
ntoa(addr, remoteAddr.sin_addr.s_addr);
cout << "接收到一个连接:" << addr << endl;
// 接收数据
int ret = recv(sclient, revData, 255, 0);
if (ret > 0 && ret < 255)
{
revData[ret] = 0x00;
cout << revData << endl;
}
// 发送数据
const char* sendData = "你好,TCP客户端!\n";
send(sclient, sendData, (int)strlen(sendData), 0);
closesocket(sclient);
}
closesocket(slisten);
WSACleanup();
system("pause");
//return 0;
}
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
telnet连接测试:
手机、电脑连同一个wifi,代理设置成电脑的ip
无线局域网适配器 WLAN:
连接特定的 DNS 后缀 . . . . . . . :
IPv6 地址 . . . . . . . . . . . . : 240e:878:d5:b560:550b:562c:730d:9764
临时 IPv6 地址. . . . . . . . . . : 240e:878:d5:b560:14d2:fbfb:711e:3b39
本地链接 IPv6 地址. . . . . . . . : fe80::a184:41cd:9359:4468%22
IPv4 地址 . . . . . . . . . . . . : 192.168.1.100
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . : fe80::1234%22
192.168.1.1
服务端接收到的数据:
等待连接... 0.0.0.0:8090
接收到一个连接:192.168.1.102
CONNECT dict.youdao.com:443 HTTP/1.1
Host: dict.youdao.com
Proxy-Connection: keep-alive
Connection: keep-alive