Community
cancel
Showing results for 
Search instead for 
Did you mean: 
GGand2
Novice
1,222 Views

Error in Socket Programming , Edison Client and Java Server

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

# include

# include

# include "mraa.hpp"

# include

# include

# include

# include

# include

# include

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 .

7 Replies
idata
Community Manager
60 Views

Hi Goutham_Gandi,

 

 

Thank you for contacting us. We want to try and reproduce your case. We researched and found more than one approach to send data to a Java server on the Edison. Could you please let us know what steps have you taken so far to configure the Edison, which image you're using, and which guide you're following, if any?

 

 

We'll be waiting for your response.

 

 

Regards,

 

-Sergio A

 

GGand2
Novice
60 Views

Thank you for responding .

I'm Using Edison with Arduino Expansion Board

I followed all the steps in here to set up the board : https://software.intel.com/en-us/get-started-edison-windows IoT - Get Started with the Intel® Edison Development Board on Windows* 64-bit | Intel® Software

So my IOT setup tool says : Firmware version installed: 201606061707. (Yocto image).https://software.intel.com/edison-image/latest

 

I only knew this way to send data to a java server so I tried it and I got this error , I wasn't following any guide .... I did some online research on how to send data from a c++ client to a java server . Since I did not get any examples of Edison sending data to a java client , I had to stick with that .

Thank you for all the help you are providing .

And please specify the other ways to send data to a java server .

Thank you

idata
Community Manager
60 Views

Hi Goutham_Gandi,

 

 

Thanks for your reply. We've already tried to compile the code for the Edison and running the code for the server. The C++ code compiled properly, but the code of the server threw some errors. We're wondering if these codes are sample codes or if you wrote the code. If you're using sample codes, please share the links to the sample code used. Are you able to run the Java code, how did you compile the C++ code? We're asking to make sure we're replicating the issue in a similar way to yours.

 

 

In regards to the other links we found that thought you might be using, these are two other options to consider. One of them uses the XDK and the other the Arduino IDE. These guides are not "how to send data from a C++ client to a Java server" but rather an alternative to configure a Web Server to be used by the Edison.

 

The links are the following:

 

https://software.intel.com/en-us/xdk-sample-creating-a-web-server

 

http://www.intel.com/content/www/us/en/support/boards-and-kits/000006164.html

 

 

Regards,

 

-Sergio A

 

GGand2
Novice
60 Views

I wrote the java code myself and it compiles and runs perfectly on my system . (I compiled the java code using the windows command prompt )

The c++ code was compiled in Intel System Studio IOT Edition . The compilation is successful and the program runs up to the point where " if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)" and then the error pops up saying connection refused.

idata
Community Manager
60 Views

We'll run some tests to try find out what could be the problem. We'll contact you soon.

 

 

-Sergio A

 

GGand2
Novice
60 Views

Hey ! I bought a router and connected both the edison board and my pc to it and ran the same code (of course changed the ip address) and it worked !!!!

I think my college wifi was blocking it or something like that.

Rest , the code works and i do not have a problem executing it !

 

Thanks a lot for your effort .

It was great having you help me . Thank you !

idata
Community Manager
60 Views

Hi Goutham_Gandi,

 

 

You're welcome, it's great to hear you've solved this problem and thank you for sharing your solution with the community.

 

 

Don't hesitate to contact us again if help is needed.

 

 

Regards,

 

-Sergio A

 

Reply