import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UserListeningThread extends Thread{
	
	private User myUser;
	
	public UserListeningThread(String s,User user) { 
		super(s);
		this.myUser = user;
	}
	
	public void run(){ 
		DatagramSocket dgramSocket= null;
		try {
		DatagramSocket dgramSocket= new DatagramSocket(this.myUser.getNport(),this.myUser.getAddIP());
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		while(!this.myUser.haveToStopThread) {
		
			
			byte[] buffer = new byte[256];
			DatagramPacket inPacket= new DatagramPacket(buffer, buffer.length);
			try {
				dgramSocket.receive(inPacket);
			} catch (Exception e) {
				e.printStackTrace();
			}
			buffer = inPacket.getData();
			InetAddress itsIP=inPacket.getAddress();
			int itsPort=inPacket.getPort();
			
			String toSend = myUser.getAddIP().toString()+":"+myUser.getNport()+":"+myUser.getPseudo();
			DatagramPacket outPacket= new DatagramPacket(toSend.getBytes(), toSend.length(),itsIP, 12229);

			try {
				dgramSocket.send(outPacket);
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		dgramSocket.close();

	}
}



