我回来啦,看之前先点个赞吧!
目录
简介
这几天捣鼓winsock网络编程,随便写了一个winsock微信小程序,不过水平不高,没有多线程,就只能双人聊天,还只能轮流说话,请见谅!
代码
直接上代码,在DEV-C++,Visual等等编译器上运行,系统:windows!!!
服务器
#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;
}
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<<"我:";
}
}
客户端
#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();
}
升级:建立、加入房间(还是二人版😅)
#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;
}
}
}
累死了
其他
如何写IP地址?
windows+R键
1.输入cmd,点击确定 2.输入 ,ipconfig,回车 3.找“无线局域网适配器 WLAN:” 4.找“ IPv4 地址 . . . . . . . . . . . . : ***.***.*.*”例“ IPv4 地址 . . . . . . . . . . . . : 192.168.1.1” 5.196.168.1.1就是IP地址
报错咋回事?
E:collect2.exe [Error] ld returned 1 exit status
dev-c++方案
1.上方找“工具”
2.点击找“编译选项”
3.点击
4.在下面写有一个框写着“-static-libgcc”
5.改成“-static-libgcc -lwsock32”
ok!!
最后……
码字不易,点赞收藏加关注!💕💕💕