• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to communicate between one server and multiple clients

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
{
}

}

}



}
 
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I child proofed my house but they still get in. Distract them with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic