socket programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saee
    New Member
    • Feb 2009
    • 24

    socket programming

    hello group,
    i'm trying udp socket program. my code works fine with local host ip. i.e 127.0.0.1
    according to my knowledge, if i change the ip address n give remote host's ip, two machines should communicate. it doesn't work. after runing server i checked port engaged with netstat. it shows perticular port in use.
    please tell is there an other settings that i should look for?
    thenk you.
  • saee
    New Member
    • Feb 2009
    • 24

    #2
    i couldn't find a solution for it yet. i have searched on net also. does anybody have a idea where shall i search for the solution at least?
    thnks

    Comment

    • Andr3w
      New Member
      • Nov 2007
      • 42

      #3
      Please can you post your code so we can see what's the problem and help you if possible?

      Thanks!

      Comment

      • saee
        New Member
        • Feb 2009
        • 24

        #4
        yes sure andr3w. here is my code.

        Code:
        ///////////
        client.cpp
        ////////////////////
        
        #include <iostream>
        #include <winsock2.h> 
        
        using namespace std;
        #define SERVER_PORT 3000
        #define BUF_SIZE 4096 // block transfer size  
        #define QUEUE_SIZE 20
        #define IPAddress "127.0.0.1" // Local to the system - Loop back address
        
        //#define IPAddress "192.168.1.11"
        
        
        int main()
        {
        	WORD			wVersionRequested;
        	WSADATA			wsaData;
        	SOCKADDR_IN		server;				 //Socket address information
        	SOCKET			s;
        	int				err;
        	int				bytesSent;
        	char			buf[100] = "hi hello";
        	int				addr_len = sizeof(struct sockaddr);
        	
        	
        		
        //--- INITIALIZATION -----------------------------------
        	wVersionRequested = MAKEWORD( 1, 1 );
        	err = WSAStartup( wVersionRequested, &wsaData );
        
        	if ( err != 0 ) {
        		printf("WSAStartup error %ld", WSAGetLastError());
        		WSACleanup();
        		return false;
        	}
        //---------------------------------------------------------
        
        		
        //---- creat a server struct.--------  
        	server.sin_family = AF_INET; // address family Internet
        	server.sin_port = htons (SERVER_PORT); //Port to connect on
        	server.sin_addr.s_addr = inet_addr (IPAddress); //Target IP
        
        //--------------------------------------------------------
        
        	while(1) {
        // ---- create SOCKET--------------------------------------
        	s = socket (AF_INET, SOCK_DGRAM, 0); //Create socket
        	if (s == INVALID_SOCKET)
        	{
        		printf("socket error %ld" , WSAGetLastError() );
        		getchar();
        		WSACleanup();
        		return false; //Couldn't create the socket
        	}  
        //---------------------------------------------------------
        
        
        //---- SEND bytes -------------------------------------------
        		
        		printf("trans buf:('q' to quit)\n");
        		gets_s(buf);
        		if ((strcmp(buf , "q") == 0) || strcmp(buf , "Q") == 0)
        			break;
        		bytesSent = sendto (s, buf, QUEUE_SIZE, 0, (struct sockaddr*) &server, sizeof(sockaddr));
        		if(bytesSent < 0 ){
        			printf("sendto fails with : %ld\n ", WSAGetLastError());
        	
        			WSACleanup();
        		}
        		else 
        			printf("bytes sent :  %d\n" ,bytesSent );
        //-------------------------------------------------------------------------
        
        	}
        	return 0;
        }

        Code:
        ////////////////////////
        server.cpp
        ////////////////////////
        
        #include <iostream>
        #include <winsock2.h> 
        
        #define SERVER_PORT 3000
        //#define BUF_SIZE 4096  // block transfer size  
        #define QUEUE_SIZE 20 
        #define IPAddress "127.0.0.1" // Local to the system - Loop back address
        
        //#define IPAddress "127.0.0.85"
        
        
        int main()
        {
        	WORD		wVersionRequested;
        	WSADATA		wsaData;
        	SOCKET		s; 
        	struct		sockaddr_in server, client;  // holds IP address 
        	char		recvbuf[10] = {}; 
        	int			b, opt; 
        	int			err;
        	int			bytesRecv;//, bytesSent;
        	int			addr_len = sizeof(struct sockaddr);
        	char		*val = new char;
        
        	
        //--- INITIALIZATION -----------------------------------
        	wVersionRequested = MAKEWORD( 1, 1 );
        	err = WSAStartup( wVersionRequested, &wsaData );
        
        	if ( err != 0 ) {
        		printf("WSAStartup error %ld", WSAGetLastError());
        		WSACleanup();
        		return false;
        	}
        //---------------------------------------------------------
        
        //------------ server socket ----------------------------
        	server.sin_addr.s_addr = inet_addr(IPAddress);//htonl(INADDR_ANY);
        	server.sin_family = AF_INET; // address family Internet
        	server.sin_port = htons (SERVER_PORT); //Port to connect on
        //-----------------------------------------------------------------
        
        
        // ---- create SOCKET--------------------------------------
        	s = socket(AF_INET, SOCK_DGRAM, 0);    //0 specifies to use appropriate protocol according to request
        	if (s < 0) {
        		printf("socket error %ld",WSAGetLastError() );
        		WSACleanup();
        		return false;
        	}
        //--------------------------------------------------------
        
        	
        //---- BIND socket ----------------------------------------
        	b = bind(s, (struct sockaddr *) &server, sizeof(sockaddr)); 
        	if (b == -1) {
        		printf("socket bind error %ld",WSAGetLastError() );
        		WSACleanup();
        		getchar();
        		return false;
        	}
        
        //----------------------------------------------------------
        while (1) {
        		printf("\nwaiting for a connection\n");	
        
        	
        //-----------receive bytes -------------------------------------------------------------
        		
        		bytesRecv  = recvfrom(s, recvbuf, QUEUE_SIZE, 0, (struct sockaddr *)&client, &addr_len );
        		
        		if(bytesRecv < 0){
        			printf("\nbytesRecv : %d\n", bytesRecv);
        			printf("\nrecvfrom error : %ld\n", WSAGetLastError());
        			WSACleanup();
        		}
        		
        		printf("connection ");
        		if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
        			printf( "Closed.\n");
        			WSACleanup();
        		}
        		else printf("accepted\n");
        		printf("received from: %s \t port : %d\n ", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
        
        		printf( " Bytes Recv: %s \n ", recvbuf );
        		
        //-----------------------------------------------------------------------------------------------
         
        	
        	}
        	
        	WSACleanup();
        	return 0;
        }
        see, here in this code if i replace loopback address with other machines ip address it should work. i mean that is what i cud know till now. please tell me if there are also other settings i should check like firewalls. actually i have kept firewall off.
        thank you again.

        Comment

        • saee
          New Member
          • Feb 2009
          • 24

          #5
          same prob with tcp

          i eve tried tcp client- server. same problem! :(
          works with loop back address n not with network addresses.

          Comment

          • shreeram
            New Member
            • Mar 2009
            • 1

            #6
            socket programming

            hi..
            your LAN activated Firewall means it generate TimedOut exception
            so u check firewall settings tick On recommend radio button

            Comment

            • saee
              New Member
              • Feb 2009
              • 24

              #7
              hi shreeram,
              i tried. no luck. :(
              r u sure the code is correct?

              Comment

              • saee
                New Member
                • Feb 2009
                • 24

                #8
                help!

                can anybody please answer me?

                Comment

                • saee
                  New Member
                  • Feb 2009
                  • 24

                  #9
                  ok. let me state my problem newly.
                  my client n server can send n receive data from labview's udp port. but not to each other.as, it can communicate with labview, the code is correct. but i can not send data through this c application. n i can not figure out why.

                  Comment

                  Working...