언어 설정

Menu
Sites
Language
Setup a TCP socket connection between a host computer and Samsung Gear 3 device

Hi,

I'm building a simple application to stream some sensor data directly to the computer from the Gear 3 watch. I've connected both devices to the same wifi network. So I've implemented a server socket in the laptop and client in the watch. But the problem I'm having is, the connection cannot be established. 

I used the privilege: tizen.org/privilege/internet

But I'm not sure this is the correct one to use. And my socket client code is this (fairly simple C++ socket client code)

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Eco Sans Mono'} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Eco Sans Mono'; min-height: 14.0px} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Eco Sans Mono'; color: #4e9072} span.s1 {color: #931a68} span.s2 {color: #793d93} span.s3 {color: #0326cc} span.s4 {color: #3933ff} span.s5 {color: #000000} span.s6 {color: #006141} span.Apple-tab-span {white-space:pre}

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Eco Sans Mono'; color: #006141} p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Eco Sans Mono'} p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Eco Sans Mono'; color: #3933ff} span.s1 {color: #931a68} span.s2 {color: #000000} span.s3 {color: #006141}

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Eco Sans Mono'; color: #3933ff} span.s1 {color: #931a68} span.s2 {color: #000000}

#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>

struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hello = "Hello from client";
char buffer[1024] = {0};

            if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
            {
                elm_object_text_set(ad->txt_ppg, "Socket creation error");
                return -1;
            }

            memset(&serv_addr, '0', sizeof(serv_addr));
            serv_addr.sin_family = AF_INET;
            serv_addr.sin_port = htons(PORT);

            // Convert IPv4 and IPv6 addresses from text to binary form

            if(inet_pton(AF_INET, "192.168.43.184", &serv_addr.sin_addr)<=0)
            {
                elm_object_text_set(ad->txt_ppg, "Invalid address");
                return -1;
            }

            if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
            {
                belm_object_text_set(ad->txt_ppg, "Connection Failed");
                return -1;
            }

            send(sock , hello , strlen(hello) , 0 );

But the problem is, with 

connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr))

method, I'm always getting -1.

Can anyone suggest me a solution for this. Or, is there any method I can directly send data to the laptop?

 

p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'Eco Sans Mono'}

Edited by: Thisum Budd on 30 9월, 2018

Responses

7 댓글
Joel Ivory Johnson

The Internet Priv is definitely the one that you need. So that's not the problem. 

What operating system and technology are you using on the PC side? (ex: Windows+.Net). 

Thisum Budd

Hi Joel,

I've implemented a Java socket server on a mac  (sierra 10.12) 

I also ran the socket client code as a separate program on a different computer and established the connection, which worked fine. So I'm clueless what's going on. Also the connect() method returns just -1, So couldn't understand the exact problem.  

Joel Ivory Johnson

Sorry to disappear. I was dealing with another problem that had me reinstalling my OS several times. Things are back in a stable state where I can develop again. 

I went and blindly wrote something and then compared it to what you have.  It doesn't look that much different. On the Tizen side I have this. 

 

void  socketConnect(appdata_s* ad, const char* address)
{
	struct sockaddr_in server;
	int socket_desc = socket(AF_INET , SOCK_STREAM , 0);
	if (socket_desc == -1)
	{
		dlog_print(DLOG_DEBUG, "socketstream", "Could not create socket");
	}
	server.sin_addr.s_addr = inet_addr(address);
	server.sin_family = AF_INET;
	server.sin_port = htons( COMM_PORT );
	int connectResult ;
	if ((connectResult = connect(socket_desc , (struct sockaddr *)&server , sizeof(server))) < 0)
	{
		dlog_print(DLOG_DEBUG, "socketstream", "connect error");
		return;
	}
	else
	{
		ad->socket_desc = socket_desc;
		const char* connectedMessage = "connected to server\n\r";
		sendSensorMessage(ad, connectedMessage, strlen(connectedMessage));
	}
}

void sendSensorMessage(appdata_s* ad, const char * message, unsigned int length)
{
	int result = send(ad->socket_desc, message, length, 0);
	if(result << 0)
	{
		dlog_print(DLOG_DEBUG, "socketstream", "data failed to send");
	}
}

On the Java side I put together something that would just let me know if I got a connection.


import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;


public class SensorServer {
	public static void main(String[] args) throws IOException {
	        ServerSocket listener = new ServerSocket(6789);
	        try {
	            while (true) {
	            System.out.println("Listening...");
	                Socket socket = listener.accept();
	                System.out.println("Connect received.");
	                try {
	                System.out.println(new Date().toString());
	                BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
	                while(true) {
        				System.out.println(input.readLine());
        			}
	                    
	                } finally {
	                    socket.close();
	                }
	            }
	        }
	        finally {
	            listener.close();
	    }
	}
};

 

Running this with the emulator it runs fine. I'd suggest first making sure that communication can be achieved to your server. Try opening a browser and typing in the address and port number as something like http://192.168.43.184:1234 (replacing 1234 with your actual port number). The browser might show an error but your server should get the HTTP request to output.  If that doesn't work then it could be a firewall issue. 

Joel Ivory Johnson

Something else came to mind last night. I was working on an app that specifically needed to communicate through the WiFi connection. I had to use the connection manager API to select the profile for the WiFi network so that communication was routed that way.

Yasin Ali

Hi,

I think you may use HTTP features provided by the openSSL and Curl open source libraries.
Check this link for more details

https://developer.tizen.org/ko/development/guides/native-application/connectivity-and-wireless/internet-and-contents-downloads/curl?langredirect=1

Hope it will help.

Thisum Budd

What I need is a connection which is capable of streaming data continuously. But with HTTP, that property cannot be achieved right (If i'm not wrong)
Basically from the wear platform, It doesn't have to wait for an acknowledgement from the laptop to initialize the next request. 

Yasin Ali

 I think it should be a backgroud service app connected with main app.
 Another thing is that you can make your app thread safe implementing socket at a
 different thread. Sometimes it solves this types of problem.