9 Replies - 524 Views - Last Post: 28 January 2014 - 09:29 PM Rate Topic: -----

#1 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 131
  • Joined: 08-May 13

Program printing out random symbols?

Posted 19 January 2014 - 12:34 AM

I'm experimenting, making a little client/server but when it cout's the buffer, it prints out random symbols. Can someone help me?

Server
#include <iostream>
#include <windows.h>
#include <WinSock2.h>

int main()
{
    char buffer[1024];
    WSAData wsa;
    WORD DLLVer=MAKEWORD(2,1);
    WSAStartup(DLLVer,&wsa);
    SOCKADDR_IN saddr;
    saddr.sin_addr.s_addr=inet_addr("127.0.0.1");
    saddr.sin_family=AF_INET;
    saddr.sin_port=htons(5071);
    SOCKET listensock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    bind(listensock,(SOCKADDR*)&saddr,sizeof(saddr));
    listen(listensock,SOMAXCONN);
    int contsize=sizeof(saddr);
    //std::cout<<"--"<<WSAGetLastError();
    if(accept(listensock,(SOCKADDR*)&saddr,&contsize))
    {
        recv(listensock,buffer,sizeof(buffer),NULL);
        buffer[sizeof(buffer)]='\0';
        std::string temp=buffer;
        std::cout<<"Message Received: "<<temp<<"\n";
        system("pause");
    }
    std::cout<<WSAGetLastError();
    main();
}



Client
#include <iostream>
#include <windows.h>
#include <winsock2.h>
int main()
{
    WSAData wsa;
    WORD mkWd=MAKEWORD(2,1);
    WSAStartup(mkWd,&wsa);
    SOCKADDR_IN saddr;
    saddr.sin_addr.s_addr=inet_addr("127.0.0.1");
    saddr.sin_family=AF_INET;
    saddr.sin_port=htons(5071);
    SOCKET sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    connect(sock,(SOCKADDR*)&saddr,sizeof(saddr));
    send(sock,"Lol123test\0",10,NULL);
    WSACleanup();
    system("pause");
    main();
}



Is This A Good Question/Topic? 0
  • +

Replies To: Program printing out random symbols?

#2 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1643
  • View blog
  • Posts: 3,120
  • Joined: 30-May 10

Re: Program printing out random symbols?

Posted 19 January 2014 - 12:41 AM

> 29 main();
Seriously, if you want a loop, use a loop.
Infinite recursion on main is just pain and suffering waiting to happen.

> 22 recv(listensock,buffer,sizeof(buffer),NULL);
Try something like this
int len = recv(listensock,buffer,sizeof(buffer),NULL);
cout << "Got " << len << " bytes from recv" << endl;


Perhaps then you can put the \0 in the right place in the buffer (and not a buffer overflow that you're managing at the moment).

This post has been edited by Salem_c: 19 January 2014 - 12:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 131
  • Joined: 08-May 13

Re: Program printing out random symbols?

Posted 19 January 2014 - 12:46 AM

View PostSalem_c, on 19 January 2014 - 12:41 AM, said:

> 29 main();
Seriously, if you want a loop, use a loop.
Infinite recursion on main is just pain and suffering waiting to happen.

> 22 recv(listensock,buffer,sizeof(buffer),NULL);
Try something like this
int len = recv(listensock,buffer,sizeof(buffer),NULL);
cout << "Got " << len << " bytes from recv" << endl;


Perhaps then you can put the \0 in the right place in the buffer (and not a buffer overflow that you're managing at the moment).

Alright thanks, I just noticed it returned -1. You know what that means?
Was This Post Helpful? 0
  • +
  • -

#4 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: Program printing out random symbols?

Posted 19 January 2014 - 12:55 AM

Quote

I just noticed it returned -1. You know what that means?

What do the docs say?
Was This Post Helpful? 0
  • +
  • -

#5 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 131
  • Joined: 08-May 13

Re: Program printing out random symbols?

Posted 19 January 2014 - 01:06 AM

This is what confuses me, it says the socket isn't connected, but you don't use connect() when listening.

Nevermind guys I fixed it, thanks a lot for your help!
Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,279
  • Joined: 28-March 11

Re: Program printing out random symbols?

Posted 19 January 2014 - 01:08 AM

How did you fix it??? Please share for others.
Was This Post Helpful? 0
  • +
  • -

#7 suppiral  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 21-September 13

Re: Program printing out random symbols?

Posted 19 January 2014 - 01:27 AM

btw, line 29 on main looks like a memory leak.
sizeof(buffer) will return (array size)*(char size) which in your case will be 1024*1
when you write buffer[sizeof(buffer)] you are trying to access something that is not in the bounds of the array, arrays in C are zero based which means you can access buffer[0] to buffer[1023]. you cannot access or modify buffer[1024].
and in this case you don't need to since recv() will automatically put \0 in the last char it reads.

woops, I meant line 23. is it possible to edit posts in this forum?
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3549
  • View blog
  • Posts: 10,989
  • Joined: 05-May 12

Re: Program printing out random symbols?

Posted 19 January 2014 - 12:53 PM

View Postsuppiral, on 19 January 2014 - 03:27 AM, said:

and in this case you don't need to since recv() will automatically put \0 in the last char it reads.

Read the documentation more closely. Where does it say that recv() null terminates the data it receives?

Although it takes a character pointer, all the implementations I've ever used does not null terminate the incoming data. It does not assume that the data that is received is a C string.
Was This Post Helpful? 1
  • +
  • -

#9 suppiral  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 24
  • Joined: 21-September 13

Re: Program printing out random symbols?

Posted 19 January 2014 - 02:28 PM

i see, you're right, thanks. anyway, the return value will state how many bytes have been read so we can use that.
Was This Post Helpful? 0
  • +
  • -

#10 Ambitious  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 131
  • Joined: 08-May 13

Re: Program printing out random symbols?

Posted 28 January 2014 - 09:29 PM

Shit. I forgot to update on how I fixed it. I'm really sorry.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1