server to serve multi clients
Hi,
i want to implement a client /server codes in the following fashion :
the client A should enter the IP of of the server and clients B and C enters the same IP,so the three clients can communication with each other through the server.
The server could omit any client from the service.
any good tutorials or examples?
I appreciate any help.
Re: server to serve multi clients
Re: server to serve multi clients
thanks so much,but still have one problem
how can i remove a specific client from the server by entering the client IP?
Re: server to serve multi clients
Quote:
Originally Posted by
sana'a
thanks so much,but still have one problem
how can i remove a specific client from the server by entering the client IP?
That question is so far out of context as to be unanswerable. If you wish your question answered, then I recommend providing more context
Re: server to serve multi clients
Quote:
how can i remove a specific client from the server by entering the client IP?
If you have code on the server that can receive the IP to be removed from some source: GUI or administrative client,
and if you have a list of clients' with their status and data such as their IP,
then you could have a method that searches the list and removes the client based on their IP.
Re: server to serve multi clients
Quote:
and if you have a list of clients' with their status and data such as their IP,
then you could have a method that searches the list and removes the client based on their IP.
and how to create such a list and close the connection with a client from that list?
please, i need some code examples ,i appreciate that.
Re: server to serve multi clients
Post your code so we can see where and how you are getting and saving the necessary information about the connections
Re: server to serve multi clients
Quote:
Originally Posted by
doWhile
That question is so far out of context as to be unanswerable. If you wish your question answered, then I recommend providing more context
I guess I stand corrected.
Re: server to serve multi clients
Often its hard to understand what OPs are asking. I always try to come up with some approach to get the OP to define what he's doing. Hoping eventually he'll give enough info that some one will understand what the problem is.
Re: server to serve multi clients
this is the server code.i didn't write all the code but i modified it
Code:
public class sender {
ServerSocket MyService;
BufferedInputStream input;
TargetDataLine targetDataLine;
BufferedOutputStream out;
ByteArrayOutputStream byteArrayOutputStream;
AudioFormat audioFormat;
SourceDataLine sourceDataLine;
byte tempBuffer[] = new byte[10000];
sender() throws LineUnavailableException{
try {
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class,audioFormat);
sourceDataLine = (SourceDataLine)
AudioSystem.getLine(dataLineInfo);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
MyService = new ServerSocket(500);
} catch (IOException e) {
e.printStackTrace();
}
try{
while(true){
new Begin(MyService.accept()).start();
MyService.close();
}
}
catch(Exception e){
System.out.println(e);
}
}
private AudioFormat getAudioFormat(){
float sampleRate = 8000.0F;
int sampleSizeInBits = 16;
int channels = 1;
boolean signed = true;
boolean bigEndian = false;
return new AudioFormat(
sampleRate,
sampleSizeInBits,
channels,
signed,
bigEndian);
}
public static void main(String s[]) throws LineUnavailableException{
sender s2=new sender();
}
private void captureAudio() {
try {
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
audioFormat = getAudioFormat();
DataLine.Info dataLineInfo = new DataLine.Info(
TargetDataLine.class, audioFormat);
Mixer mixer = AudioSystem.getMixer(mixerInfo[2]);
targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
Thread captureThread = new CaptureThread();
captureThread.start();
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
class CaptureThread extends Thread {
byte tempBuffer[] = new byte[10000];
public void run() {
try {
while (true) {
int cnt = targetDataLine.read(tempBuffer, 0,
tempBuffer.length);
out.write(tempBuffer);
}
} catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
}
class Begin extends Thread{
private Socket clientSocket;
public Begin(Socket sock){
this.clientSocket=sock;
System.out.println(clientSocket.getInetAddress());
}
public void run(){
try{
captureAudio();
input = new BufferedInputStream(clientSocket.getInputStream());
out=new BufferedOutputStream(clientSocket.getOutputStream());
while(input.read(tempBuffer)!=-1){
sourceDataLine.write(tempBuffer,0,10000);
}
}
catch(Exception e){
System.out.println(e);
System.exit(0);
}
}
}
}
so i can get the IP of all the clients in the Begin thread by the getInetAddress() method,(that's how i can create a list of the clients define them by IP when i switch to GUI)
Let us say I have a button (Remove),when i click Remove an input dialog would appear and i should write the IP of the client that i want to remove him
from the server.How to close the connection with that client without affect the other clients communication?
Re: server to serve multi clients
Change the code so that the socket is saved in a list before passing the socket to the Begin class.