I am having a simple Peer to Peer program written in java. I have the following code.
import java.net.*;
import java.io.*;
public class Broadcasts {
private final Runnable receiver;
private final Runnable sender;
private boolean run = true;
public Broadcasts(UDPSocketHandler parent) throws UnknownHostException{
InetAddress aHost = InetAddress.getLocalHost();
sender = new Runnable() {
public void run() {
byte data[] = "Hello".getBytes();
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
} catch (SocketException ex) {
ex.printStackTrace();
parent.quit();
}
DatagramPacket packet = new DatagramPacket(
data,
data.length,
aHost,
9090);
while (run) {
try {
System.out.println("what is sent"+new String(packet.getData()));
socket.send(packet);
Thread.sleep(1000);
} catch (IOException ex) {
ex.printStackTrace();
parent.quit();
} catch (InterruptedException ex) {
ex.printStackTrace();
parent.quit();
}
}
}
};
receiver = new Runnable() {
public void run() {
byte data[] = new byte[0];
DatagramSocket socket = null;
try {
socket = new DatagramSocket(9090);
} catch (SocketException ex) {
ex.printStackTrace();
//parent.quit();
}
DatagramPacket packet = new DatagramPacket(data, data.length);
//System.out.println("this is what has been received"+packet.getData());
String temp;
// while (run) {
try {
socket.receive(packet);
System.out.println("this is what has been received"+packet.getData());
//System.out.println("Message received ..."+ temp);
} catch (IOException ex) {
ex.printStackTrace();
parent.quit();
}
//}
}
};
new Thread(sender).start();
new Thread(receiver).start();
}
public void quit() {
run = false;
}
}
Then I have the following class to handle my communication
public class UDPSocketHandler {
private final Broadcasts broadcasts;
// continue running application?
private boolean run = true;
public UDPSocketHandler() throws UnknownHostException
{
// start socket server to accept incoming connections
new Thread().start();
// late initialize of UDP broadcast and receive, to ensure needed
// objects are instantiated
broadcasts = new Broadcasts(this);
}
// global quit method shuts down everything and exits
public void quit() {
run = false;
broadcasts.quit();
System.exit(0);
}
// application entry
public static void main(String[] args) throws UnknownHostException{
new UDPSocketHandler();
}
}
The problem is that the receiver do not receive anything. From what I understood, we could run the sender and receive on the same program as shown in this question. That is actually what I would like to do but using UDP instead of TCP. What is the problem with my code?
After some efforts and hours I have finally managed to to get my program working. I have the following:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.*;
public class SocketTest {
private boolean run = true;
public static void main(String[] args) throws IOException {
startServer();
startSender();
}
public static void startSender() throws UnknownHostException{
InetAddress aHost = InetAddress.getLocalHost();
(new Thread() {
#Override
public void run() {
byte data[] = "Hello".getBytes();
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
socket.setBroadcast(true);
} catch (SocketException ex) {
ex.printStackTrace();
//parent.quit();
}
DatagramPacket packet = new DatagramPacket(
data,
data.length,
aHost,
9090);
int i=0;
while (i<10) {
try {
System.out.println("what us mmmm.."+new String(packet.getData()));
socket.send(packet);
Thread.sleep(50);
i++;
System.out.println(i);
} catch (IOException ex) {
ex.printStackTrace();
// parent.quit();
} catch (InterruptedException ex) {
ex.printStackTrace();
// parent.quit();
}
}
}}).start();
}
public static void startServer() {
(new Thread() {
#Override
public void run() {
//byte data[] = new byte[0];
DatagramSocket socket = null;
try {
socket = new DatagramSocket(9090);
//socket.setBroadcast(true);;
} catch (SocketException ex) {
ex.printStackTrace();
//parent.quit();
}
DatagramPacket packet = new DatagramPacket(new byte[1024], 1024);
//System.out.println("this is what has been received111"+packet.getData());
String temp;
while (true) {
try {
socket.receive(packet);
temp=new String(packet.getData());
System.out.println("this is what has been received"+temp);
//System.out.println("Message received ..."+ temp);
} catch (IOException ex) {
ex.printStackTrace();
//parent.quit();
}
}
}
}).start();
}
}
With this code, each node can act as both client and server. All we have to do is to pass the correct port number of a server to which we are sending the request. In this sample code, the port number is the same as the client sending the packet to itself. I hope this could help others
Related
I'm trying to send Message object from Server module to Client module. Unfortunately it throw java.io.StreamCorruptedException: invalid type code: 64 error. Does anyone know what is the problem with code below?
Client class from Server module
private boolean needToRun;
public final Socket socket;
private OutputStream outputStream;
private InputStream inputStream;
private ObjectOutputStream objOut;
private ObjectInputStream objIn;
public Client(Socket socket) {
this.needToRun = true;
this.socket = socket;
try {
this.outputStream = socket.getOutputStream();
this.inputStream = socket.getInputStream();
this.objOut = new ObjectOutputStream(outputStream);
this.objIn = new ObjectInputStream(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
run();
}
public void send(Message msg) {
try {
objOut.writeObject(msg);
objOut.flush();
objOut.reset();
} catch (Exception ex){
ex.printStackTrace();
}
}
public void close() {
needToRun = false;
}
public void run() {
new Thread(() -> {
while(needToRun) {
try {
int amount = inputStream.available();
if (amount != 0) {
Message msg = (Message) objIn.readObject();
receivedContent(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
public void receivedContent(Message msg) {
for (Client connection : Server.clients) {
connection.send(msg);
}
}
the Client class from Client module looks the same but receivedContent looks like this:
public void receivedContent(Message msg) {
String errorName = msg.content().trim();
MainPane.addLabel(errorName);
}
and last one Server class, which accepts socket connections:
while (true) {
System.out.println("running");
try {
System.out.println("Waiting for client...");
Socket socket = serverSocket.accept();
clients.add(new Client(socket));
} catch (Exception e) {
if (!serverSocket.isClosed()) {
stopServer();
}
break;
}
}
What i'm trying to do is group 2 clients and make them communicate with eachother. So if 2 clients are connected they would only be able to communicate with eachother and if a third client got connected it would not be able to communicate with the 2 other clients but it would create another group of 2 clients and so on... Right now if a client sends a message it send it over to all clients but i'm not sure how to make them communicate in groups of 2 like in a peer-to-peer connection.
class Server {
private static DatagramSocket socket = null;
private static Map<Session, Integer> sessions = new HashMap<Session, Integer>();
private static Session session = new Session();
public static void main(String[] args) {
try {
socket = new DatagramSocket(6066);
} catch (SocketException e) {
System.out.println("[SERVER] Unable to launch server on port: " + socket.getLocalPort());
}
System.out.println("[SERVER] Server launched successfully on port " + socket.getLocalPort());
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
Arrays.fill(buffer, (byte) 0);
try {
socket.receive(packet);
} catch (IOException e) {
System.out.println("[SERVER] Unable to receive packets from buffer");
}
InetAddress ip = packet.getAddress();
int port = packet.getPort();
String data = new String(packet.getData()).trim();
if(session.getIp1() == null) {
session.setIp1(ip);
session.setPort1(port);
session.setData1(data);
} else {
session.setIp2(ip);
session.setPort2(port);
session.setData2(data);
}
DatagramPacket pt = new DatagramPacket(packet.getData(), packet.getData().length, ip, port);
try {
socket.send(pt);
} catch (IOException e) {
System.out.println("[SERVER] Unable to send packets to client.");
}
}
}
});
thread.start();
}
}
Client:
public class Client {
private static DatagramSocket socket = null;
public static void main(String[] args) {
System.out.println("Send to server:");
Scanner scanner = new Scanner(System.in);
while (true) {
try {
// port shoudn't be the same as in TCP but the port in the datagram packet must
// be the same!
socket = new DatagramSocket();
} catch (SocketException e1) {
System.out.println("[CLIENT] Unable to initiate DatagramSocket");
}
InetAddress ip = null;
try {
ip = InetAddress.getByName("127.0.0.1");
} catch (UnknownHostException e) {
System.out.println("[CLIENT] Unable to determine server IP");
}
// must be in a while loop so we can continuously send messages to server
String message = null;
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
receive();
}
});
thread.start();
while (scanner.hasNextLine()) {
message = scanner.nextLine();
byte[] buffer = message.getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, ip, 6066);
try {
socket.send(packet);
} catch (IOException e) {
System.out.println("[CLIENT] Unable to send packet to server");
}
}
}
}
private static void receive() {
// receiving from server
byte[] buffer2 = new byte[100];
DatagramPacket ps = new DatagramPacket(buffer2, buffer2.length);
while (true) {
try {
socket.receive(ps);
} catch (IOException e) {
System.out.println("[CLIENT] Unable to receive packets from server.");
}
System.out.println("[SERVER] " + new String(ps.getData()));
}
}
}
Object class:
public class Session {
private InetAddress ip1;
private int port1;
private String data1;
private InetAddress ip2;
private int port2;
private String data2;
public Session() {
}
public InetAddress getIp1() {
return ip1;
}
public void setIp1(InetAddress ip1) {
this.ip1 = ip1;
}
public int getPort1() {
return port1;
}
public void setPort1(int port1) {
this.port1 = port1;
}
public String getData1() {
return data1;
}
public void setData1(String data1) {
this.data1 = data1;
}
public InetAddress getIp2() {
return ip2;
}
public void setIp2(InetAddress ip2) {
this.ip2 = ip2;
}
public int getPort2() {
return port2;
}
public void setPort2(int port2) {
this.port2 = port2;
}
public String getData2() {
return data2;
}
public void setData2(String data2) {
this.data2 = data2;
}
}
Currently, you store the client's information in an array. Make an object where it will contain two client session's data. When a new client is attempting to connect, see if there are any objects that have a free spot, if not, create a new object and await a new participant; otherwise, join an existing session.
Hackish way: Create a Map<ObjectHere, UserCount> then filter based on userCounts = 1 and then join the session to the first returned Object.
The program is intended to have multiple clients connect to a single server and the clients are able to send and receive messages among other clients.
For example if Client A says "Hi", Client B and Client C connected to the server would also receive "Hi".
In my current code, the server only receives the messages sent by the clients.
I'm currently looking for a solution to have the server broadcast the message sent by a client (eg. ClientA) to other clients. Any advice would be much appreciated.
This server class handles the connections of multiple clients with the use of threads:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
class EchoThread extends Thread {
private Socket socket;
//constructor
public EchoThread(Socket clientSocket) {
this.socket = clientSocket;
}
#Override
public void run() {
DataInputStream inp = null;
try {
inp = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
//print whatever client is saying as long as it is not "Over"
String line = "";
while (!line.equals("Over")) {
try {
line = inp.readUTF();
System.out.println(line);
} catch (IOException e) { System.out.println(e); }
}
//closes connection when client terminates the connection
System.out.print("Closing Connection");
socket.close();
} catch (IOException e) { System.out.println(e); }
}
}
public class Server {
private static final int PORT = 5000;
public static void main(String args[]) {
ServerSocket serverSocket = null;
Socket socket = null;
//starts the server
try {
serverSocket = new ServerSocket(PORT);
System.out.println("Server started");
System.out.println("Waiting for a client ...\n");
} catch (IOException e) { System.out.println(e); }
//while loop to accept multiple clients
int count = 1;
while(true) {
try {
socket = serverSocket.accept();
System.out.println("Client " + count + " accepted!");
count++;
} catch (IOException e) { System.out.println(e); }
//starts the server thread
new EchoThread(socket).start();
}
}
}
and this is the client class (I have multiple instances of this code running):
import java.net.*;
import java.io.*;
public class ClientA {
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream output = null;
public ClientA(String address, int port) {
//establish connection
try {
socket = new Socket(address, port);
System.out.println("Connected");
//takes input from terminal
input = new DataInputStream(System.in);
//sends output to the socket
output = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) { System.out.println(e); }
//string to read message from input
String line = "";
//keep reading until "Over" is input
while (!line.equals("Over")) {
try {
line = input.readLine();
output.writeUTF(line);
} catch (IOException e) { System.out.println(e); }
}
//close the connection
try {
input.close();
output.close();
socket.close();
} catch (IOException e) { System.out.println(e); }
}
public static void main (String args[]) {
ClientA client = new ClientA("127.0.0.1", 5000);
}
}
Do feel free to correct me on my code comments as I'm still not very familiar with socket programming.
You did well. Just add a thread to receive message in ClientA; and store socket clients in Server.
In fact, Server is also a "client" when is send message to client.
I add some code based on your code. It works well, hope it's helpful.
class EchoThread extends Thread {
//*****What I add begin.
private static List<Socket> socketList = new ArrayList<>();
//*****What I add end.
private Socket socket;
//constructor
public EchoThread(Socket clientSocket) {
this.socket = clientSocket;
socketList.add(socket);
}
#Override
public void run() {
DataInputStream inp = null;
try {
inp = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
//print whatever client is saying as long as it is not "Over"
String line = "";
while (!line.equals("Over")) {
try {
line = inp.readUTF();
System.out.println(line);
//*****What I add begin.
sendMessageToClients(line);
//*****What I add end.
} catch (IOException e) { System.out.println(e); break;}
}
//closes connection when client terminates the connection
System.out.print("Closing Connection");
socket.close();
} catch (IOException e) { System.out.println(e); }
}
//*****What I add begin.
private void sendMessageToClients(String line) throws IOException {
for (Socket other : socketList) {
if (other == socket) {
continue;//ignore the sender client.
}
DataOutputStream output = new DataOutputStream(other.getOutputStream());
output.writeUTF(line);
}
}
//*****What I add end.
}
public class ClientA {
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream output = null;
public ClientA(String address, int port) {
//establish connection
try {
socket = new Socket(address, port);
System.out.println("Connected");
//takes input from terminal
input = new DataInputStream(System.in);
//sends output to the socket
output = new DataOutputStream(socket.getOutputStream());
//*****What I add begin.
//Here create a thread to receive message from server.
DataInputStream inp = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
new Thread(() -> {
while (true) {
String str;
try {
str = inp.readUTF();
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();//error.
break;
}
}
}, "Client Reveiver.").start();
//*****What I add end.
} catch (IOException e) { System.out.println(e); }
//string to read message from input
String line = "";
//keep reading until "Over" is input
while (!line.equals("Over")) {
try {
line = input.readLine();
output.writeUTF(line);
} catch (IOException e) { System.out.println(e); }
}
//close the connection
try {
input.close();
output.close();
socket.close();
} catch (IOException e) { System.out.println(e); }
}
I would have a single server thread which would maintain a register of the clients, possibly in a concurrent collection. Then I would send each message received from a client to all other clients.
I am able to connect my DatagramSocket server and client inside my local area network(LAN) but I am unable outside of it.
Here is my code:
GameServer class:
public class GameServer extends Thread {
private DatagramSocket socket;
public GameServer() {
try {
this.socket = new DatagramSocket(7081,InetAddress.getByName("0.0.0.0"));
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
try {
socket.receive(packet);
if(new String(packet.getData()).trim().equalsIgnoreCase("ping")){
sendData("pong".getBytes(),packet.getAddress(),packet.getPort());
}
System.out.println(new String(packet.getData()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void sendData(byte[] data, InetAddress ipAddress, int port) {
DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, port);
try {
this.socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
}
GameClient class:
public class GameClient extends Thread {
private InetAddress ipAddress;
private DatagramSocket socket;
public GameClient(String ipAddress) {
try {
this.socket = new DatagramSocket();
this.ipAddress = InetAddress.getByName(ipAddress);
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
try {
socket.receive(packet);
System.out.println(new String(packet.getData()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void sendData(byte[] data) {
DatagramPacket packet = new DatagramPacket(data, data.length, ipAddress, 7081);
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
}
RunClient class:
public class RunClient {
public static void main(String[] args) {
GameClient client = new GameClient("/*Here is my external ip address*/"); //If i am running this for LAN then 192.168.1.67 and localhost work
client.start();
client.sendData("ping".getBytes());
}
}
RunServer class:
public class RunServer {
public static void main(String[] args) {
GameServer server = new GameServer();
server.start();
}
}
Here are my port forwarding configurations:
Some additional information:
My local ip address is 192.168.1.67
The port I am using is 7081
When I check my ip and port at http://www.canyouseeme.org it gave me the following error:
Error: I could not see your service on "my external ip" on port (7081)
I don't see any problem in your code. That site (http://www.canyouseeme.org) must be trying with TCP instead of UDP.
Please add exception for the port 7081 into firewall, so that it allow incoming connections on port.
when you are trying in LAN the IP might be different, when you are trying outside, you have to try with the external ip, which should be configured to redirect the request to your localip.
Thanks,
Ankireddy Polu
I've searched for examples of how to implement a simple ipv6 multicast example, however I have only found examples using ipv4.
Can anybody provide a simple "helloworld" example of ipv6 multicasting?
Here is a simple client server example. Incidentally running it on multiple machines on a network wil get all the machines chatting to each other, good for testing automatic discovery on the network.
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UdpBroadcaster {
private static final Logger LOGGER = LoggerFactory.getLogger(UdpBroadcaster.class);
private static final int PORT = 9876;
private static final String MCAST_ADDR = "FF7E:230::1234";
private static InetAddress GROUP;
public static void main(String[] args) {
try {
GROUP = InetAddress.getByName(MCAST_ADDR);
Thread server = server();
server.start();
Thread.sleep(3000);
Thread client = client();
client.start();
client.join();
} catch (Exception e) {
LOGGER.error("Usage : [group-ip] [port]");
}
}
private static Thread client() {
return new Thread(new Runnable() {
public void run() {
MulticastSocket multicastSocket = null;
try {
multicastSocket = new MulticastSocket(PORT);
multicastSocket.joinGroup(GROUP);
while (true) {
try {
byte[] receiveData = new byte[256];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
multicastSocket.receive(receivePacket);
LOGGER.info("Client received from : " + receivePacket.getAddress() + ", " + new String(receivePacket.getData()));
} catch (Exception e) {
LOGGER.error(null, e);
}
}
} catch (Exception e) {
LOGGER.error(null, e);
} finally {
multicastSocket.close();
}
}
});
}
private static Thread server() {
return new Thread(new Runnable() {
public void run() {
DatagramSocket serverSocket = null;
try {
serverSocket = new DatagramSocket();
try {
while (true) {
byte[] sendData = new byte[256];
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, GROUP, PORT);
serverSocket.send(sendPacket);
ThreadUtilities.sleep(1000);
}
} catch (Exception e) {
LOGGER.error(null, e);
}
} catch (Exception e) {
LOGGER.error(null, e);
}
}
});
}
}
Hope that helps.
M
The only difference between an IPv6 program and an IPv4 program in Java is the IP addresses. In this case you have to use an IPv6-style multicast address when joining the group, and when sending to it. Everything else is the same.