i have got spark core and Ubuntu server (192.168.164.3). I am trying to connect spark core (TCP Client) to Ubuntu server (TCP Server) by using TCP 90 port number. But they are not connecting to eachother(i have found it by using tcpdump). Can any body tell me why?
here is the core code:
#include <string.h>
char server[] = { 192, 168, 164, 3 };
TCPClient client;
int ledPin = D5;
int inputPin = D7;
int pirState = LOW;
int val = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(inputPin, INPUT);
Spark.variable("inputPin", &inputPin, INT);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 90))
{
Serial.println("connected");
client.println();
}
else
{
Serial.println("connection failed");
client.stop();
}
}
void loop()
{
val = digitalRead(inputPin);
if (val == HIGH)
{
digitalWrite (ledPin, HIGH); // then turn on the LED
}
if (pirState == LOW)
{
pirState = HIGH;
}
else
{
digitalWrite (ledPin, LOW);
delay(300);
if (pirState == HIGH)
{
pirState = LOW;
}
}
}
here is Ubuntu server code:
#include <pthread.h>
#include time.h>
#include stdio.h>
#include stdlib.h>
#include string.h>
#include unistd.h>
#include sys/types.h>
#include sys/socket.h>
#include netinet/in.h>
#define MAXLINE 4096 /* max text line length */
#define SA struct sockaddr
#define LISTENQ 1024 /* 2nd argument to listen() */
void* ClientConnect(void* Unused)
{
int listenfd, connfd, *n = (int*) Unused;
socklen_t CliLen;
struct sockaddr_in servaddr, CliAddr;
char buff[MAXLINE];
char vector[0];
time_t ticks;
strcpy(vector , "1");
listenfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(90);
bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
listen(listenfd, LISTENQ);
CliLen = sizeof(CliAddr);
while(*n)
{
connfd = accept(listenfd, (SA *) &CliAddr, &CliLen);
write(connfd, vector, strlen(vector));
close(connfd);
}
close(listenfd);
return 0;
}
int main(int argc, char **argv)
{
int i=1;
pthread_t PthreadID;
pthread_create (&PthreadID, NULL, &ClientConnect, &i);
while(i)
{
printf("print i:\n");
scanf ("%d",&i);
}
pthread_join (PthreadID, NULL);
return 0;
}```