Using Sockets to post to port

This is a discussion on Using Sockets to post to port within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I'm trying to use a simple socket connection to drop a string to local port, so that another application ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    164

    Unhappy Using Sockets to post to port

    Hi, I'm trying to use a simple socket connection to drop a string to local port, so that another application that is constantly listening to that port can grab the string.

    I used telnet to detetermine that I am opening the socket and the port is active, I can attach to the port, but when I use a send () to send my string to the port, I do not see any data come accross that port. I am a bit stumped, been searching and reading google stuff for days and had no success, can anyone point me to where I'm going wrong...

    Code:
      // Send converted line back to client.
        WORD sockVersion;
        WSADATA wsaData;
        int rVal;
    
        sockVersion = MAKEWORD(1,1);
        //start dll
        WSAStartup(sockVersion, &wsaData);
    
        //create socket
        SOCKET s = socket(AF_INET, SOCK_DGRAM, IPPROTO_TCP);
    
        if(s == INVALID_SOCKET)
        {
            AfxMessageBox("Failed socket()");
            WSACleanup();
         //   return SERVER_SOCKET_ERROR;
        }
    
        //fill in sockaddr_in struct 
    
        SOCKADDR_IN sin;
        sin.sin_family = AF_INET;
        sin.sin_port = htons(10001);
        sin.sin_addr.s_addr = INADDR_ANY;
    
    	//bind the socket
        rVal = bind(s, (LPSOCKADDR)&sin, sizeof(sin));
        if(rVal == SOCKET_ERROR)
        {
            AfxMessageBox("Failed bind()");
            WSACleanup();
         //   return SERVER_SOCKET_ERROR;
        }
    
        //get socket to listen 
        rVal = listen(s, 2);
        if(rVal == SOCKET_ERROR)
        {
            AfxMessageBox("Failed listen()");
            WSACleanup();
         //   return SERVER_SOCKET_ERROR;
        }
    
    	//allow client connections
    	//wait for a client
        SOCKET client;
    
        client = accept(s, NULL, NULL);
    	
    	Sleep (10000);
    	send(s,m_Barcode,strlen(m_Barcode),0);
    	Sleep (10000);	  
        //closesocket() closes the socket and releases the socket descriptor
    	closesocket (client);
        closesocket(s);
    	WSACleanup();

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    I highly recommend Network Programming for Microsoft Windows, Second Edition by Anthony Jones and Jim Ohmund.

    Kuphryn

  3. #3
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Ok first off you never check to see if the client socket is established, it looks like you trust everything is ok and then off you go. You should test to make sure the socket is valid before you go any further. Additionally, on the accept line you should have something to fill in the null sections...

    i.e.

    Code:
    client = accept(s, NULL, NULL);
    should be more like

    Code:
    client = accept(sock, (LPSOCKADDR)&client, &lenclient);
    where client is a sockaddr_in struct (defined in winsock.h) and lenclient is an int.

    additonally you might want something seeing how much you send to the client, such as:

    Code:
    int sent = 0, n_error = 0;
    
    sent = send(s,m_Barcode,strlen(m_Barcode),0);
    
    n_error = WSAGetLastError();
    and get the error associated with getlasterror().. that will give you some insight into your problems. Do print out the return values and such, it will save you a huge headache later. This is a quick lookover of the code, so analyze them for yourself and see what fits.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    164
    I got it working late last night, but thank you both for your replies, your both right, I am new to socket programing, and I had to get this done on an emergency basis, but the client end of things was the problem as you suggested, and thank you, it gave me some good insight on things I need to do to make the program more reliable and error checking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. serial port to poll on request
    By infineonintern in forum C++ Programming
    Replies: 2
    Last Post: 06-11-2009, 06:52 AM
  2. Problem with string and serial port
    By collinm in forum C Programming
    Replies: 2
    Last Post: 03-23-2005, 09:19 AM
  3. Sockets switching data?
    By b00l34n in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-15-2005, 12:03 PM
  4. sockets problem
    By lithium in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 06:10 PM

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