7 Replies Latest reply on May 19, 2017 1:30 PM by Intel Corporation

    Error in Socket Programming , Edison Client and Java Server

    Goutham_Gandi

      Hi, I'm new with intel Edison and I'm trying to develop a c++ application to send data to a java server.

      Im getting a runtime error "Connection Refused".

      I searched the internet and most of the answers were about firewall ,so I turned off my firewall but the result did not change .

      Here is the code in Edison :

       

       

      #include <unistd.h>

      #include <iostream>

      #include <exception>

      #include "mraa.hpp"

      #include <string.h>

      #include <sys/types.h>

      #include <sys/socket.h>

      #include <netinet/in.h>

      #include <arpa/inet.h>

      #include <netdb.h>

       

       

      using namespace std;

       

       

      int main()

      {

       

       

       

       

        int sockfd; // socket file descriptor

        struct sockaddr_in serv_addr;

        struct hostent *server;

       

        sockfd = socket(AF_INET, SOCK_STREAM, 0); // generate file descriptor

        if (sockfd < 0)

           perror("ERROR opening socket");

       

       

        server = gethostbyname("192.168.99.1"); //the ip address (or server name) of the listening server.

        if (server == NULL) {

           fprintf(stderr,"ERROR, no such host\n");

           exit(0);

        }

       

       

        bzero((char *) &serv_addr, sizeof(serv_addr));

        serv_addr.sin_family = AF_INET;

        bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);

        serv_addr.sin_port = htons(9000);

       

       

        if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)

           perror("ERROR connecting");

        send(sockfd,"hello",5,0);

       

       

        //  Connection Done !

        return 0 ;

        }

       

       

       

        My Java Code in the server is :

       

        import java.io.*; 

        import java.net.*; 

        public class Hello { 

        public static void main(String[] args){ 

        try{ 

        ServerSocket ss=new ServerSocket(9000); 

        Socket s=ss.accept();//establishes connection  

        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

        String str = in.readLine();

        System.out.println("message= "+str); 

        ss.close(); 

        }catch(Exception e){System.out.println(e);} 

        } 

        } 

       

        And the Error I'm getting is :

        

        root@edison:~#

        root@edison:~# chmod 755 /tmp/Digital_Input;/tmp/Digital_Input ;exit

        ERROR connecting: Connection refused

        logout

       

       

        Please Help .

        Thank You .