求个c++的socket通信的程序
求个通过客户端传输用户名,旧密码,新密码到服务端,服务端调用netuser命令修改用户密码的小程序。所以求个socket的程序学习一下。输入输出是这个样子的。//Clie...
求个通过客户端传输用户名,旧密码,新密码到服务端,服务端调用net user命令修改用户密码的小程序。
所以求个socket的程序学习一下。输入输出是这个样子的。
//Client
//————————————
input username;
input oldpassword;
input newpassword;
send (username,oldpassword,newpassword)to server;
wait server response;
//————————————
//Server
//————————————
receive username,oldpassword,newpassword;
connect to ftp;
if(login in success by username oldpassword){
system("net user username newpassword");
tell client change password succeess;
}
else{
tell client old password is wrong;
}
//==============================
附自己写的批处理字数太长不让填。http://hi.baidu.com/szjonnivenbckyd/item/29a77f238655f60e73863e29 展开
所以求个socket的程序学习一下。输入输出是这个样子的。
//Client
//————————————
input username;
input oldpassword;
input newpassword;
send (username,oldpassword,newpassword)to server;
wait server response;
//————————————
//Server
//————————————
receive username,oldpassword,newpassword;
connect to ftp;
if(login in success by username oldpassword){
system("net user username newpassword");
tell client change password succeess;
}
else{
tell client old password is wrong;
}
//==============================
附自己写的批处理字数太长不让填。http://hi.baidu.com/szjonnivenbckyd/item/29a77f238655f60e73863e29 展开
1个回答
展开全部
/*………………服务器端如下……………………*/(客户端另外补充)
#include<iostream>
#include<string>
#include<winsock2.h>
#include<cstdlib>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main()
{
system("color 2");
cout<<"服务器启动.....\n";
//初始化winsock
WSADATA wsaData;
int err;
if(err=WSAStartup(MAKEWORD(2,1),&wsaData))
return 1;
if(LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=1)
{
WSACleanup();
return 1;
}
//创建监听套接字
SOCKET sclient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//绑定地址和监听套接字
sockaddr_in my_addr;//主机地址
my_addr.sin_family=AF_INET;
my_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
my_addr.sin_port=htons(8080);//指定端口
bind(sclient,(sockaddr*)&my_addr,sizeof(my_addr));
//启动监听
if(listen(sclient,5)!=0)//10代表最大连接数
cout<<"启动监听失败!\n";
//开始接收数据
sockaddr_in c_addr;//客户地址
int addr_len=sizeof(c_addr);
SOCKET sconnect=accept(sclient,(sockaddr*)&c_addr,&addr_len);//通过监听套接字创建连接
if(sconnect==INVALID_SOCKET)//INVALID_SOCKET=-1无效的套接字
{
cout<<"invalid socket\n";
return 0;
}
cout<<"链接成功...."<<endl;
while(true)
{
char buf[1024];
char buf1[1024];
int len=recv(sconnect,buf,sizeof(buf),0);
if(len>0)
cout<<"客户说:"<<buf<<endl;
cout<<endl;
cout<<"服务器说:";
cin.getline(buf1,1024);
send(sconnect,buf1,strlen(buf1) 1,0);
/*
char kehu[1024];
string quit;
cout<<"回应(Ctrl Z to exit):";
while(cin.getline(kehu,1024))
{
send(sconnect,kehu,strlen(kehu) 1,0);
cout<<"Do you want to quit(enter -999 to quit!):";
cin>>quit;
if(strcmp(quit,"Q") == 0)
break;
}*/
}
closesocket(sclient);
WSACleanup();
system("pause");
return 0;
}
#include<iostream>
#include<string>
#include<winsock2.h>
#include<cstdlib>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main()
{
system("color 2");
cout<<"服务器启动.....\n";
//初始化winsock
WSADATA wsaData;
int err;
if(err=WSAStartup(MAKEWORD(2,1),&wsaData))
return 1;
if(LOBYTE(wsaData.wVersion)!=2 || HIBYTE(wsaData.wVersion)!=1)
{
WSACleanup();
return 1;
}
//创建监听套接字
SOCKET sclient=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
//绑定地址和监听套接字
sockaddr_in my_addr;//主机地址
my_addr.sin_family=AF_INET;
my_addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
my_addr.sin_port=htons(8080);//指定端口
bind(sclient,(sockaddr*)&my_addr,sizeof(my_addr));
//启动监听
if(listen(sclient,5)!=0)//10代表最大连接数
cout<<"启动监听失败!\n";
//开始接收数据
sockaddr_in c_addr;//客户地址
int addr_len=sizeof(c_addr);
SOCKET sconnect=accept(sclient,(sockaddr*)&c_addr,&addr_len);//通过监听套接字创建连接
if(sconnect==INVALID_SOCKET)//INVALID_SOCKET=-1无效的套接字
{
cout<<"invalid socket\n";
return 0;
}
cout<<"链接成功...."<<endl;
while(true)
{
char buf[1024];
char buf1[1024];
int len=recv(sconnect,buf,sizeof(buf),0);
if(len>0)
cout<<"客户说:"<<buf<<endl;
cout<<endl;
cout<<"服务器说:";
cin.getline(buf1,1024);
send(sconnect,buf1,strlen(buf1) 1,0);
/*
char kehu[1024];
string quit;
cout<<"回应(Ctrl Z to exit):";
while(cin.getline(kehu,1024))
{
send(sconnect,kehu,strlen(kehu) 1,0);
cout<<"Do you want to quit(enter -999 to quit!):";
cin>>quit;
if(strcmp(quit,"Q") == 0)
break;
}*/
}
closesocket(sclient);
WSACleanup();
system("pause");
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询