I want a Node JS server to communicate with a UDP or TCP client.
My questions:
TCP and UDP sockets are not language-dependant, if you have an UDP server in NodeJS, and an UDP Client in Java, this should work out of the box.
So you must be doing something wrong in your NodeJS or Java program. Here's a simple Hello world program :
Server.js :
var dgram = require("dgram");
var server = dgram.createSocket("udp4");
server.on("message", function (msg, rinfo) {
console.log("server got: " + msg + " from " +
rinfo.address + ":" + rinfo.port);
});
server.on("listening", function () {
var address = server.address();
console.log("server listening " +
address.address + ":" + address.port);
});
server.bind(41234);
// server listening 0.0.0.0:41234
and UDPClient.java :
import java.io.*;
import java.net.*;
class UDPClient
{
public static void main(String args[]) throws Exception
{
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
String sentence = "Hello World!";
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 41234);
clientSocket.send(sendPacket);
clientSocket.close();
}
}
i'm sory if my question is bad, i mean i want to implement socket with nodejs like UDP and TCP , i want to create socket client using another programing.
You have to choose between UDP or TCP. Each protocol has it's advantages/disadvantages. I really like the two images on that site.
I would suggest you to use TCP, especially when you don't have a lot experience implementing UDP. Furthermore I would suggest you to use HTTP(on top of TCP) because that is also wildly available in any programming language. I think a pretty easy solution would be to define a REST API. You can easily create server-side using express.js on the node side and superagent as the client-side(interact with other server implementation). On java you can for example use atmosphere-spade-server and async-http-client
what are the steps or suggestion when i want to create socket client with another language , but the server is using nodejs?
I suggest using JSON for data interchange. You can find a solid implementation in any language easily and the users browsers also has support for it.
There shouldnt be any problem in connecting two different programs over TCP or UDP. and in fact one of the basic purpose is to address such scenarios.
Try to write your node js server with spec in here. it should work. If still it doesnt then post your java code as well. We can dig it thru.