Reputation: 1
I have done one server and one client communication by using ip address but am stuck with one server multiple communication
s=new ServerSocket(77);
ss=s.accept();
icon.displayMessage("New message for you", "Please click here", TrayIcon.MessageType.WARNING);
os=ss.getOutputStream();
ps=new PrintStream(os);
is=ss.getInputStream();
br=new BufferedReader(new InputStreamReader(is));
ps.println(st);
}
catch(Exception e)
{}
on client side
try
{
ss=new Socket(ip,77);
}
catch(Exception e){
}
is=ss.getInputStream();
br=new BufferedReader(new InputStreamReader(is));
os=ss.getOutputStream();
ps=new PrintStream(os);
ps.println(msg+" : "+st1);
Upvotes: 0
Views: 69
Reputation: 4064
You can do something like:
while (true){
s=new ServerSocket(77);
ss=s.accept();
Thread at = new Thread(ss);
at.start();
}
Then the communication to the client happens in the run-method of 'at'.
Upvotes: 2
Reputation: 136112
you should run each session in a separate Thread, like this:
static class Session extends Thread {
Socket s;
Session(Socket s) {
this.s = s;
}
@Override
public void run() {
try {
OutputStream os = s.getOutputStream();
// your code
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public static void main(String[] args) throws Exception {
ServerSocket s = new ServerSocket(77);
for (;;) {
Socket ss = s.accept();
new Session(ss).start();
}
}
This code is just to explain the idea.
Upvotes: 2