Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Sockets programming

  Asked By: Hamish    Date: Jun 12    Category: Java    Views: 804
  

I need guidance in Sockets programming

I have one ServerSocket which creates a socket client request and the
communication goes on. But if we consider more than one client then how will you
handel on the server side so that ser ver will respond to all the clients
without disturbing each other.

Share: 

 

7 Answers Found

 
Answer #1    Answered By: Gorkem Yilmaz     Answered On: Jun 12

use a multi-threaded server. Each single thread can accept() one
connection and handle it. For an example, have a look at the archives
of beginnersclub (it's also about Java), Jacob Skillin
had some interesting questions about this subject.
How you coordinate access to common resources (such as configuration
files and so on) is a different story. For this sort of tasks you'll
probably need some synchronized pieces of code.

 
Answer #2    Answered By: May Hansen     Answered On: Jun 12

I have a problem too with regards to socket  particularly streams, i
don't know why everytime I call on the reader readline function it
actually waits till my socket times out just then it displays the data
on the screen (becoz i print it out on my test server)

someone told me to take out the data using bytes and display it to
screen, i don't know how to do this, can anyone help me on this?

 
Answer #3    Answered By: Abana Cohen     Answered On: Jun 12

don't use the readline() method in your case. This method only returns
to the caller when an end-of-line character has been read. This
presumes that you're reading a text file but most probably won't help
with any other kind of file (or communication channel in general).
Use the read() methods associated with single bytes or an array of
bytes if you know how many bytes you have to read exactly. But in
order to give some more detailed advice you would first have to tell
us more about your project and what you've done so far.

The easiest thing for you (in my opinion) is to browse the Javadoc of
java.io and/or java.net packages and all their methods. Then you'll
get the quickest survey of what can be done how and with what methods.
For example, usually I open a TCP/IP channel with the classes Socket
and ServerSocket; afterwards I retrieve the input stream and output
stream associated with that socket  (using methods getInputStream() and
getOutputStream() of class Socket), and then use the read() and
write() methods of these streams to exchange data with sockets.

 
Answer #4    Answered By: Lu Fischer     Answered On: Jun 12

My project involves using a machine that communicates via TCP/IP, what
this machine does is it sends out packets that aren't human readable so,
I made a class that could convert them into human readable hex and
displays them in the screen. but the problem arises when I try to run a
packet, the machine timed out (i guess your right there) and that's the
time my human readble packet was displayed.

I'll try to analyze my program further and keep you guys updated. Thank
you very much

 
Answer #5    Answered By: Alfonso Martin     Answered On: Jun 12

U can get Socket's InputStream & read byte-by-byte... i hv attached the portion
to read byte-by-byte & to print on screent... i hope u can make it other
portions, if u can't do reply i will give u the complete programs..

BufferedInputStream bin = new BufferedInputStream(s.getInputStream());
int n=-1;
byte[] b = new byte[5000];
while((n=bin.read(b,0,b.length))!=-1)
{
for(int i=0;i<n;i++)
{
System.out.print((char)b[i]);
}
}
bin.close();
s.close();

 
Answer #6    Answered By: Amir Shaikh     Answered On: Jun 12

i'll try to incorporate this into my java file,
i'll notify you of the status. thanks again

 
Answer #7    Answered By: Abriana Rossi     Answered On: Jun 12

The ServerSocket listens continuously in a thread.
Whenever a client  is connected a new thread a spawned which handles
the communication.
So, you will have to use multithreading. If you do not want to
multithread your application then may be you'll have to use the
SocketChannels etc. from the nio pkg.

If the above informaton is not enough, drop in a message...

 
Didn't find what you were looking for? Find more on Sockets programming Or get search suggestion and latest updates.




Tagged: