前两天写了篇文章:winsock微信然后我怕有人不懂,出个教程吧,这样出了问题你们也能自己解决
码字不易,点赞收藏加关注!!!
今天讲用户端(出两篇教程,骗流量卷)第一篇请看winsock微信讲解(1)
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include<WinSock2.h>
#include<bits/stdc++.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main() {
WSADATA data;
int ret = WSAStartup(MAKEWORD(2, 2), &data);//初始化
if (ret) {
cout << "初始化网络错误!" << endl;
WSACleanup();
return -1;
}
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);//建套接字
char a[100];
cout<<"你要连接的ip:";
cin>>a;
cout<<endl;
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
addr.sin_addr.S_un.S_addr = inet_addr(a);
ret = connect(sock, (sockaddr*)&addr, sizeof(addr));//绑定ip、端口,连接服务器
if (ret == -1) {
WSACleanup();
cout << "连接服务器失败" << endl;
return -1;
}
char buf[0xFFF];
while(1){
ret=recv(sock,buf,sizeof(buf),0);//接收
if (ret <= 0) {
WSACleanup();
cout << "接收服务器数据失败" << endl;
return -1;
}
cout << "服务器:" << buf << endl<<"我:";
cin>>buf;
ret=send(sock,buf,0xFFF,0);//发送
if (ret <= 0)
cout << "发送服务器数据失败" << endl;
}
WSACleanup();
}
这一个简单多了,前面的不会请看教程一。
其他的都没问题,重点讲一下
1.
char a[100];
cout<<"你要连接的ip:";
cin>>a;
cout<<endl;
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
addr.sin_addr.S_un.S_addr = inet_addr(a);
ret = connect(sock, (sockaddr*)&addr, sizeof(addr));//绑定ip、端口,连接服务器
这一个一定是服务端的ip,不是你的,但连接后服务器端也能知道你ip。端口要和服务器开的端口一模一样。
2.
ret = connect(sock, (sockaddr*)&addr, sizeof(addr));//绑定ip、端口,连接服务器
connect参数:
1.套接字
2.前面代码绑定的服务器ip、端口
3.服务器ip、端口的长度
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include<WinSock2.h>
#pragma comment(lib,"ws2_32")
#include<bits/stdc++.h>
//注释我不写了,结合前面代码理解
using namespace std;
int main() {
WSADATA data;
int ret=WSAStartup(MAKEWORD(2,2),&data);
if (ret) {
cout << "初始化网络错误!" << endl;
WSACleanup();
return -1;
}
SOCKET sock=socket(AF_INET,SOCK_STREAM,0);
if (sock == -1) {
cout << "创建套接字失败";
WSACleanup();
return -1;
}
int b;
cout<<"1.创建房间 2.加入房间\n";
cin>>b;
cout<<endl;
if(b==1){
char a[100];
cout<<"你的ip:";
cin>>a;
cout<<endl;
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
addr.sin_addr.S_un.S_addr = inet_addr(a);
ret=bind(sock,(sockaddr*)&addr, sizeof(addr));
if (ret == -1) {
cout << "绑定地址端口失败";
WSACleanup();
return -1;
}
ret=listen(sock,5);
if (ret == -1) {
cout << "监听套接字失败";
WSACleanup();
return -1;
}
sockaddr addrCli;
int len = sizeof(addrCli);
SOCKET sockCli=accept(sock,&addrCli,&len);
if (sockCli == -1) {
cout << "接收客户端连接失败";
WSACleanup();
return -1;
}
char buf[0xFFF];
cout<<"我:";
while(1){
cin>>buf;
ret=send(sockCli, buf, strlen(buf),0);
if (ret == -1) {
cout << "发送信息失败";
WSACleanup();
return -1;
}
ret=recv(sockCli,buf,0xFFF,0);
if (ret <= 0) {
cout << "接受客户端数据失败";
WSACleanup();
return -1;
}
cout<<"客户:"<<buf<<endl<<"我:";
}
}
else{
char a[100];
cout<<"你要连接的ip:";
cin>>a;
cout<<endl;
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
addr.sin_addr.S_un.S_addr = inet_addr(a);
ret = connect(sock, (sockaddr*)&addr, sizeof(addr));
if (ret == -1) {
WSACleanup();
cout << "连接服务器失败" << endl;
return -1;
}
char buf[0xFFF];
while(1){
ret=recv(sock,buf,sizeof(buf),0);
if (ret <= 0) {
WSACleanup();
cout << "接收服务器数据失败" << endl;
return -1;
}
cout << "服务器:" << buf << endl<<"我:";
cin>>buf;
ret=send(sock,buf,0xFFF,0);
if (ret <= 0)
cout << "发送服务器数据失败" << endl;
}
}
}
这个你们自己理解吧,通过前面的教程自己看看