I am developing a program that has a chat feature and I am using sockets in it.
In my case, I want to handle each of the client in a different window chat(PLEASE SEE ATTACHED IMAGE).
As of now, when 1 client is connected, there is no problem. But when 2 clients are connected, the first client will be overridden by the 2nd one and he can't receive messages from server not unless I close the connection for the latest client connected(Server still receiving messages from all client although only 1 client can receive from server).
How am I gonna do this? I am using captain casa framework
I want to manage it like what did the image below do.
IMAGE HERE
Here is my code:
Server:
public void mainserver(){
Thread server = new Thread(new Runnable() {
#Override
public void run() {
try {
serverSocket = new ServerSocket(port);
System.out.println("Server Online... \nWaiting for Connections");
} catch (IOException e) {
e.printStackTrace();
}
while (accept){
try {
socket = serverSocket.accept();
System.out.println("New Connection Estasblished!!!");
chatHandler chat = new chatHandler(socket);
chat.start();
} catch (IOException e) {
System.out.println("server not terminate all connections");
System.exit(-1);
}
}
}
});
server.start();
}
public class chatHandler extends Thread{
Socket socket;
public chatHandler(Socket socket){
this.socket = socket;
}
public void run(){
try {
din = new DataInputStream(socket.getInputStream());
dout = new DataOutputStream(socket.getOutputStream());
dout.writeUTF("Hi! Thank you for reaching us! How may I help you!?");
while (!read.equals(".end")){
read = din.readUTF();
if (getServerArea()!=null){
setServerArea(getServerArea()+"\n"+read);
}else {
setServerArea(read);
}
}
System.out.println("end of chat server");
} catch (IOException e) {
e.printStackTrace();
}finally {
System.out.println("Exit");
try {
dout.close();
din.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void serverSend(javax.faces.event.ActionEvent event) { // "Send" button
write = getServerField();
try {
dout.writeUTF(write);
dout.flush();
if (getServerArea()!=null){
setServerArea(getServerArea()+"\n"+write);
setServerField("");
}else {
setServerArea(write);
setServerField("");
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(write);
}
Client:
public void client(){
Thread client = new Thread(new Runnable() {
#Override
public void run() {
try {
socket = new Socket("localhost",port);
din = new DataInputStream(socket.getInputStream());
dout = new DataOutputStream(socket.getOutputStream());
while (!read.equals("bye")){
read = din.readUTF();
if (getClientArea()!=null){
setClientArea(getClientArea()+"\n"+read);
}else {
setClientArea(read);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
din.close();
dout.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
client.start();
}
public void clientSend(javax.faces.event.ActionEvent event) {
write = getClientField();
try {
dout.writeUTF(write);
dout.flush();
if (getClientArea()!=null){
setClientArea(getClientArea()+"\n"+write);
setClientField("");
}else {
setClientArea(write);
setClientField("");
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(write);
}
I believe I understand the problem, and how to correct it.
You are using a unique thread (chatHandler) for each new connection.
This thread writes an automatic "Hello" upon connection, but thereafter is dedicated to reading messages (in the while loop you only read din) and updating the console accordingly. Since each thread is managing a reference to din, all incoming messages are OK.
However, it seems that writing back to a client (serverSend) is not in a thread; it is triggered by a button event. At this point, dout will be a reference to the most recent connection, and not a reference to the client intended to get the message. That is why the most recent client gets all future messages.
The correction is to choose the correct 'dout' for the intended client. When the server 'operator' chooses to write a message back (clicking the send button), somehow you need to obtain the correct 'dout' for that client.
One way to do this is to establish dout prior to creating the thread (using socket), and maintain a relationship between each client, and it's corresponding dout (i.e. in a Map).
If the problem is still not clear (that each client must have a unique reference to dout), please let me know and I will try to clarify.
I have a UDP client sending packets to my UDP server.
The packets are sent and received successfully when I use the server's IP address or the when I use the router's public IP address
// Works
Constants.SERVER_IP_ADDRESS
// Works
Constants.PUBLIC_IP_ADDRESS;
However, I'm not able to receive any packet when sending over the internet even though I've set up forwarding.
Any idea why? Thanks.
UdpClient
public class UdpClient {
private String mServerAddress = Constants.PUBLIC_IP_ADDRESS;
private int mServerPort = Constants.PORT;
private static final int MAX_TRIES = 5;
public void sendDatagramPacket(){
// Cannot network on Main UI thread
new AsyncTask(){
#Override
protected Object doInBackground(Object[] objects) {
System.out.println("sendDatagramPacket");
// Create a socket
DatagramSocket socket = null;
try{socket = new DatagramSocket();}
catch (SocketException e){e.printStackTrace();}
// Create a datagram
byte[] bytesToSend = new byte[3];
bytesToSend[0] = (byte) 255;
bytesToSend[1] = (byte) 255;
bytesToSend[2] = (byte) 255;
InetAddress serverInetAddress = null;
try{serverInetAddress = InetAddress.getByName(mServerAddress);}
catch (UnknownHostException e) {e.printStackTrace();}
DatagramPacket datagramPacket = new DatagramPacket(
bytesToSend, bytesToSend.length, serverInetAddress, mServerPort);
// Send packet; packets may be lost, so we have to keep trying
int tries = 0;
while(tries < MAX_TRIES) {
try{socket.send(datagramPacket);}
catch (NullPointerException e){e.printStackTrace();}
catch (IOException e){e.printStackTrace();}
tries++;
}
try{socket.close();}
catch (NullPointerException e){e.printStackTrace();}
return null;
}
}.execute();
}
}
UdpServer
public class UdpServer {
private int mHostPort = Constants.MAC_PORT;
// Defines max receive-buffer size; maximum possible for UDP is ~64,000
private static final int MAX_BUFFER_SIZE = 256;
public void listenForPacket(){
System.out.println("listenForPacket");
new Thread(){
#Override
public void run(){
// Get the socket to the receiving port
DatagramSocket socket = null;
try { socket = new DatagramSocket(mHostPort);}
catch (SocketException e){e.printStackTrace();}
// Create receive-buffer and receive-packet
byte[] receiveBuffer = new byte[MAX_BUFFER_SIZE];
DatagramPacket datagramPacket = new DatagramPacket(receiveBuffer,MAX_BUFFER_SIZE);
// Pause thread here listening for packet
try{
socket.receive(datagramPacket);
System.out.println("Datagram received successfully");
}
catch (IOException e){e.printStackTrace();}
try{socket.close();}
catch (NullPointerException e){e.printStackTrace();}
}
}.start();
}
}
This answer doesn't directly address your problem, but it could do indirectly. (Your bad exception handling could be hiding other problems ...)
You have systemic problems in the way that you deal with exceptions. For example:
DatagramSocket socket = null;
try { socket = new DatagramSocket(mHostPort);}
catch (SocketException e){e.printStackTrace();}
What happens if an exception is thrown by the constructor?
Answer: you print a stack trace, and then keep going as if nothing bad happened.
Except that something bad did happen. Indeed, if the constructor failed, and you don't have a DatagramSocket, then the remaining code cannot possibly work. But you "recovered".
This pattern is repeated in a number of places. Indeed you end up with code to catch NPEs that are directly caused by incorrect "recovery" code.
This the right way to do it:
public void run() {
try (DatagramSocket socket = new DatagramSocket(mHostPort)) {
// Get the socket to the receiving port
DatagramSocket socket = new DatagramSocket(mHostPort);
// Create receive-buffer and receive-packet
byte[] receiveBuffer = new byte[MAX_BUFFER_SIZE];
DatagramPacket datagramPacket =
new DatagramPacket(receiveBuffer,MAX_BUFFER_SIZE);
// Pause thread here listening for packet
socket.receive(datagramPacket);
System.out.println("Datagram received successfully");
}
catch (IOException | RuntimeException e) {
e.printStackTrace();
}
}
Things to note:
As well as being more correct (I claim), this code is actually simpler than your version. There are fewer error cases, and (I claim) the source of those pesky NPE's has been eliminated.
The exception handling does not attempt to recover. If an exception occurs, the thread will end ... immediately
Note the use of try-with-resources to ensure that the socket is always closed.
Catching RuntimeException is probably unnecessary ... see above about NPEs.
An alternative to handling the exception in the run() method would be to use a default exception handler.
I have a problem with socket communication.
Sometimes reading from inputstream on client side stops working while the server is still sending new messages. I debugged the server so I know that is still working and sending messages to the outputstream. But on the client side read from inputstream is blocked. I can't debug this situation on client side. I only see difference in received messages on client side just before everything stops.
Example of good received message when everything works fine. Single send message in one line (I use DataInputStream.readUTF() method on client side and DataOutputStream.writeUTF(String msg) on server side. )
ADD;MB57,18-9,5,dd,10,10;
UP;MB15;20;14;20;13;1.0;
ADD;MB37,18-9,5,xx,10,10;
UP;MB13;20;14;20;13;1.0;
ADD;MB47,18-9,5,ww,10,10;
UP;MB13;20;14;20;13;1.0;
And this is happens just before my socket stop reading from input. One big mess. And everything that has been sent from the beginning in one line. It looks like the buffer overload O.o What happens?
11-07 11:36:41.978: I/System.out(17980): 11;8;10;8;0.1;��UPPOS;MB8;16;8;16;7;1.0;��PATH;MB8��UPPOS;MB20;14;9;14;10;1.0;�� ADDMOB;MB20,14-10,6,mummy,50,50;�� PATH;MB20��UPPOS;MB50;12;8;12;7;1.0;�� PATH;MB50��UPPOS;MB13;15;11;14;11;1.0;�� PATH;MB19��PATH;MB8��UPPOS;MB20;14;10;13;10;1.0;�� PATH;MB20��UPPOS;MB50;12;7;12;6;1.0;�� PATH;MB50��UPPOS;MB13;14;11;14;10;1.0;��UPPOS;MB19;13;9;14;9;1.0;�� PATH;MB19��PATH;MB8��UPPOS;MB20;13;10;13;9;1.0;��ADDMOB;MB20,13-9,6,mummy,50,50;�� PATH;MB20��UPPOS;MB50;12;6;12;7;1.0;�� PATH;MB50��UPPOS;MB13;14;10;15;9;1.0;��!ADDMOB;MB13,15-9,5,chicken,10,10;�� PATH;MB13��UPPOS;MB19;14;9;14;10;1.0;��!ADDMOB;MB19,14-10,1,goblin,37,50;�� PATH;MB19��UPPOS;NP12;10;8;9;8;0.1;��UPPOS;MB8;16;7;17;7;1.0;��PATH;MB8��UPPOS;MB20;13;9;12;9;1.0;�� PATH;MB20��UPPOS;MB50;12;7;11;7;1.0;�� PATH;MB50��UPPOS;MB13;15;9;14;9;1.0;�� PATH;MB13��UPPOS;MB19;14;10;13;9;1.0;�� ADDMOB;MB19,13-9,1,goblin,37,50;�� PATH;MB19��UPPOS;MB8;17;7;16;7;1.0;��PATH;MB8��UPPOS;MB20;12;9;12;8;1.0;��UPPOS;MB50;11;7;12;7;1.0;�� PATH;MB50��UPPOS;MB13;14;9;14;10;1.0;��"ADDMOB;MB13,14-10,5,chicken,10,10;�� PATH;MB13�� PATH;MB19��UPPOS;MB8;16;7;16;8;1.0;��PATH;MB8�� PATH;MB20�� PATH;MB50��UPPOS;MB13;14;10;15;10;1.0;�� PATH;MB13��UPPOS;MB19;13;9;14;9;1.0;�� PATH;MB19��UPPOS;NP12;9;8;9;9;0.1;��UPPOS;MB8;16;8;16;7;1.0;��PATH;MB8��UPPOS;MB20;12;8;12;9;1.0;�� PATH;MB20��UPPOS;MB50;12;7;12;6;1.0;�� PATH;MB50��UPPOS;MB13;15;10;14;10;1.0;��UPPOS;MB19;14;9;13;9;1.0;�� PATH;MB19��PATH;MB8��UPPOS;MB20;12;9;12;8;1.0;�� PATH;MB50��UPPOS;MB13;14;10;14;9;1.0;��!ADDMOB;MB13,14-9,5,chicken,10,10;�� PATH;MB13�� PATH;MB19��UPPOS;MB8;16;7;16;6;1.0;��PATH;MB8��UPPOS;MB20;12;8;12;7;1.0;�� PATH;MB20�� PATH;MB50��UPPOS;MB13;14;9;14;10;1.0;��"ADDMOB;MB13,14-10,5,chicken,10,10;�� PATH;MB13��UPPOS;MB19;13;9;13;10;1.0;��!ADDMOB;MB19,13-10,1,goblin,37,50;�� PATH;MB19��UPPOS;NP12;9;9;9;8;0.1;��PATH;MB8�� PATH;MB20��UPPOS;MB50;12;6;11;6;1.0;�� PATH;MB50��UPPOS;MB13;14;10;14;9;1.0;��!ADDMOB;MB13,14-9,5,chicken,10,10;�� PATH;MB13��UPPOS;MB19;13;10;13;9;1.0;�� ADDMOB;MB19,13-9,1,goblin,37,50;�� PATH;MB19��UPPOS;MB8;16;6;16;7;1.0;��PATH;MB8�� PATH;MB20��UPPOS;MB50;11;6;12;6;1.0;�� PATH;MB50��UPPOS;MB13;14;9;15;9;1.0;�� PATH;MB13�� PATH;MB19��PATH;MB8��UPPOS;MB20;12;7;12;8;1.0;�� PATH;MB20�� PATH;MB50��UPPOS;MB13;15;9;14;9;1.0;�� PATH;MB13��UPPOS;MB19;13;9;13;10;1.0;��!ADDMOB;MB19,13-10,1,goblin,37,50;�� PATH;MB19��UPPOS;NP12;9;8;10;8;0.1;��UPPOS;MB8;16;7;16;8;1.0;��PATH;MB8��UPPOS;MB20;12;8;12;7;1.0;�� PATH;MB20�� PATH;MB50��UPPOS;MB13;14;9;15;9;1.0;�� PATH;MB13��UPPOS;MB19;13;10;13;11;1.0;�� PATH;MB19��UPPOS;MB8;16;8;16;9;1.0;��PATH;MB8��UPPOS;MB20;12;7;11;7;1.0;�� PATH;MB20��UPPOS;MB13;15;9;14;9;1.0;�� PATH;MB50�� PATH;MB13��UPPOS;MB19;13;11;13;10;1.0;��UPPOS;MB20;11;7;12;7;1.0;�� PATH;MB20��UPPOS;MB8;16;9;16;8;1.0;�� PATH;MB50��UPPOS;MB13;14;9;15;9;1.0;�� PATH;MB13��UPPOS;MB19;13;10;14;10;1.0;��UPPOS;NP12;10;8;11;8;0.1;�� PATH;MB20��UPPOS;MB8;16;8;16;7;1.0;��PATH;MB8�� PATH;MB50��UPPOS;MB13;15;9;14;9;1.0;�� PATH;MB13��UPPOS;MB19;14;10;15;9;1.0;�� ADDMOB;MB19,15-9,1,goblin,37,50;�� PATH;MB19��UPPOS;MB20;12;7;11;7;1.0;�� PATH;MB20��UPPOS;MB8;16;7;16;6;1.0;��PATH;MB8��UPPOS;MB50;12;6;12;7;1.0;�� PATH;MB50��UPPOS;MB13;14;9;13;9;1.0;�� PATH;MB13��UPPOS;MB19;15;9;14;9;1.0;�� PATH;MB19�� PATH;MB20��UPPOS;MB8;16;6;16;7;1.0;��PATH;MB8��UPPOS;MB50;12;7;12;8;1.0;�� PATH;MB50�� PATH;MB13��UPPOS;MB19;14;9;14;10;1.0;��!ADDMOB;MB19,14-10,1,goblin,37,50;�� PATH;MB19��UPPOS;MB20;11;7;12;7;1.0;�� PATH;MB20��UPPOS;MB8;16;7;16;8;1.0;��PATH;MB8��UPPOS;MB50;12;8;12;9;1.0;�� PATH;MB50��UPPOS;MB13;13;9;14;9;1.0;�� PATH;MB13��UPPOS;MB19;14;10;15;10;1.0;�� PATH;MB19��UPPOS;MB20;12;7;11;7;1.0;�� PATH;MB20��UPPOS;MB50;12;9;12;8;1.0;�� PATH;MB50��UPPOS;MB8;16;8;16;7;1.0;��PATH;MB8��UPPOS;MB13;14;9;13;9;1.0;�� PATH;MB13��UPPOS;MB19;15;10;15;9;1.0;�� ADDMOB;MB19,15-9,1,goblin,37,50;��UPPOS;NP12;11;8;10;8;0.1;��UPPOS;MB20;11;7;12;7;1.0;��UPPOS;MB8;16;7;16;6;1.0;��PATH;MB8�� PATH;MB13��UPPOS;MB50;12;8;11;8;1.0
Client side
private DataOutputStream out;
private Socket client;
private DataInputStream in;
private Thread inputListener;
public void createConnection(){
try {
client = new Socket(serverName, port);
setOut(new DataOutputStream(client.getOutputStream()));
in = new DataInputStream(client.getInputStream());
inputListener=new Thread(){
public void run(){
try {
synchronized(in){
while(client!=null){
try{
String read = new String(in.readUTF());
/** do somethink with input msg */
} catch (java.io.UTFDataFormatException e1) {
e1.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally{
reconnect();
}
}
};
inputListener.start();
} catch (Exception e) {
e.printStackTrace();
}
}
private void reconnect() {
try {
client.close();
client=null;
inputListener.interrupt();
setOut(null);
in.close();
in=null;
} catch (IOException e) {
e.printStackTrace();
} finally{
System.out.println("RECONECT METHOD IN SOCKET");
}
}
Server side
private DataOutputStream out;
private Socket client;
public Client(Socket client) {
try {
setOut(new DataOutputStream(client.getOutputStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessage(String string) {
try {
getOut().writeUTF(string);
} catch (IOException e) {
e.printStackTrace();
disconected();
}
}
You must be writing something else to the stream. Catching and ignoring UTFDataFormatException is no solution. Once you get it, you will never get back into sync with the sender.
NB Converting the result of readUTF() to a String is futile. It already is a String.
I have multiple servers on my network that all send out a broadcast message. With the following client I am trying to capture all the broadcast messages from all servers. The sending part works fine(not included in this post), but my receiving part doesn't work... I keep getting "SocketException: Not a multicast address", what am I doing wrong?
public static String[] capture(int port) { // port is always 63332
ArrayList<String> clients = new ArrayList<>();
InetAddress address = Utilities.getBroadcastAddress(); // I get "/192.168.2.255" here
MulticastSocket socket = null;
try {
socket = new MulticastSocket(port);
socket.setSoTimeout(2000);
socket.joinGroup(address); // this part throws the exception
DatagramPacket packet;
byte[] packetContent;
while (true) {
packetContent = new byte[1024];
packet = new DatagramPacket(packetContent, packetContent.length);
try {
socket.receive(packet);
String client = packet.getAddress() + ":" + packet.getPort();
clients.add(client);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(socket != null) {
try {
socket.leaveGroup(address);
} catch(IOException e) {
e.printStackTrace();
}
socket.close();
}
return clients.toArray(new String[clients.size()]);
}
You are confusing broadcasting with multicasting. A multicast address is not a broadcast address. Make up your mind which it is that you're doing. If you're receiving multicasts, you need to join the correct multicast address, whatever it is. If you're receiving broadcasts, don't join anything.
I am writing a class in Java which is used to greatly simplify the process of multicasting. However, I am having two big problems with it:
The class sends data (I can verify this with my net monitor, Wireshark) but the data is not received by any others in the same group.
On some machines, the sending packet TTL is exceeded in transit (again, according to Wireshark).
Could anyone please help me? I've been trying and searching for answers for hours, and it appears that my code follows all of the basic procedures for connecting to, joining, sending, and receiving data from a multicast host.
Here is a snippet of the relevant portions of the class:
Multicaster class:
public class Multicaster {
public int port = 5540;
protected String IPAddress;
private MulticastSocket msConn;
private InetAddress netAddr;
public Multicaster(String IPAddress) {
this.IPAddress = IPAddress;
}
public String recieveData() {
byte[] buf = new byte[1000];
DatagramPacket pack = new DatagramPacket(buf, buf.length);
try {
this.msConn.receive(pack);
new Message(pack);
String out = new String(pack.getData());
return out.substring(0, pack.getLength());
} catch (IOException e) {
return new String("");
}
}
public void joinGroup() {
try {
this.msConn.joinGroup(this.netAddr);
} catch (IOException e) {
//This error shouldn't occur since it would caught by the connect() method during initial connection to the host
}
}
public void connect() throws MulticasterInitException {
//Try to create a multicast connection on the given IP address and port
try {
try {
//Create a multicast connection on a given port, throws UnknownHostException
this.msConn = new MulticastSocket(this.port);
//If all goes well, then create a connection to a given IP address using the above port number, throws IOException and SecurityException
this.netAddr = InetAddress.getByName(this.IPAddress);
}
}
/**
* Here all of the possible exceptions that are thrown above
* are caught and handled here. This works just fine.
*/
}
public void sendData(String data) throws MulticasterSendException {
DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(), this.netAddr, this.port);
try {
this.msConn.send(packet);
} catch (IOException e) {
throw new MulticasterSendException("Java could not communicate with the server. Please check your network connections.", e);
}
}
}
Sample usage to send data:
Multicaster multicast = new Multicaster("239.0.0.0");
try {
multicast.connect();
} catch (MulticasterInitException e) {
//Handle exception...
}
multicast.joinGroup();
try {
multicast.sendData("Hi");
} catch (MulticasterSendException e) {
//Handle exception...
}
Sample usage to receive data:
Multicaster multicast = new Multicaster("239.0.0.0");
try {
multicast.connect();
} catch (MulticasterInitException e) {
//Handle exception...
}
multicast.joinGroup();
System.out.print(multicast.recieveData());
I've run into similar problems before and had to ensure that the NetworkInterface was specified on the receiving side.
SocketAddress socketAddress = new InetSocketAddress(groupIp, groupPort);
NetworkInterface networkInterface = NetworkInterface.getByName(interfaceName);
socket.joinGroup(socketAddress, networkInterface);
Where interfaceName is one of the interface names shown when running ifconfig on Linux or Mac.