This site requires JavaScript, please enable it in your browser!
Greenfoot back
CooliMC
CooliMC wrote ...

2015/4/11

Need Help with Sockets

CooliMC CooliMC

2015/4/11

#
Hey guys last night i have tried to write me a little programm like teamspeak with a server reading the byte from the sockets and contributes it to all clients so they can hear the sound but it doesn't work and i don't know why: Server:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.commons.io.IOUtils;


public class Server
{
	ServerSocket server;
	Socket[] Clients;
	
	public static void main(String[] args)
	{
        Server s = new Server(999,64);
	}
	
	public Server(int port,int slots)
	{
		if (runServer(port, slots))
        {
			listenToClients();
        } 
        else
        {
        	
        }
	}
	
	public boolean runServer(int port, int slots)
	{
        try
        {
                server = new ServerSocket(port);
                Clients = new Socket[slots];
                System.out.println("Server wurde gestartet!");
                
                return true;
        }
        catch (IOException e)
        {
                System.err.println("Server konnte nicht gestartet werden!");
                
                return false;
        }
	}
	
	public void listenToClients()
	{
        while(true)
        {
                try
                {
                        Socket client = server.accept();
                        System.out.println("Neuer Client Verbunden!");
                        
                        Thread clientThread = new Thread(new ClientHandler(client));
                        clientThread.start();
                        
                        for(int i = Clients.length; i > 0; i--)
                        {
                        	if(Clients[i-1] == null)
                        	{
                        		Clients[i-1] = client;
                        		i = 0;
                        	}
                        }
                }
                catch (IOException e)
                {
                        System.err.println("Fehler beim Aufbau von Verbindungen!");
                }              
        }
	}
	
	public class ClientHandler implements Runnable
	{	 
        Socket client;
       
        public ClientHandler(Socket client)
        {
                try
                {
                        this.client = client;
                }
                catch (Exception e)
                {
                        System.err.println("Fehler bei Clientverbindung!");
                }
        }
       
        public void run()
        {
                try
                {
                	DataInputStream dis;
                	byte[] data = new byte[1024];
                	
                	while(client != null)
                        {
                			dis = new DataInputStream(client.getInputStream());
                			dis.readFully(data, 0, 1024);
                			//data = IOUtils.toByteArray(client.getInputStream());
                			sendToAllClients(data,client);
                        }
                }
                catch (IOException e)
                {
                		System.out.println(e);
                        System.err.println("Fehler bei Clientverarbeitung!");
                }
        }
	}
	
	public void sendToAllClients(byte[] b, Socket s)
	{
		DataOutputStream dos;
		
		for(int i = Clients.length; i > 0; i--)
        {
        	if(Clients[i-1] != null /*&& Clients[i-1] != s*/)//s is the user whos talking he shouldnt hear himself :D
        	{
        		try
        		{
        			dos = new DataOutputStream(Clients[i-1].getOutputStream());
        			dos.write(b, 0, b.length);
        		}
        		catch(Exception e)
        		{
        			
        		}
        	}
        }
	}
}
Client:
import java.io.DataOutputStream;
import java.net.Socket;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

import org.apache.commons.io.IOUtils;

public class Verbindung
{
	Socket client;
	byte[] bytes = new byte[1024];
	DataOutputStream dos;
	
	public static void main(String[] args)
	{
		Verbindung d = new Verbindung("localhost",999);
	}
	
	public Verbindung(String Ip, int port)
	{	
		if(connectToServer(Ip, port))
		{
			System.out.println("Zum Server verbunden !");
			
			Thread Microfon = new Thread(new MicToServer());
			Microfon.start();
			
			try
			{
				AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
				DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, format);
				
		    	SourceDataLine speakers = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
		        speakers.open(format);
		        speakers.start();
		        
				while(client != null)
				{
					bytes = IOUtils.toByteArray(client.getInputStream());
					
					speakers.write(bytes, 0, bytes.length);
				}
			}
			catch (Exception e)
			{
				System.err.println("Lautsprecher Fehler!");
			}
		}
	}
	
	
	public boolean connectToServer(String Ip, int port) {
		try
		{
			System.out.println("Verbinde mit "+Ip+":"+port);
			client = new Socket(Ip, port);
			
			return true;
		}
		catch(Exception e)
		{
			System.err.println("Verbindung zum Server fehlgeschlagen");
			
			return false;
		}
	}
	
	public class MicToServer implements Runnable {

		public void run()
		{
			byte[] tosend = new byte[1024];
			
			try
			{
				AudioFormat format = new AudioFormat(8000.0f, 16, 1, true, true);
				TargetDataLine microphone = AudioSystem.getTargetDataLine(format);
				DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
		        microphone = (TargetDataLine) AudioSystem.getLine(info);
		        microphone.open(format);
				
				while(client != null)
				{
					microphone.read(tosend, 0, 1024);
					dos = new DataOutputStream(client.getOutputStream());
					dos.write(tosend);
					
				}
			}
			catch(Exception e)
			{
				System.err.println("Mikrofon Fehler!");
			}
		}
		
	}
}
I hope one of you knows how to fi it ...
CooliMC CooliMC

2015/4/13

#
#push :D
CooliMC CooliMC

2015/4/16

#
I really need help pls .... :(
davmac davmac

2015/4/16

#
"doesn't work"?
lordhershey lordhershey

2015/4/17

#
Did quick search found this http://stackoverflow.com/questions/25798200/java-record-mic-to-byte-array-and-play-sound now I am to try do see if I can make a chatty thing with java submitting sound bites via http submits. I have not run your program yet, but my initial thoughts is you need to make the sockets flush, and I am not 100% sure of the socket option available in java, but there must be some auto flush feature.
You need to login to post a reply.