CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2004
    Posts
    232

    Recv returns junk

    Code:
    #pragma comment(lib, "winmm.lib")
    #pragma comment(lib, "wsock32.lib")
    
    #include <windows.h>
    #include <winsock.h>
    #include <stdio.h>
    
    int err;
    
    SOCKET Server;
    SOCKET Client;
    
    bool IsServer = false;
    
    int startServer();
    DWORD WINAPI beginRecieve(LPVOID);
    
    int startServer()
    {
    	Server = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
    
    	if(Server == INVALID_SOCKET)
    	{
    		printf("ERROR: Unable to create server socket.\n");
    	}
    
    	SOCKADDR_IN sockIn;
    
    	sockIn.sin_family = AF_INET;
    	sockIn.sin_port = htons(4000);
    	sockIn.sin_addr.s_addr = inet_addr("127.0.0.1");
    
    	err = bind(Server,(LPSOCKADDR)&sockIn,sizeof(struct sockaddr));
    
    	if(err != 0)
    	{
    		printf("ERROR: Cannot bind socket to port 4001.\n");
    	}
    
    	printf("Waiting for data...\n");
    
    	return 0;
    }
    
    int main()
    {
    	WSADATA wsaData;
    
    	err = WSAStartup(MAKEWORD(1,1),&wsaData);
    
    	if(err != 0)
    	{
    		printf("ERROR: Unable to initialize Winsock.\n");
    	}
    
    	startServer();	
    
    	CreateThread(0,0,&beginRecieve,0,0,0);
    
    	do {} while(true);
    }
    
    DWORD WINAPI beginRecieve(LPVOID)
    {
    	int bytesRecieved;
    	const char *buffer;
    
    	while(true)
    	{
    		bytesRecieved = recv(Server,(char *)buffer,100,0);
    
    		printf(buffer);
    	}
    }
    It prints 's to the console. I made sure nothing was being sent to that particular port, but nothing was.
    Need help with anything related to audio programming? I can help!

  2. #2
    Join Date
    Sep 1999
    Location
    USA
    Posts
    178
    I think const char *buffer needs to be allocated before you can receive data into it.

    I think what's happening is that when you are outputing the value of buffer, it outputs uknown values due to the fact that it does not know where buffer is pointing to in memory since you did not initialize it to a null or with a valid memory location.

    Try that, see what happens

  3. #3
    Join Date
    Feb 2004
    Posts
    232
    Let me throw VirtualAlloc in there....

    works perfectly! Thanks so much for reminding me!

    Code:
    buffer = (const char *)VirtualAlloc(0,100,MEM_COMMIT,PAGE_READWRITE);
    Need help with anything related to audio programming? I can help!

  4. #4
    Join Date
    Feb 2004
    Posts
    232
    Though I still have a problem. When I recieve data, I only get three chars, but I set the limit to 100. What can I do there?
    Need help with anything related to audio programming? I can help!

  5. #5
    Join Date
    Jun 2002
    Posts
    395
    Originally posted by Ness
    Though I still have a problem. When I recieve data, I only get three chars, but I set the limit to 100. What can I do there?
    There may have only been three bytes ready to be read.

    YOu may also have a problem with this line:

    Code:
        do {} while(true);
    This puts you into a tight loop which will use 100% of available cpu.

    At worst, do this:

    Code:
        do { Sleep(1000); } while (true);
    At best use an event or whatever and do proper synchronization.

  6. #6
    Join Date
    Sep 1999
    Location
    USA
    Posts
    178
    yes, there may only be 3 bytes to be read so if your buffer is filled up with junk, it will display the junk.

    I like to use memset to clear the buffer before any recv operation when i do socket programing. Try it let us know how it goes

  7. #7
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    [Moved thread]

  8. #8
    Join Date
    Feb 2004
    Posts
    232
    i did the do while loop because I was doing some testing on the recieve function, and I didn't want the console window to close on me.
    Need help with anything related to audio programming? I can help!

  9. #9
    Join Date
    Feb 2004
    Posts
    232
    I tried using memset, it still only returned 3 chars.
    Need help with anything related to audio programming? I can help!

  10. #10
    Join Date
    Feb 2003
    Location
    Bangalore, India
    Posts
    1,354
    It has been stressed over and over again that you have to checked for the return value for error using GetLastError() to newbies. A few looks in this forum (Networking) would have revealed that. If you had checked then you'd have understood that the socket you are using is not connected. There is no call to accept(). A call to accept() would have revealed that there is no listen(). I suggest you to look at some sample code before you set to write a server. Lots of samples are available on the net.
    Even if our suggestions didn't help, please post the answer once you find it. We took the effort to help you, please return it to others.

    * While posting code sections please use CODE tags
    * Please check the codeguru FAQ and do a little search to see if your question have been answered before.
    * Like a post, Rate The Post
    * I blog: Network programming, Bible

    I do all things thru CHRIST who strengthens me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured