#include<iostream>
#include<winsock.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
#pragma comment(lib,"ws2_32.lib")
using namespace std;
void initialization();
int main() {
int send_len = 0;
int recv_len = 0;
char send_buf[100];
char recv_buf[100];
SOCKET s_server;
SOCKADDR_IN server_addr;
initialization();
server_addr.sin_family = AF_INET;
server_addr.sin_addr.S_un.S_addr = inet_addr("192.168.0.5");
server_addr.sin_port = htons(8080);
s_server = socket(AF_INET, SOCK_STREAM, 0);
if (connect(s_server, (SOCKADDR *)&server_addr, sizeof(SOCKADDR)) == SOCKET_ERROR) {
cout << "服务器连接失败!" << endl;
WSACleanup();
}
else {
cout << "服务器连接成功!" << endl;
}
while (1) {
cout << "请输入发送信息:";
std::string contents1;
std::string contents2;
std::string contents3;
contents1.assign(50, 'c');
contents2.assign(150, 'c');
contents3.assign(550,'c');
int len1 = contents1.length();
int len2 = contents2.length();
int len3 = contents3.length();
std::string base0;
base0.assign((char*)&len1, sizeof(int));
base0 += contents1;
base0.append((char*)&len2, sizeof(int));
base0 += contents2;
base0.append((char*)&len3, sizeof(int));
base0 += contents3;
std::cout << base0.length() << std::endl;
send_len = send(s_server, (const char*)base0.c_str(), base0.length(), 0);
if (send_len < 0) {
cout << "发送失败!" << endl;
break;
}
recv_len = recv(s_server, recv_buf, 100, 0);
if (recv_len < 0) {
cout << "接受失败!" << endl;
break;
}
else {
cout << "服务端信息:" << recv_buf << endl;
}
}
closesocket(s_server);
WSACleanup();
return 0;
}
void initialization() {
WORD w_req = MAKEWORD(2, 2);
WSADATA wsadata;
int err;
err = WSAStartup(w_req, &wsadata);
if (err != 0) {
cout << "初始化套接字库失败!" << endl;
}
else {
cout << "初始化套接字库成功!" << endl;
}
if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wHighVersion) != 2) {
cout << "套接字库版本号不符!" << endl;
WSACleanup();
}
else {
cout << "套接字库版本正确!" << endl;
}
}```
|