C++初学者求C⼀S模式小程序

2024-12-23 06:25:00
推荐回答(4个)
回答1:

客户端
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();

回答2:

#include
#include
using namespace std;
void main()
{
string s;
cout<<"输入字符:";
cin>>s;
cout<}
用string类会使输入一串字符变的简单,建议使用。

回答3:

#include
#include
using namespace std;
void main()
{
string S;
cout<<"请输入字符:";
cin>>S;
cout<<"你输入的字符是:";
cout<}

回答4:

#include
using namespace std;

void main()
{
char p[10];
cout << "请输入" << endl;
cin >> p;
cout << "您输入的是:"< cout << p <}