Recieve packets

This is a discussion on Recieve packets within the C++ Programming forums, part of the General Programming Boards category; Code: int main(void) { WORD sockVersion; WSADATA wsaData; int nret; sockVersion = MAKEWORD(1, 1); WSAStartup(sockVersion, &wsaData); LPHOSTENT hostEntry; hostEntry = ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    20

    Recieve packets

    Code:
    int main(void)
    {
    	WORD sockVersion;
    	WSADATA wsaData;
    	int nret;
    
    	sockVersion = MAKEWORD(1, 1);
    
    
    	WSAStartup(sockVersion, &wsaData);
    
    	LPHOSTENT hostEntry;
    
    	hostEntry = gethostbyname("192.168.2.101");	
    
    	if (!hostEntry) {
    		nret = WSAGetLastError();
    		printf("gethostbyname()");
    
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    	SOCKET theSocket;
    
    	theSocket = socket(AF_INET,		
    			   SOCK_STREAM,		
    			   IPPROTO_TCP);		
    
    	if (theSocket == INVALID_SOCKET) {
    		nret = WSAGetLastError();
    		printf("socket()");
    
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    
    	SOCKADDR_IN serverInfo;
    
    	serverInfo.sin_family = AF_INET;
    	serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
    	serverInfo.sin_port = htons(80);
    
    
    	nret = bind(theSocket,
    		       (LPSOCKADDR)&serverInfo,
    		       sizeof(struct sockaddr));
    
    	if (nret == SOCKET_ERROR) {
    		nret = WSAGetLastError();
    		printf("bind()");
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    ///////////////////////////////////////////////////////////////////////////
    ///////////////////////////////RECV////////////////////////////////////////
    	char *buffer = new char[256];
    
    	nret = recv(theSocket,buffer,256,0);
    
    	if (nret == SOCKET_ERROR) 
    	{
    		printf("recv()");
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    	else
    	{
    		delete [] buffer;
    	}
    ////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////RECV///////////////////////////////////////
    	closesocket(theSocket);
    	WSACleanup();
    
    	return 0;
    }

    This is what I got so far... Basically I want to recieve packets in a buffer pointer and then display them on the console screen.. Then after that I would like to feed this information into a ASM dll that has a function built into it that will decrypt the packets... You need a pointer to a buffer containing the packets in order for the function in the dll to work.


    This kinda stuff


    Code:
    0000  47 45 54 20 2F 73 65 61 72 63 68 3F 63 6C 69 65    
    0010  6E 74 3D 6E 61 76 63 6C 69 65 6E 74 2D 61 75 74    
    0020  6F 26 67 6F 6F 67 6C 65 69 70 3D 4F 3B 32 31 36    
    0030  2E 32 33 39 2E 33 39 2E 39 39 3B 32 36 36 26 
    0040  66 72 65 73 68 6E 65 73 73 5F 63 68 65 63 6B 3D    
    0050  34 63 51 64 35 44 63 49 6B 55 75 65 2D 6B 46 55    
    0060  30 49 49 37 66 26 69 71 72 6E 3D 30 53 36 42 26
    Any help would be awesome...

    THANKS

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    OK, so what is your question?

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    20
    Quote Originally Posted by Salem
    OK, so what is your question?
    heheh sorry..

    so when i use the recv function it stores the packets in "buffer"?

    How do i display them?

    cout<<buffer<<endl;?

    And to constantly record packet I just put it threw a infinite loop?

    Also,

    Code:
    	if (nret == SOCKET_ERROR) 
    	{
    		printf("recv()");
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    This if statement checker keeps activating... What am I doing wrong with the recv() function?

    Thanks

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Well, call this to get more information about a failure
    http://msdn.microsoft.com/library/de...asterror_2.asp

    > cout<<buffer<<endl;?
    Except it isn't really printable characters, you said it was encrypted.

    something like
    Code:
    for ( int i = 0 ; i < nret ; i++ ) {
      cout << (int)buffer[i];  // add formatting to suit
    }
    Also bear in mind that for TCP, sending say 100 bytes in a single send() call does NOT guarantee that some later single recv() call will receive those 100 bytes. You have to reassemble the data byte by byte, if the network chose to fragment your data.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    20
    Thanks!


    , but im getting an error saying that when I

    Code:
    recv(theSocket,buffer,256,0);
    I get this error

    Code:
    WSAENOTCONN
    10057 
    Socket is not connected. 
    A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using sendto) no address was supplied. Any other type of operation might also return this error—for example, setsockopt setting SO_KEEPALIVE if the connection has been reset.
    Im trying to connect to myself.. Doesnt bind work on a connectionless conection..?


    Code:
    bind(theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));

    Oh, when I use connect..

    Code:
    connect(theSocket, (LPSOCKADDR)&serverInfo,sizeof(struct sockaddr));
    I get this error
    Code:
    WSAECONNREFUSED
    10061 
    Connection refused. 
    No connection could be made because the target computer actively refused it. This usually results from trying to connect to a service that is inactive on the foreign host—that is, one with no server application running.
    Am I missing something? Do I have to set up another program for accepting this connect to myself??

    How do i connect to myself to read incomming packets from a target port?

    Thanks
    My Sloppy Prog
    Code:
    #include <windows.h>
    #include <winsock.h>
    #include <stdio.h>
    #include <iostream.h>
    
    
    #define NETWORK_ERROR 0
    
    int main(void)
    {
    	WORD sockVersion;
    	WSADATA wsaData;
    	int nret;
    
    	sockVersion = MAKEWORD(1, 1);
    
    
    	WSAStartup(sockVersion, &wsaData);
    
    	LPHOSTENT hostEntry;
    
    	hostEntry = gethostbyname("192.168.2.101");	
    
    	if (!hostEntry) {
    		nret = WSAGetLastError();
    		printf("gethostbyname()");
    
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    	SOCKET theSocket;
    
    	theSocket = socket(AF_INET,		
    			   SOCK_STREAM,		
    			   IPPROTO_TCP);		
    
    	if (theSocket == INVALID_SOCKET) {
    		nret = WSAGetLastError();
    		printf("socket()");
    
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    
    	SOCKADDR_IN serverInfo;
    
    	serverInfo.sin_family = AF_INET;
    	serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
    	serverInfo.sin_port = htons(80);
    
    
    	
    	nret=bind(theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
    
    
    
    	if (nret == SOCKET_ERROR) {
    		nret = WSAGetLastError();
    		cout<<"bind()"<<nret;
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    nret=	connect(theSocket,
    		       (LPSOCKADDR)&serverInfo,
    		       sizeof(struct sockaddr));
    
    	if (nret == SOCKET_ERROR) {
    		nret = WSAGetLastError();
    		cout<<"connect()"<<nret;
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    
    ///////////////////////////////////////////////////////////////////////////
    ///////////////////////////////RECV////////////////////////////////////////
    	char *buffer = new char[256];
    	
    	
    	while(1)
    	{
    	nret = recv(theSocket,buffer,256,0);
    	
    	if (nret == SOCKET_ERROR) 
    	{
    		int k = WSAGetLastError();
    		cout<<"recv()"<<k<<endl;
    		WSACleanup();
    		return NETWORK_ERROR;
    	}
    
    	else
    	{
    		for ( int i = 0 ; i <sizeof(buffer) ; i++ ) 
    		{
    		cout << (int)buffer[i];  
    	}
    
    	}
    		delete [] buffer;
    	}
    ////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////RECV///////////////////////////////////////
    	closesocket(theSocket);
    	WSACleanup();
    
    	return 0;
    }
    Last edited by valt; 02-03-2006 at 06:06 PM.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    You've only got one socket there - to do 2-way communication, you need 2 sockets - one is your server socket, which you bind() and recv() on, the other is your client socket, which you connect() with and send() on. Oh - and you need to actually wait for connections on your socket - check this tutorial: http://beej.us/guide/bgnet/ for a good intro to socket communications and the process. It's more Linux/Unix based, but since most of the calls work in Windows, and you already seem to have the Windows-specific initialization/cleanup code down, you can probably figure it out pretty easily from there.
    Last edited by Stuka; 02-03-2006 at 10:21 PM.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    with TCP doesn't the server need to listen on the socket you bind and then accept the connect making a data socket for the connection?

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    Syneris: exactly - that's why I pointed him to Beej's tutorial. It's one of the better ones out there about explaining the whole TCP client/server process from a socket-level view that I've ever seen.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    You were editing the addition information as I posted Good link too

  10. #10
    Registered User
    Join Date
    Feb 2005
    Posts
    20
    Wow thats a very very very very good tutorial... Thanks a lot! I found it hard to find one as good as that so i just was using msdn reference.

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to recieve all packets coming to machine??
    By shady_Dev in forum Networking/Device Communication
    Replies: 6
    Last Post: 03-29-2008, 10:21 AM
  2. Accessing and editing packets of other applications
    By Inder in forum Linux Programming
    Replies: 1
    Last Post: 09-01-2006, 12:00 PM
  3. text rpg problem
    By xxwerdxx in forum C Programming
    Replies: 19
    Last Post: 12-19-2005, 07:23 AM
  4. need help with my text rpg
    By xxwerdxx in forum Game Programming
    Replies: 0
    Last Post: 12-08-2005, 08:13 PM
  5. answer calls and setup to recieve data using TAP
    By mk_xyon in forum C Programming
    Replies: 3
    Last Post: 11-13-2004, 07:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21