[英]C++ Error converting Char* to Int using Winsock2 buffer
I've been looking for hours trying to understand how I can convert我一直在寻找几个小时试图了解我如何转换
a ( char* to a int ) I'm getting at valread = socket( sock , buffer, 1024);
a ( char* to a int ) 我得到了valread = socket( sock , buffer, 1024);
at the end of try to read my buffer.在尝试读取我的缓冲区结束时。
I kept looking for any way to convert it properly, but I could not.我一直在寻找任何方法来正确转换它,但我做不到。 Thank you for your help, here is my code :)感谢您的帮助,这是我的代码:)
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include<winsock2.h>
#include <Windows.h>
#include<WS2tcpip.h>
#include <string.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
char const *hello = "Hello from client";
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
send(sock , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
valread = socket( sock , buffer, 1024);
printf("%s\n",buffer );
return 0;
}
If i understand your question right, you want to convert a char* to an int?如果我正确理解您的问题,您想将 char* 转换为 int 吗?
To convert a char* to an int in c++ there are usually several ways, mostly depending on the c++ standard you can use.要将 char* 转换为 c++ 中的 int,通常有几种方法,主要取决于您可以使用的 c++ 标准。 The following code snippet goes with the assumption, that your desired char* is named m_pChar
以下代码片段假设您想要的 char* 名为m_pChar
std::atoi (array to integer) from the header stdlib.h来自标头 stdlib.h 的 std::atoi(数组到整数)
int i;
i = std::atoi(m_pChar);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.