java - Sending requests and handling multiple threads talking to each other over sockets without interference -


i'm creating java program, based on client-server model in need server , client(s) communicate.

more need them "give orders" each other or request things, , transfer data inside orders/requests, can see i'm trying achieve looks method, instead of happening locally, "methods" called 1 side , executed other.

for instance possible scenario is:

  1. the server listening orders
  2. the client tries authenticate , sends username , password server
  3. the server receives authentication request, controls whether username , password correct , reports client
  4. the client authenticated
  5. the server once again listening orders

now, first idea use datainputstream , dataoutputstream, sending requests under form of strings, instance, if wanted client request authentication this:

dataoutputstream dos = new dataoutputstream(socket.getoutputstream()); dos.writeutf("requestauth"); dos.writeutf(username); dos.writeutf(password); 

and server:

datainputstream dis = new datainputstream(socket.getinputstream()); if(dis.readutf().equals("requestauth")){     string username = dis.readutf();     string password = dis.readutf();     //check whether they're correct or not bla bla bla }  

this doesn't seem best option me, think there's better ways don't know how, searched better solution found nothing.

another problem arised need both server , client able send requests simultaneously , these requests can sent anytime, asynchronously.

for instance: let's client authenticating, it's sending username, password, etc... meanwhile server wants check if client still connected because thread doing every second, how can make sure each information delivered in right place without stream clogging or threads receiving unwanted data? if client waiting know whether username , password correct, don't want clog request of other thread that's doing different.

basically: client/server program can multithreaded, can sockets without things getting messy? can have thread servera communicating thread clienta, , thread serverb communicating thread clientb without interferring each other? can have multiple streams same socket , work on them separately?


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -