C ++中的套接字 - 发送char *和char [] [英] Sockets in C++ - sending char* and char[]

查看:81
本文介绍了C ++中的套接字 - 发送char *和char []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有以下代码将数据发送到线程套接字,因为我在Windows上找不到一个针对异步套接字的教程显然比单独的插座线更好。



无论如何,我有几个问题;



我无法弄清楚如何连接char *和buf。我也担心它效率低下而且让我陷入困境。



2.这是处理传入数据和发送数据的最有效方法吗?这需要超快,因为它将用于生产。



3.异步套接字是否更有效,因为如果是这样我会放弃这个和深入挖掘,试图找到一个教程。



 void send(SOCKET套接字,const char *数据)
{
发送(套接字,数据,sizeof(数据),0);
}

DWORD WINAPI receive_thread(LPVOID lpParam)
{
SOCKET current_client =(SOCKET)lpParam;

char buf [100];
int res;

//如果buffer ='hello':strstr(buf,hello)

//接收消息
while(true)
{
res = recv(current_client,buf,sizeof(buf),0); //读取数据

睡眠(10);

printf(客户说:%s,buf);
睡眠(10);

char * x =(char *)你说:;
x + = buf; //在这里尝试几件事,只是不工作
strcpy_s(x,buf);
send(current_client,x);

//空缓冲区
strcpy_s(buf,);
}
}





我的尝试:



--------------------------------------- -------------------------

解决方案

您的问题和错误与套接字无关但是与基本的C知识有关。



这将发送4或8个字节,具体取决于构建(32位或64位),因为 sizeof() 指针返回指针的大小而不是任何数据(在编译期间扩展而在执行期间不处理):

 void send (SOCKET套接字,const char *数据)
{
send(套接字,数据,sizeof(数据),0);
}



这要求发送字符串,包括终止NULL字符。

 res = recv (current_client,buf,sizeof(buf),0); //读取数据
printf(客户端说:%s,buf);

为确保字符串为NULL终止,您可以使用

 buf [min( sizeof (buf) -   1 ,res)] =  0 ; 



这很可能导致崩溃:

 char * x =(char *)你说:; 
x + = buf; //在这里尝试几件事,只是不工作
strcpy_s(x,buf);
send(current_client,x);

添加指针不会添加内容并丢弃 x 指向的内存的常量导致访问冲突。您的代码是纯C而不是C ++字符串类,通常可以这样做。您必须使用另一个缓冲区来准备和发送字符串:

  char  txbuf [ 100 ]; 
strcpy_s(txbuf, 你说:);
// 要求buf为NULL终止(见上文)
strcat_s( txbuf,buf);
// 发送包含NULL字节
send(current_client,txbuf,strlen (txbuf)+ 1 0 );

或带有<$的C ++ c $ c> std :: string

 std :: string txbuf( 你说:); 
// std :: string类支持附加+ =运算符
txbuf + = buf;
// 发送包含NULL字节
send(current_client,txbuf.c_str (),txbuf.length()+ 1 0 );


Hi,

I have the following code to send data to a threaded socket, because I couldn't find a single tutorial online for async sockets in Windows which are apparently better than having separate threads for sockets.

Anyway, I have a few questions;

1. I can't figure out how to concatenate the char* and buf. I'm also worried about it being inefficient and it's gotten me into a muddle.

2. Is this the most efficient way to handle incoming data and send data? This needs to be ultra fast as it's going to be used in production.

3. Are async sockets more efficient, because if so I'll give up on this and dig deeper to try to find a tutorial.

void send(SOCKET socket, const char* data)
{
	send(socket, data, sizeof(data), 0);
}

DWORD WINAPI receive_thread(LPVOID lpParam)
{
	SOCKET current_client = (SOCKET)lpParam;

	char buf[100];
	int res;

	// if buffer = 'hello': strstr(buf, "hello")

	// receive messages
	while (true)
	{
		res = recv(current_client, buf, sizeof(buf), 0); // read data

		Sleep(10);

		printf("Client said: %s", buf);
		Sleep(10);

		char* x = (char*)"You said: ";
		x += buf; // trying several things here, just isn't working
		strcpy_s(x, buf);
		send(current_client, x);
		
		// empty buffer
		strcpy_s(buf, "");
	}
}



What I have tried:

----------------------------------------------------------------

解决方案

Your problems and errors are not socket related but related to basic C knowledge.

This will send 4 or 8 bytes depending on the build (32 or 64 bit) because sizeof() for a pointer returns the size of a pointer and not of any data (it is extended during compilation and not processed during execution):

void send(SOCKET socket, const char* data)
{
    send(socket, data, sizeof(data), 0);
}


This requires that the string is send including the terminating NULL character.

res = recv(current_client, buf, sizeof(buf), 0); // read data
printf("Client said: %s", buf);

To ensure that the string is NULL terminated you can use

buf[min(sizeof(buf) - 1, res)] = 0;


This will very probable result in a crash:

char* x = (char*)"You said: ";
x += buf; // trying several things here, just isn't working
strcpy_s(x, buf);
send(current_client, x);

Adding pointers will not add the content and casting away the constness of the memory pointed to by x results in an access violation. Your code is plain C and not a C++ string class where such is usually possible. You have to use another buffer to prepare and send the string:

char txbuf[100];
strcpy_s(txbuf, "You said: ");
// Requires that buf is NULL terminated (see above)
strcat_s(txbuf, buf);
// Send including the NULL byte
send(current_client, txbuf, strlen(txbuf) + 1, 0);

or as C++ with std::string

std::string txbuf("You said: ");
// The std::string class supports appending with the += operator
txbuf += buf;
// Send including the NULL byte
send(current_client, txbuf.c_str(), txbuf.length() + 1, 0);


这篇关于C ++中的套接字 - 发送char *和char []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆