c++ string类基本用法

下面这个博文很好

https://www.cnblogs.com/X-Do-Better/p/8628492.html

string结合socket客户端

#define _CRT_SECURE_NO_WARNINGS /* VS2013,2015需要这一行 */
#include <stdio.h>
#include <string>
#include <iostream>

#include "osapi/osapi.h"
using namespace std;
int main()
{
	// 打开Socket
	OS_TcpSocket client_sock;
	client_sock.Open();

	// 连接服务器
	OS_SockAddr serv_addr("127.0.0.1", 9555);
	if(	client_sock.Connect( serv_addr ) < 0)
	{
		printf("无法连接服务器!\n");
		return -1;
	}

	char buf[1024];
	
		// 发送请求
	strcpy(buf, "help me");
	//strcpy(str, "help me");
	int n = strlen(buf);
	client_sock.Send(buf, n);

	while(1)
	{

		// 发送请求2
		strcpy(buf, "give data to me");
		n = strlen(buf);
		client_sock.Send(buf, n);


		char buf1[1024];


		// 接受应答
		n = client_sock.Recv(buf1, sizeof(buf));
		buf1[n] = 0;
		string str(buf1);
		cout<<str<<endl;
		//printf("Got: %s \n", str);
		cout<<"str's size is "<<str.size()<<endl; //没有毛病
		cout<<"str's length is "<<str.length()<<endl;//没有毛病
	}

	// 关闭Socket
	client_sock.Close();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42053726/article/details/88749798