• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to communicate between one server and multiple clients

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i created an application which communicates between a server and one client using serversocket class.is there any way to find the instant at which a client connects and then assign a seperate socket for it. thanks in advance
 
P.Praveen Jesudhas
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the code i wrote.(it works properly for server to one client communication)
public class server
{
Socket s;
JFrame fr;
JTextArea ta;
JTextArea tb;
JButton b;
JPanel p;
BufferedReader br;
PrintWriter pw;
ServerSocket ss;
server()
{

fr=new JFrame("Client Window");
p=new JPanel();
b=new JButton("Send Data");
ta=new JTextArea(3,50);
tb=new JTextArea(30,50);
b.addActionListener(new sending());
p.add(ta);
p.add(b);
p.add(tb);
(fr.getContentPane()).add(p);
fr.setSize(400,300);
fr.setVisible(true);
}




public static void main(String a[])throws Exception
{
server x=new server();
x.makeCon();
x.createProcess();
Thread.currentThread().sleep(500);
}

void createProcess()
{
Thread t=new Thread(new clientread());
t.start();
}


void makeCon()
{
try
{
ss=new ServerSocket(3500);
s=ss.accept();
br=new BufferedReader(new InputStreamReader(s.getInputStream()));
pw=new PrintWriter(s.getOutputStream());
System.out.println("Connection Established With client");
}
catch(Exception e)
{
}

}

class sending implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
try
{
pw.println(ta.getText());
pw.flush();
ta.setText("");
ta.requestFocus();
}
catch(Exception e)
{
}

}
}



class clientread implements Runnable
{
public void run()
{
String s;
try
{

while((s=br.readLine())!=null)
{
tb.append(s+"\n");
}

}
catch(Exception e)
{
}

}

}



}
 
Master Rancher
Posts: 3883
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, you have to put the accept() method call in a forever loop. When accept() returns with a new connection, you have to spawn a new thread to handle that connection and then loop back to the accept() to wait for the next connection.
Most of your global variables will have to be moved to the new threaded class.
[ June 23, 2008: Message edited by: Norm Radder ]
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to start a new thread for each client.
In server you have the code like this Socket client=ss.accept();
you write a class as Servicethread that extends the thread class
when ever the client is connected to server you start the client with
ServiceThread st=new ServiceThread(client);
st.start();
And the rest of the code is upto you.what you are going to with that client
socket.
    Bookmark Topic Watch Topic
  • New Topic