使用套接字c ++ eclipse删除数组

时间:2015-12-18 15:35:50

标签: c++ arrays sockets char override

我有一个使用套接字的双项目。 它以一个名为" UdpClient"的类开头。即将消息发送到名为" UdpConnection"的类。并输出它从中获取的消息。 问题是,每次我发回一条消息,char-array" buffer"或"答案"被覆盖,我调试有问题,因为它使用了2个项目。我想问一下是否有人发现接收和发送有问题,我认为其余的都没问题。

class UdpClient {

unsigned int from_len;
int sent_bytes;
struct sockaddr_in from;
int sock;
const char * ip_address = "127.0.0.1";
int port_no;
const char* answer;
struct sockaddr_in sin;
int data_len;
char buffer[4096];
int bytes;
string input;
bool stayHere=true;

UdpClient::UdpClient(int port) {

port_no=port;
sock = socket(AF_INET, SOCK_DGRAM, 0);
do {
    answer=NULL;
    cout<< "please choose"<<endl;
    getline(cin,input);
    answer = input.c_str();
    memset(&sin, 0, sizeof(sin));   
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = inet_addr(ip_address);
    sin.sin_port = htons(port_no);
    sendto(sock, answer, data_len, 0, (struct sockaddr *)&sin, sizeof(sin));
    from_len = sizeof(struct sockaddr_in);
    bytes = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr *) 
    &from, &from_len);
    cout << buffer << endl;

}while(stayHere);
   close(sock);
}

我在项目中的第二堂课,serverUdp:

class UdpConnection {

struct sockaddr_in sin;
struct sockaddr_in from;
int sock, bytes;
char buffer[4096];
char answers[4096];
int sentBytes;
int server_port;
std::string returnStr;
char split_char;
unsigned int from_len;
bool stayHere=true;
std::vector<std::string> tokens;
userInterface starting;

UdpConnection::UdpConnection(int port) {

server_port=port;
sock = socket(AF_INET, SOCK_DGRAM, 0);
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port= htons(server_port);
bind(sock, (struct sockaddr*)&sin,sizeof(sin));
from_len = sizeof(struct sockaddr_in);
do {
    memset(&buffer, 0, sizeof(buffer)); //ADDED NOW
    bytes = recvfrom(sock, buffer, sizeof(buffer),0, 
            (struct sockaddr *)&from, &from_len);
    split_char = ' ';           //to split the message
    std::istringstream split(buffer);  
    for (std::string each; std::getline(split, each, split_char); 
        tokens.push_back(each))  {   };
    returnStr=starting.start(tokens);
    tokens.clear();
    strncpy(answers, returnStr.c_str(), sizeof(answers));
    answers[sizeof(answers) - 1] = 0;
    sentBytes=sendto(sock, answers, bytes, 0, (struct sockaddr *)&from, 
    sizeof(from));

 }while(stayHere);
  close(sock);
}

userInterface(&#34; starting.start()方法)是返回字符串的类。在它内部有一个开关盒循环,我收到了#34;令牌&#34;并将一个字符串发送到屏幕。我不会在这里添加它,因为它很长,另外还有正确的开关和TRIES来打印正确的信息。 它按预期工作,希望每次都打印正确的消息,但问题是这些消息被前一条消息所覆盖。 userInterface类代码是&#39;返回&#34;成功&#34; &#39;例如,并且没有字符串对象返回消息。

预期输出为:

successfull
successful movie
successful adding

实际输出是:

successfull
successful movie
successful addie     //the length of last message, cut ng and put e

有谁知道缓冲阵列需要清理的地方以及为什么每次都会给我带来麻烦? HELP ..

1 个答案:

答案 0 :(得分:1)

在“ UdpClient ”类中,在发送数据时,您未设置“ data_len ”。它应该如下所示:

data_len = strlen(answer);
sendto(sock, answer, data_len, 0, (struct sockaddr *)&sin, sizeof(sin));

同样在 UdpConnection 课程中,您应该使用 strlen(答案),而不是使用sizeof(答案),因为 sizeof(答案)将始终提供 4096 (答案数组的大小),而 strlen(答案)将为您提供答案中存储的字符串的确切大小。 发送时你应该使用 strlen(答案)而不是字节

answers[strlen(answers)] = 0;
sentBytes = sendto(sock, answers, strlen(answers), 0, (struct sockaddr *)&from, sizeof(from));