Setting a socket

Hello! I want to stablish a socket between the core and my pc.
To set TCPClient I only need a host (IPAddress or name of it) and a port number, right? In order to set a local net.

IPAddress or name of the server I can access it in …etc/host (someone told me this, 127.0.0.1), is it true?
I am using IPAddress server( , , , );

Also I set a port number, any among 1023 and 65535 (e.g. 6000).

Do I need something else to set a net with the core? (Does not work)

I’m sorry if it’s a silly question but I don’t know where else to ask for some help. My knowledge for data networks are vagues (Just starting at this).

Thanks :smiley:

If you want the core to talk TO your PC (with your PC being the sever) you want to use TCPClient

If you want the core to answer incoming messages FROM your PC (with your PC being the client) you want to use TCPServer

What software are you running on your PC? What are you trying to do once you initiate this connection?

Hi @harrisonhjones, I am running it on Java. First of all I am sending one string so I expect to visualize it in Java. After I want to send several data, but once I can achieve send one single string I hope to send more data.

Ok. How is your java program setup? Can I get a gist of your code?

@harrisonhjones Sure, I only have this.

import java.net.*;
import java.io.*;
public class ServerrSocket {
	public static void main(String[] args) throws IOException{
		ServerSocket socket = new ServerSocket (6000); //The same port on Spark
		System.out.println("Connecting...");
		Socket client = socket.accept();
		System.out.println("Connected");
		
		InputStream entrada = client.getInputStream();
		System.out.printf("Dato entrante: %s",entrada);
		socket.close();
	}
}

Would you mind using this example for your java code: (from: http://www.tutorialspoint.com/java/java_networking.htm)

// File Name GreetingServer.java

import java.net.*;
import java.io.*;

public class GreetingServer extends Thread
{
    private ServerSocket serverSocket;

    public GreetingServer(int port) throws IOException
    {
        serverSocket = new ServerSocket(port);
        serverSocket.setSoTimeout(10000);
    }

    public void run()
    {
        while(true)
        {
            try
            {
                System.out.println("Waiting for client on port " +
                serverSocket.getLocalPort() + "...");
                Socket server = serverSocket.accept();
                System.out.println("Just connected to "
                + server.getRemoteSocketAddress());
                DataInputStream in =
                new DataInputStream(server.getInputStream());
                System.out.println(in.readUTF());
                DataOutputStream out =
                new DataOutputStream(server.getOutputStream());
                out.writeUTF("Thank you for connecting to "
                + server.getLocalSocketAddress() + "\nGoodbye!");
                server.close();
            }catch(SocketTimeoutException s)
            {
                System.out.println("Socket timed out!");
                break;
            }catch(IOException e)
            {
                e.printStackTrace();
                break;
            }
        }
    }
    public static void main(String [] args)
    {
        int port = 6000;
        try
        {
            Thread t = new GreetingServer(port);
            t.start();
        }catch(IOException e)
        {
            e.printStackTrace();
        }
    }
}

That should setup a Java Server on Port 6000. Run the server code. It should wait for incoming connection. Next flash your SparkCore will the following code (modify the IP for you server’s ip)

// SYNTAX
TCPClient client;
// EXAMPLE USAGE

TCPClient client;
byte server[] = { 74, 125, 224, 72 }; // PUT YOUR SERVER'S IP HERE!
void setup()
{
  // Make sure your Serial Terminal app is closed before powering your Core
  Serial.begin(9600);
  // Now open your Serial Terminal, and hit any key to continue!
  while(!Serial.available()) SPARK_WLAN_Loop();

  Serial.println("connecting...");

  if (client.connect(server, 6000))
  {
    Serial.println("connected");
    client.println("Hello from the SparkCore!");
  }
  else
  {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;) SPARK_WLAN_Loop();
  }
}

Open up the serial console on you computer and connect to your SparkCore. Press anykey and look for output.

Let me know if you have any issues.

1 Like

@harrisonhjones Awesome! :blush:
I can read my ip from Spark at Java (Such as it’s written in code), but program stays waiting for data here:

new DataInputStream(server.getInputStream());

Maybe it’s the way how I am sending data, using client.println()(I also putclient.write()and doesn’t work either) which not recognize any input.

Any idea?

I think what you posted here should be on a tutorial for first steps using TCPClient. Thanks again.

Yes, I had the same problem. I actually didn’t have a java dev setup on my machine (I do now!) to test it.

The following code works: https://gist.github.com/harrisonhjones/bf43288b8a1df2f22a9f Make sure to update the server IP.

Regards to:

I think what you posted here should be on a tutorial for first steps using TCPClient. Thanks again.

I’m working on it :slight_smile:

1 Like

Yes I also faced the same problem but I easily got the solution…I really wants to say thanks to http://www.tutorialspoint.com/java/java_networking.htm and https://intellipaat.com/tutorial/java-tutorial/ It helps me a lot to understand the networking concept using java with better explanation. Now I understand the concept of TCPClient and TCPServer with basic concepts of java…

Check this simple…Socket Programming in Java

Jovier