J2ME Gtalk Connection over XMPP fails - java-me

I am developing J2ME app that connect to GTalk. I am using WTK2.5.2.
Here is my code sample,
StreamConnection connection = (StreamConnection) Connector.open(
"socket://gmail.com:5222", Connector.READ_WRITE);
InputStream inStream = connection.openInputStream();
int i = inStream.read();//returns -1
It doesn't get connected on port 5222 port.(with 5222 port is open in firewall)
When i connect with 443 port, it connects well but the input stream is not read correctly. While reading the stream, first read character return -1.
Please Help Me!

Related

Node.js - Bi-directional communication between a tcp server and web portal

I've a GPS tracker device that sends data to my Node.js TCP server and I can send data back to device from the TCP server.
const net = require('net');
const port = 6565;
const host = '127.0.0.1';
const server = net.createServer(onClientConnection);
function onClientConnection(sock){
console.log(`${sock.remoteAddress}:${sock.remotePort} Connected`);
sock.on('data',function(data){
console.log(`${sock.remoteAddress}:${sock.remotePort} Says : ${data} `);
sock.write("Hello World!");
});
//Handle client connection termination.
sock.on('close',function(){
console.log(`${sock.remoteAddress}:${sock.remotePort} Terminated the connection`);
});
//Handle Client connection error.
sock.on('error',function(error){
console.error(`${sock.remoteAddress}:${sock.remotePort} Connection Error ${error}`);
});
};
server.listen(port,host,function(){
console.log(`Server started on port ${port} at ${host}`);
});
However I'm looking to extend this device/server interaction to a web portal from where I want to send/receive data from the device in real-time and I can't seem to wrap my head around how to approach this problem. Is it possible to do this? The device itself is a low-end device that don't seem to have a embedded web server. Can we create a REST API like interface between the web portal and TCP server to accomplish this task? I'm really lost. Any pointers?
Please help.

Solving Socket.IO socket undefined on connection from Arduino?

I'm trying to connect to socket.io from arduino. The service works from the browser, but if I try to connect from the arduino, when I look in the node log I created via console.log when a connection happens, the socket is undefined, but picks up the connection. Below is the protocol switching request, which returns a 404 not found, but socket.io does register a connection but doesn't define the socket.
client.print(F("GET /socket.io/1/websocket/"));
client.print(sid);
client.println(F(" HTTP/1.1"));
client.print(F("Host: "));
client.print(hostname);
client.fastrprint(F(":"));
char portBuffer[10];
itoa(port, portBuffer, 10);
client.fastrprint(portBuffer);
client.print(F("Sec-WebSocket-Key: ")); //dGhlIHNhbXBsZSBub25jZQ==
client.print(F("dGhlIHNhbXBsZSBub25jZQ=="));
client.println(F("Origin: ArduinoSocketIOClient"));
client.println(F("Sec-WebSocket-Protocol: chat, superchat"));
client.println(F("Sec-WebSocket-Version: 13"));
client.println(F("Upgrade: websocket"));
client.println(F("Connection: Upgrade\r\n"));
I was thinking maybe I formed the request incorrectly, but I can't find any information for the socket.io protocol switching request so I don't know what to change. What I have done is loosely based on what I read in web socket protocol stand.

data transfer from HTTP server to TCP server

I'm working on a project where in I've a .Net client connected to a node TCP server. This TCP server in turn needs to receive data from another node HTTP server. So, this HTTP server is receiving some data and it needs to forward it to the TCP server. And then the TCP server needs to broadcast it to all its .Net clients.
I'm stuck with the data transfer part (tried using sockets, bt not quite confident about it).
say, the HTTP server is defined as:
var httpServer = http.createServer(function(req,res){
res.writeHead(200, { 'Content-type': 'text/html' });
var parsed = url.parse(req.url, true);
output = parsed.query.id;
}).listen(http_PORT);
where the variable 'output' is contrived from a URL sent from a third server. So, I need to pass this variable 'output' to a TCP server running on some other port.
Any help would be appreciated.
Regards,
Abinash.
You can use WCF with TCP binding for this.
Http page will ping WCF service.

TCP socket receives from wrong port

I have problem with a TCP socket receiving messages with wrong destination port.
The OS is Ubuntu Linux 10.10 and kernel version is 2.6.31-11-rt, but this happens with other kernels, too. The C/C++ program with this problem does this:
A TCP server socket is listening to connections in INADDR_ANY at port 9000.
Messages are received with recv(2) by a TCP message receiver thread. Connection is not closed after reading message, but the thread continues to read from the same connection forever.
Error: also messages to other ports than 9000 are received by the TCP message receiver. For example, when a remote SFTP client connects to the PC where TCP message receiver is listening, it causes the TCP message receiver to receive also the SFTP messages. How is this EVER possible? How can the TCP ports "leak" this way? I think SFTP should use port 22, right? Then how it's possible those messages are visible in port 9000?
More info:
At the same time there's a raw socket listening on another network interface, and the interface is in promiscuous mode. Could this have an effect?
The TCP connection is not closed in between message receptions. The message listener just keeps reading data from the socket. Is this really a correct way to implement a TCP message receiver?
Has anyone seen this kind of problem? Thanks in advance.
EDIT:
Ok, here is some code. The code looks to be allright, so the main strange thing is, how a TCP socket can ever receive data sent to another port?
/// Create TCP socket and make it listen to defined port
TcpSocket::listen() {
m_listenFd = socket(AF_INET, SOCK_STREAM, 0)
...
bzero(&m_servaddr, sizeof(sockaddr_in));
m_servaddr.sin_family = AF_INET;
m_servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
m_servaddr.sin_port = htons(9000);
bind(m_listenFd, (struct sockaddr *)&m_servaddr, sizeof(sockaddr_in);
...
listen(m_listenFd, 1024);
...
m_connectFd = accept(m_listenFd, NULL, NULL);
}
/// Receive message from TCP socket.
TcpSocket::receiveMessage() {
Uint16 receivedBytes = 0;
// get the common fixed-size message header (this is an own message structure)
Uint16 numBytes = recv(m_connectFd, msgPtr + receivedBytes, sizeof(SCommonTcpMSGHeader), MSG_WAITALL);
...
receivedBytes = numBytes;
expectedMsgLength = commonMsgHeader->m_msgLength; // commonMsgHeader is mapped to received header bytes
...
// ok to get message body
numBytes = recv(m_connectFd, msgPtr + receivedBytes, expectedMsgLength - receivedBytes, MSG_WAITALL);
}
The TCP connection is not closed in between message receptions. The
message listener just keeps reading data from the socket. Is this
really a correct way to implement a TCP message receiver?
Yes but it must close the socket and exit when it receives the EOS indication (recv() returns zero).
I think it's ten to one your raw socket and TCP socket FDs are getting mixed up somewhere.
Umm... It appears that it was the raw socket after all which has received the messages. Can see from the log that it's the raw message handler printing out message reception things, not the TCP message handler. Duh...! :S Sorry. So it seems the binding of the raw socket into the other network interface doesn't work correctly. Need to fix that. Funny though that sometimes it works with SSH/SFTP, sometimes it does not.

Can node.js listen on UNIX socket?

Can node.js listen on UNIX socket? I did not find any documentation regarding this. I only saw the possibility of listening on a dedicated port.
To listen for incoming connections in node.js you want to use the net.server class.
The standard way of creating an instance of this class is with the net.createServer(...) function. Once you have an instance of this class you use the server.listen(...) function to tell the server where to actually listen.
If the first argument to listen is a number then nodejs will listen on a TCP/IP socket with that port number. However, if the first argument to listen is a string, then the server object will listen on a Unix socket at that path.
var net = require('net');
// This server listens on a Unix socket at /var/run/mysocket
var unixServer = net.createServer(function(client) {
// Do something with the client connection
});
unixServer.listen('/var/run/mysocket');
// This server listens on TCP/IP port 1234
var tcpServer = net.createServer(function(client) {
// Do something with the client connection
});
tcpServer.listen(1234);
Yes. It's in the documentation.
https://nodejs.org/api/net.html#net_server_listen_path_backlog_callback

Resources