C++初学者求C/S模式小程序
一个简单的小程序,要求在客户端输入一个“HELLO”,然后服务器返回一个“HELLO”用C++用语言编写,我用VC++6.0。谢谢!C/S模式的程序...
一个简单的小程序,要求在客户端输入一个“HELLO”,然后服务器返回一个“HELLO”用C++用语言编写,我用VC++6.0。谢谢!
C/S模式的程序 展开
C/S模式的程序 展开
4个回答
展开全部
客户端
WORD version;
WSADATA wsaData;
int rVal=0;
version = MAKEWORD(1,1);
WSAStartup(version,(LPWSADATA)&wsaData);
LPHOSTENT hostEntry;
//store information about the server
hostEntry = gethostbyname("hibbert");
if(!hostEntry)
{
sError("Failed gethostbyname()");
//WSACleanup();
return CS_ERROR;
}
//create the socket
SOCKET theSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(theSocket == SOCKET_ERROR)
{
sError("Failed socket()");
return CS_ERROR;
}
//Fill in the sockaddr_in struct
SOCKADDR_IN serverInfo;
serverInfo.sin_family = PF_INET;
serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
serverInfo.sin_port = htons(8888);
rVal=connect(theSocket,(LPSOCKADDR)&serverInfo, sizeof(serverInfo));
if(rVal==SOCKET_ERROR)
{
sError("Failed connect()");
return CS_ERROR;
}
char *buf = "HELLO\n";
rVal = send(theSocket, buf, strlen(buf), 0);
if(rVal == SOCKET_ERROR)
{
sError("Failed send()");
return CS_ERROR;
}
char buf[4];
rVal = recv(client, buf, 4, 0);
closesocket(theSocket);
cout << "closing client"<< endl;
WSACleanup();
WORD version;
WSADATA wsaData;
int rVal=0;
version = MAKEWORD(1,1);
WSAStartup(version,(LPWSADATA)&wsaData);
LPHOSTENT hostEntry;
//store information about the server
hostEntry = gethostbyname("hibbert");
if(!hostEntry)
{
sError("Failed gethostbyname()");
//WSACleanup();
return CS_ERROR;
}
//create the socket
SOCKET theSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(theSocket == SOCKET_ERROR)
{
sError("Failed socket()");
return CS_ERROR;
}
//Fill in the sockaddr_in struct
SOCKADDR_IN serverInfo;
serverInfo.sin_family = PF_INET;
serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
serverInfo.sin_port = htons(8888);
rVal=connect(theSocket,(LPSOCKADDR)&serverInfo, sizeof(serverInfo));
if(rVal==SOCKET_ERROR)
{
sError("Failed connect()");
return CS_ERROR;
}
char *buf = "HELLO\n";
rVal = send(theSocket, buf, strlen(buf), 0);
if(rVal == SOCKET_ERROR)
{
sError("Failed send()");
return CS_ERROR;
}
char buf[4];
rVal = recv(client, buf, 4, 0);
closesocket(theSocket);
cout << "closing client"<< endl;
WSACleanup();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
#include<string>
using namespace std;
void main()
{
string s;
cout<<"输入字符:";
cin>>s;
cout<<s;
}
用string类会使输入一串字符变的简单,建议使用。
#include<string>
using namespace std;
void main()
{
string s;
cout<<"输入字符:";
cin>>s;
cout<<s;
}
用string类会使输入一串字符变的简单,建议使用。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
#include<string>
using namespace std;
void main()
{
string S;
cout<<"请输入字符:";
cin>>S;
cout<<"你输入的字符是:";
cout<<S;
}
#include<string>
using namespace std;
void main()
{
string S;
cout<<"请输入字符:";
cin>>S;
cout<<"你输入的字符是:";
cout<<S;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
using namespace std;
void main()
{
char p[10];
cout << "请输入" << endl;
cin >> p;
cout << "您输入的是:"<<endl;
cout << p <<endl;
}
using namespace std;
void main()
{
char p[10];
cout << "请输入" << endl;
cin >> p;
cout << "您输入的是:"<<endl;
cout << p <<endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询