Pages: [1]   Go Down
Author Topic: TCP/IP client says CONNECTION REFUSED!  (Read 1280 times)
0 Members and 1 Guest are viewing this topic.
ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 7
Posts: 1545
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


I have Arduino with a rev3 ethernet shield and I'm trying to make work a servo connected to pin 3 work ,controlling it with the TCP client written on for Android and the other server on Arduino but with port 8888 the tcp client says "198.168.1.214:8888 connection refused" , Do not know why?
Any encryption is there which I'm not able to detect and code for?

Android Code:
Code:
/*
 * Simple Android -> Arduino TCP Client
 *
 * 20.04.2012
 * by Laurid Meyer
 *
 * http://www.lauridmeyer.com
 *
 */
package com.lauridmeyer.tests;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class SimpleAndroidTCPClientActivity extends Activity {
TextView textlog;//Log for outputs

Button buttonConnect;//(dis)connect Button
SeekBar seekBar;//Seekbar to control the Servo
TextView seekBarValue;//Textfield displaing the Value of the seekbar

Boolean connected=false;//stores the connectionstatus

DataOutputStream dataOutputStream = null;//outputstream to send commands
Socket socket = null;//the socket for the connection

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
   
        //connect the view and the objects
    buttonConnect = (Button)findViewById(R.id.connect);
    textlog = (TextView)findViewById(R.id.textlog);
    seekBar = (SeekBar)findViewById(R.id.seekbar);
    seekBarValue = (TextView)findViewById(R.id.seekbarvalue);
   
    textlog.setText("Starting Client");//log that the App launched
    changeConnectionStatus(false);//change connectionstatus to "disconnected"
   
    //Eventlisteners
    buttonConnect.setOnClickListener(buttonConnectOnClickListener);
    seekBar.setOnSeekBarChangeListener(seekbarchangedListener);
    }
   
    // ----------------------- SEEKBAR EVENTLISTENER - begin ----------------------------
    SeekBar.OnSeekBarChangeListener seekbarchangedListener = new SeekBar.OnSeekBarChangeListener(){
    //Methd is fired everytime the seekbar is changed
    @Override
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    String valueOfseekbar = String.valueOf(progress);//save the value of the seekbar in a string
    seekBarValue.setText(valueOfseekbar);//update the value in the textfield
   
    if(connected){//if the socket is connected
    try {
    //send a string to the Arduino Server in the form of "set: -seekbarvalue- \n"
    dataOutputStream.writeBytes("set:"+valueOfseekbar+'\n');
    }catch (UnknownHostException e) {//catch and
    outputText(e.getMessage());//display errors
} catch (IOException e) {//catch and
outputText(e.getMessage());//display errors
}
    }
     }

     @Override
     public void onStartTrackingTouch(SeekBar seekBar) {
     }

     @Override
     public void onStopTrackingTouch(SeekBar seekBar) {
     }
    };
    // ----------------------- SEEKBAR EVENTLISTENER - end ----------------------------
   
    // ----------------------- CONNECTION BUTTON EVENTLISTENER - begin ----------------------------
    Button.OnClickListener buttonConnectOnClickListener = new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
if(!connected){//if not connected yet
outputText("connecting to Server");
try {//try to create a socket and outputstream
  socket = new Socket("192.168.1.214", 8888);//create a socket
  dataOutputStream = new DataOutputStream(socket.getOutputStream());//and stream
  outputText("successfully connected");//output the connection status
  changeConnectionStatus(true);//change the connection status
} catch (UnknownHostException e) {//catch and
  outputText(e.getMessage());//display errors
  changeConnectionStatus(false);
} catch (IOException e) {//catch and
outputText(e.getMessage());//display errors
changeConnectionStatus(false);
}
}else{
outputText("disconnecting from Server...");
try {//try to close the socket
  socket.close();
  outputText("successfully disconnected");
  changeConnectionStatus(false);//change the connection status
} catch (UnknownHostException e) {//catch and
  outputText(e.getMessage());//display errors
} catch (IOException e) {//catch and
  outputText(e.getMessage());//display errors
}
}
}};
// ----------------------- CONNECTION BUTTON EVENTLISTENER - END ----------------------------

// Method changes the connection status
public void changeConnectionStatus(Boolean isConnected) {
connected=isConnected;//change variable
seekBar.setEnabled(isConnected);//enable/disable seekbar
if(isConnected){//if connection established
buttonConnect.setText("disconnect");//change Buttontext
}else{
buttonConnect.setText("connect");//change Buttontext
}
}

//Method appends text to the textfield and adds a newline character
public void outputText(String msg) {
textlog.append(msg+"\n");
}

}

Arduino Ethernet code:

Code:
#include <SPI.h>
#include <Ethernet.h>
#include <Servo.h>

// Enter a MAC address, IP address and Portnumber for your Server below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0xE1 };
byte ip[] = {192,168,1,214};
int serverPort=8888;
int servoPin=3;

// Initialize the Ethernet server library
// with the IP address and port you want to use
Server server(serverPort);

//Initialise the Servo Library
Servo myservo;

void setup()
{
  // start the serial for debugging
  Serial.begin(9600);
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.println("Server started");//debug

  myservo.attach(servoPin);  // attache the servo on the givven servoPin
}

void loop()
{
  // listen for incoming clients
  Client client = server.available();
  if (client) {
    String commandStr ="";//Connamdstring where incoming commands are stored

    while (client.connected()) {//if a client is connected
      if (client.available()) {//and available
        //reading the inputs from the client
        char c = client.read();
        commandStr+=c;//and ands them to the commands String

        if (c == '\n') {//if a newline character is sent (commandline is fully recieved)
          Serial.println("Command:"+commandStr);//output the command
          if(commandStr.indexOf("set:")==0){//if the command begins with "set:"
            String value=commandStr;//store the command into a new String
            value.replace("set:", " "); //replace the "set:" from the command string
            Serial.println("Set a Servo:"+value);//output the servo value
            myservo.write(convertToInt(value));//set the servo to the recieved value
          }
          commandStr="";//reset the commandline String
        }
      }
    }
    // give the client time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

int convertToInt(String value){
  char buf[value.length()];
  value.toCharArray(buf,value.length());
  return atoi(buf);
}
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Switzerland
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you checked with a PC before trying the self-made Android app? Use a PC in the same net and enter the following command (command line interface, means terminal, command prompt or whatever your OS is calling it):

telnet 192.168.1.214 8888

Do you also get a "connection refused" message?
Logged

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 7
Posts: 1545
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No Pylon I haven't tested stuff as you said on the PC system itself, I will do the same and get back to you.
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 7
Posts: 1545
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
telnet 192.168.1.214 8888

tried this it says telnet is not recognized as an internal or external command, operable programme or batch file

NOTE: I have a wireless router and modem , the ethernet connection from modem goes to WiFI router's back and my laptop is connected to internet via. WiFI whereas from the same back end of the D-Link router there are various ethernet slots where the ethernet shield is connected to.
« Last Edit: September 27, 2012, 03:21:13 am by NI$HANT » Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
telnet is not recognized as an internal or external command, operable programme or batch file

Windows 7? (probably 8 too, but I don't have that) If so, you can install the telnet client, that MS thoughtfully decided you wouldn't need, by going through Control Panel/Programs and Features/Turn Windows features on or off.

We've been 'upgraded' to Windows 7 at work.
Logged

ਪੰਜਾਬ
Offline Offline
Edison Member
*
Karma: 7
Posts: 1545
Another year! Alone! :(
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a windows 7, so I should install the Telnet client? write?
Logged

"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੂਂ !

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I would. It's a useful tool to have available.
Logged

Pages: [1]   Go Up
Jump to: