I'm trying to connect to a web socket (Details: here ) to get information back from it.
As you can see I need clients IP address and so on, here's what I have so far:
//client IP address
//is client behind something?
String ipAddress = request.getHeader("X-FORWARDED-FOR");
if (ipAddress == null) {
ipAddress = request.getRemoteAddr();
}
String socketAddress = "whois.nic.uk";
int socketPort = 1043;
Socket socket = new Socket(socketAddress, socketPort);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.flush();
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
//for every domain loop
String singleDomain = "google.co.uk"; //just an example
//<hostname of client> <IP address of client> <domain to query><cr><lf>
String whoIs2 = ipAddress + " " + ipAddress + " " + singleDomain + "\r\n";
//convert message to bytes
byte[] whoIs2B = whoIs2.getBytes();
Basically I'm not sure how to sent the whoIs2B (byte version to the socket if that makes sense?). I then need to change the byte response to text, that's all I need.
Note: Don't mind try / catches since I had to delete some unnecessary info to make it easier to see what I'm asking.
To send the bytes down the socket you need to write to the underlying stream 'out'.
You'd do something similar for the input stream.
http://docs.oracle.com/javase/tutorial/networking/sockets
Related
I am trying to make an application to control DMX Channels. For this I have an ESP8266 which takes a String as an input like "2.255", where the first integer is the DMX Channel and the second integer the value.
For my PC I wrote a method that first builds the string and then sens it to the IP Address of the ESP8266 via a Socket.
for(DMXChannel c : list){
if(lastvalue.get(c.getChannelID() - 1) != c.getValue()){
try {
String msg = c.getChannelID() + "." + c.getValue();;
DatagramSocket clientSocket = new DatagramSocket();
InetAddress ipaddr = InetAddress.getByName(ip); //IP Address is "192.168.4.1"
byte[] sendData = new byte[1024];
sendData = msg.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipaddr, 8888);
clientSocket.send(sendPacket);
clientSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
This works fine but I have a problem when I am trying to do a fade. The frequency of this piece of code seems to be too slow.
Does anyone have a "faster" solution?
Here are some things you could do to speed up the code:
Do not call getByName for an IP address. This involves DNS, but DNS is not needed for an IP address. Instead call getByAddress. If you do need to call getByName, call it only once and cache the answer.
Do not open, close, reopen, etc. the socket each time. Just keep it open.
How can i send message from server to any specific client. I have the concept of how to do it like i have to make a list of all the clients connected to server and then by iterating each client i can send message but i will be thankful if any one can help me by code.I have searched many codes but i didn't get any considerable help from them Code shouldn't be GUI based. Thanks in advance.Sorry for my bad English.
This is my code in which message is send to all clients but i want to send message to a client of my choice using clients ipaddress
Map<Integer, java.net.Socket> clients = new HashMap<Integer, java.net.Socket> ();
socket = serverSocket.accept();
// Add the socket to a HashMap
clients.put(socket.getPort(), socket);
for (Iterator<Integer> iter = clients.keySet().iterator(); iter.hasNext(); )
{
int key = iter.next();
java.net.Socket client = clients.get(key);
// Sending the response back to the client.
// Note: Ideally you want all these in a try/catch/finally block
OutputStream os = client.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("Some message");
bw.flush();
}
What I would do is create a Client class:
class Client
{
private String userName;
private String ipAddress;
private java.net.Socket socket = null;
public Client (String userName, String ipAddress, java.net.Socket socket)
{
this.userName = userName;
this.ipAddress = ipAddress;
this.socket = socket;
}
public java.net.Socket getSocket()
{
return this.socket;
}
}
Instead of adding just the socket and port number to the map, I would map the combination of the userName and ipAddres to a Client object.
socket = serverSocket.accept();
// get the username from the socket
// get the ipAddress from the socket
Client c = new Client(userName, ipAddress, socket);
// Add the client to a HashMap
clients.put(userName + ":" + ipAddress, c);
Now, you can send a message to a specific client based on the username and ipAddress:
public void sendToOneClient (String userName, String ipAddress, Map<String, Client> clients)
{
Client c = clients.get(userName + ":" + ipAddress);
java.net.Socket socket = c.getSocket();
// Sending the response back to the client.
// Note: Ideally you want all these in a try/catch/finally block
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("Some message");
bw.flush();
}
I'd use Socket.getInetAddress() and compare the result to whatever you have the IPs you want to send to in. Personally, I'd use a String[] or ArrayList<String> for that. Here's an example:
ArrayList<String> addresses;
//TODO: Add things to 'addresses'
clients.put(socket.getPort(), socket);
for (Iterator<Integer> iter = clients.keySet().iterator(); iter.hasNext(); )
{
int key = iter.next();
java.net.Socket client = clients.get(key);
//Checking to make sure it's a client we want to send to.
if (addresses.contains(client.getInetAddress().toString()) {
// Sending the response back to the client.
// Note: Ideally you want all these in a try/catch/finally block
OutputStream os = client.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("Some message");
bw.flush();
}
}
Alternatively, you could store the sockets in your HashMap by the InetAddress instead.
You can store a relationship between how you want to look up the client with the socket that they are on. The natural way to do this is with a map, like
Map<String, Socket> sockets = new HashMap<String,Socket>();
...
ServerSocket ss = ...;
Socket s = ss.accept();
String username = getUserName(s);
sockets.put(username, s);
Obviously, in this example here, the client would have to send his/her userName in a format which you expect to receive after making the Socket connection
I have found that it is effective to create an object type that can hold both a unique name or id be it an int or a String and also a Socket. You could store instances of this object in an ArrayList (or any other list) and iterate through them searching for the name or id that you want to use.
This is what I did for my program.Here I used the ">>" string to specify that a message should be sent to a particular user. (e.g.: "Ross>>Hi Ross What's Up?" means that the message should be sent to the user named 'Ross'). I used a HashMap(named 'WritersMap') to keep details as KEY-VALUE pairs. Key will be the name of the user who sends the particular message and Value will be that message. 'in' is a BufferedReader instance.
while (true) {
String input = in.readLine();
if (input == null) //if there is no input,do nothing
{
return;
}
//when a user sends a message to a specific user
else if(input.contains(">>")) //checks whether the message contains a >>
{
String person=input.substring(0,input.indexOf(">")); //extract the name of the destination user
for(HashMap.Entry<String,PrintWriter> entry:writersMap.entrySet()) //find the destination user from the users list
{
if(entry.getKey().matches(person)) //if the destination user is found
{
PrintWriter writer=entry.getValue();
writer.println("MESSAGE " + name + ": " + input);
}
}
}
else //when a user sends a broadcast message to all users
{
for(HashMap.Entry<String,PrintWriter> entry:writersMap.entrySet()) //find the destination user from the users list
{
PrintWriter writer=entry.getValue();
writer.println("MESSAGE " + name + ": " + input);
}
}
}
I'm programming an Server-Client Software, where the client connects to the server with an port request, the server opens a new port and sends back the new port number. Then the client communicates with an RSA shared AES key encryption and the port from the port request.
Well, it should be like that.
I had the program already running, only one client could connect to the server, and everything worked fine. But now I'm sending an "portreq" String to the server which should give a port reply. But when i check, if the incoming request is a "portreq" it gives me false. If i do same with .contains it gives me true. That's the first problem, and secondly:
When the server converts the port-Integer into an String and sends it, i can't transform it into a int again on the client-side:
java.lang.NumberFormatException: For input string: "6002������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at application.PowerPanelController.addDataToCpu(PowerPanelController.java:360)
at application.PowerPanelController.lambda$0(PowerPanelController.java:143)
at application.PowerPanelController$$Lambda$200/357277047.handle(Unknown Source)
at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:226)
at com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineClipCore.java:167)
at javafx.animation.Timeline.impl_playTo(Timeline.java:176)
at javafx.animation.AnimationAccessorImpl.playTo(AnimationAccessorImpl.java:39)
at com.sun.scenario.animation.shared.InfiniteClipEnvelope.timePulse(InfiniteClipEnvelope.java:110)
at javafx.animation.Animation.impl_timePulse(Animation.java:1102)
at javafx.animation.Animation$1.lambda$timePulse$25(Animation.java:186)
at javafx.animation.Animation$1$$Lambda$186/1977464318.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at javafx.animation.Animation$1.timePulse(Animation.java:185)
at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:344)
at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:447)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:431)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$363(QuantumToolkit.java:298)
at com.sun.javafx.tk.quantum.QuantumToolkit$$Lambda$42/317723766.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Which i don't get because the String looks pretty much the same as i send it.
Here's my client side send and receive function
private String sendCommandNoAES(String command)
{
byte[] outData = new byte[1024];
byte[] inD = new byte[1024];
String message = "";
try
{
// create Socket
DatagramSocket socket = new DatagramSocket();
// build Packet
InetAddress serverIP = InetAddress.getByName(PowerPanelMain.ip);
outData = command.getBytes();
DatagramPacket out = new DatagramPacket(outData,outData.length, serverIP, PowerPanelMain.port); // send packet
socket.send(out);
logger.info("Sent an " + command + " Request non encrypted to " + PowerPanelMain.ip + ":" + PowerPanelMain.port);
// Receive answer
byte[] inData = new byte[1024];
DatagramPacket in = new DatagramPacket(inData,inData.length);
socket.receive(in);
inD = in.getData();
message = new String(inD,0,inD.length);
// Close Socket
socket.close();
logger.info("Go answer " + message + " from " + in.getAddress().toString() + ":" + in.getPort());
}
catch(Exception ee)
{
logger.error("Error while requesting data from server \n" + getStackTrace(ee));
return "offline";
}
return message;
}
This is where i send the portreq:
String portString = sendCommandNoAES("portreq");
int port = Integer.parseInt(portString);
Thats the server code:
byte[] inData = new byte[1024];
byte[] outData = new byte[1024];
String message;
DatagramSocket socket = null;
try
{
socket = new DatagramSocket(port);
System.out.println("Bound to " + port);
//JOptionPane.showMessageDialog(null, "Bound to " + String.valueOf(port), "Yeay", JOptionPane.OK_CANCEL_OPTION);
}
catch(Exception ee)
{
JOptionPane.showMessageDialog(null, "Error occured! \n#002", "Error #002", JOptionPane.OK_CANCEL_OPTION);
}
...
DatagramPacket in = new DatagramPacket(inData,inData.length);
socket.receive(in);
InetAddress senderIP = in.getAddress();
int senderPort = in.getPort();
byte[] inc = in.getData();
message = new String(inc,0,inc.length);
System.out.println("Got " + message + " from " + senderIP + ":" + senderPort + " (byte array: " + inc.toString());
...
if(message.contains("portreq"))
{
System.out.println("Creatinfg answer");
outData = String.valueOf(portcount).getBytes();
DatagramPacket out = new DatagramPacket(outData,outData.length, senderIP, senderPort);
socket.send(out);
System.out.println("Sent " + String.valueOf(portcount));
UDPTraffic udpt = new UDPTraffic(portcount, this.mode);
udpt.start();
portcount++;
}
I'm really thankful to anyone who can solve/explain me why this is happening :D
inD = in.getData();
This merely reasserts the same value. Remove.
message = new String(inD,0,inD.length);
Wrong. You're ignoring the actual length of the received datagram. Change to:
message = new String(in.getData(), in.getOffset(), in.getLength());
You are sending a string of one size, yet on the other side you are reading the string of 1024 bytes - which you never sent. This will not do. You should either serialize string using serialize() method, or, if you want to send raw string bytes, send string length as a separate part of the message.
My server creates a multicast socket which listens for UDP packets. It is receiving packets sent to the broadcast address of the network but I can't get the ip address of the sender:
multisocket.getInetAddress().getHostAddress();
returns
"::"
(I guess its because of null getInetAddress).
How can I get the IP address of the sender?
TIPS: I guess it has to do with the socket not being bound and basically the whole broadcasting because those packets arent sent exclusively to me but to the whole network, but shouldnt they hold the IP address of the sender? Enlighten me please.
Here is the code:
public void run() {
try {
Thread.sleep(5000);
Log.i("SERVERUDP", "Connecting...");
MulticastSocket multisocket = new MulticastSocket(SERVERPORT);
multisocket.setBroadcast(true);
Log.i("SERVERUDP","Server's IP is: " + multisocket.getLocalAddress().getHostAddress());
getLocalIpAddress();
while(true){
byte[] b = new byte[65535];
ByteArrayInputStream b_in = new ByteArrayInputStream(b);
DatagramPacket dgram = new DatagramPacket(b, b.length);
multisocket.receive(dgram); // blocks
ObjectInputStream o_in = new ObjectInputStream(b_in);
Object o = o_in.readObject();
dgram.setLength(b.length);
b_in.reset();
if(o.getClass().getSimpleName().equalsIgnoreCase("Request")){
Request request = (Request)o;
String inetaddress = multisocket.getInetAddress().getHostAddress();
Log.i("SERVERUDP-if", "Sending request to IP: " + inetaddress);
new Thread(new ClientTCP(inetaddress, createRequestFromBroadcast(request))).start();
}else if(o.getClass().getSimpleName().equalsIgnoreCase("String")){
Log.e("SERVERUDP-elseif-string", "WTF received a string: " + (String)o);
}else{
Log.e("SERVERUDP-else", "Unrecognized object of type: " + o.getClass().getSimpleName());
}
o_in.close();
//iteration done only once for testing!
break;
}
multisocket.close();
} catch (Exception e) {
Log.e("SERVERUDP", "Error", e);
}
}
Each packet that you receive might have a different source address. So I'm not sure why you're trying to look at multisocket to figure out the source address.
I have to admit I haven't tried this, but does dgram.getSocketAddress () give you what you want after the multisocket.receive call returns?
Morning.
I'm pretty new in Java and socket connections but I'm trying to send out a UDP packet/broadcast on 255.255.255.255 on port 8001 to a device. I can get the data to send just fine, however when it comes time to receive the data the connection times out. I have a packet sniffer and I can see the packet send and then the device respond.
I'm pretty sure it is a rookie mistake that I'm missing in my code but I've been stuck on it for awhile and any help would be appreciated.
m_Socket = new DatagramSocket(m_SERVERPORT);
InetAddress address = InetAddress.getByName(m_SERVERIP);
m_DataPack = new DatagramPacket(m_SERVERCMD.getBytes(), m_SERVERCMD.getBytes().length,
address, m_SERVERPORT);
m_Socket.setBroadcast(true);
m_Socket.connect(address, m_SERVERPORT);
m_Socket.send(m_DataPack);
m_DataPack = new DatagramPacket(data, data.length,
address, m_SERVERPORT);
m_Socket.receive(m_DataPack); // This is where it times out
data = m_DataPack.getData();
String received = data.toString();
System.out.println("Received: " + received);
m_Socket.close();
Thanks and Gig'Em.
EDIT:
I'm not sure if this helps but when I watch the m_Socket object I can see the following right before it sends:
bound = true;
close = false;
connectedAddress = Inet4Address (id = 32) (-1,-1,-1,-1);
connectedPort = 8001;
connectState = 1;
created = true;
impl = PlainDatagramSocketImpl;
oldImpl = false;
and the m_DataPack object is the following:
address = Inet4Address (id = 32) (-1,-1,-1,-1);
bufLength = 6 (size of packet I'm sending is 6 char long);
offset = 0;
port = 8001;
This doesn't make sense. You are broadcasting, which is 1-to-many, and you are also connecting, which is 1-to-1. Which is it?
Lose the connect. And lose the 255.255.255.255. This has been heavily deprecated for about 20 years. Use a subnet-local broadcast address, e.g. 192.168.1.255.
You can also take a look at MulticastSocket described at Broadcasting to Multiple Recipients. Hope this helps.
If you want to receive a datagram you need to bind() to the local endpoint (address + port).
You are sending and receiving the packet on same device. You could separate send and receive ports (e.g send on 8001, receive on 8002). And run send and receive codes as separate threads. If both device must find each other (or one device find itself).
import java.io.IOException;
import java.net.*;
Receiving:
private DatagramSocket getReceiveSocket() throws UnknownHostException, SocketException {
if (receiveSocket == null) {
receiveSocket = new DatagramSocket(8002, InetAddress.getByName("0.0.0.0")); // 0.0.0.0 for listen to all ips
receiveSocket.setBroadcast(true);
}
return receiveSocket;
}
public void receive() throws IOException {
// Discovery request command
byte[] buffer = "__DISCOVERY_REQUEST__".getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
getReceiveSocket().receive(packet);
System.out.println("Discovery package received! -> " + packet.getAddress() + ":" + packet.getPort());
// Get received data
String data = new String(packet.getData()).trim();
if (data.equals("__DISCOVERY_REQUEST__")) { // validate command
// Send response
byte[] response = new byte["__DISCOVERY_RESPONSE".length()];
DatagramPacket responsePacket = new DatagramPacket(response, response.length, packet.getAddress(), packet.getPort());
getReceiveSocket().send(responsePacket);
System.out.println("Response sent to: " + packet.getAddress() + ":" + packet.getPort());
} else {
System.err.println("Error, not valid package!" + packet.getAddress() + ":" + packet.getPort());
}
}
Sending:
private DatagramSocket getSendSocket() throws UnknownHostException, SocketException {
if (sendSocket == null) {
sendSocket = new DatagramSocket(8001, InetAddress.getLocalHost());
sendSocket.setBroadcast(true);
}
return sendSocket;
}
public void send() throws IOException {
// Discovery request command
byte[] data = "__DISCOVERY_REQUEST__".getBytes();
DatagramPacket packet = new DatagramPacket(data, data.length, InetAddress.getByName("255.255.255.255"), 8002);
getSendSocket().send(packet);
System.out.println("Discovery package sent!" + packet.getAddress() + ":" + packet.getPort());
// Discovery response command
byte[] response = new byte["__DISCOVERY_RESPONSE__".length()];
DatagramPacket responsePacket = new DatagramPacket(response, response.length);
getSendSocket().receive(responsePacket);
System.out.println("Discovery response received!" + responsePacket.getAddress() + ":" + responsePacket.getPort());
String responseData = new String(responsePacket.getData()).trim();
if (responseData.equals("__DISCOVERY_RESPONSE__")) { // Check valid command
System.out.println("Found buddy!" + responsePacket.getAddress() + ":" + responsePacket.getPort());
}
}
Of course should put this code in a loop in a thread.
Based on this example: https://demey.io/network-discovery-using-udp-broadcast/
UPDATE
The link above is dead. But same method described here also: https://www.baeldung.com/java-broadcast-multicast