Basic HTML version of Foils prepared Sept 21 1998

Foil 49 Sockets from the Server Side

From Java Tutorial 98- 4: Multi-Treading, Useful Java Classes, I/O and Networking NAVO Tutorial -- Sept 23 1998. by Geoffrey C. Fox, Nancy McCracken

Socket Server examples for Foil 49
A Java application can open a single Socket on a port and open an InputStream and OutputStream just like the client, but in general, a server will want to be able to open multiple sockets to establish communication with a number of clients at the same time.
The ServerSocket class allows multiple sockets:
  • ServerSocket s = new ServerSocket(port);
Then the Java server application can use the accept method to open a socket for each client which tries to open a socket on the port:
  • Socket client = s.accept();
Typically, a server main program would be waiting to accept a client. When a client tries to establish a socket, the accept proceeds. Then the server would spawn a thread to handle that one socket, opening input and output streams as necessary and communicating with the client.



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sat Nov 28 1998