Java Networking
Lecture 26
Multiple Clients
Multithreaded Server
The multithreaded server program creates a new thread
for every client request.
This way each client has its own connection to the server
for passing data back and forth.
Basic Flow
The basic flow of logic
while (true) {
accept a connection ;
create a thread to deal with the client ;
end while
}
Server Side
public
bli class
l
serverside
id {
ServerSocket server;
static JTextArea textArea;
public serverside(){
listenSocket(); //method that accepts the connection and creates the client
}
public static void main(String argv[]) throws Exception {
JF
JFrame
frame=new
f
JFrame();
JF
()
textArea=new JTextArea();
frame.add(textArea);
f
frame.setSize(300,300);
Si (300 300)
frame.setVisible(true);
serverside ss=new serverside();
}
public void listenSocket()
{ try{
server = new ServerSocket(4444); }
catch (IOException e){
S t
System.out.println("Could
t i tl ("C ld nott lilisten
t on portt 4444");
4444")
System.exit(-1); }
while(true){
ClientWorker w;
System.out.println("server is listening for connection");
textArea.append("server listening");
try{
//server.accept returns a client connection
w = new ClientWorker(server.accept(), textArea);
Thread t = new Thread(w);
t t t() }
t.start();
catch (IOException e) {
System.out.println("Accept failed: 4444"); System.exit(-1); }
}
}//end of listenSocket method
class ClientWorker implements Runnable
{ private Socket clientcon;
private JTextArea textArea;
ClientWorker(Socket client, JTextArea textArea) {
thi li t
this.clientcon
= client;
li t
this.textArea = textArea;
}
public void run(){
String line;
BufferedReader in = null;
PrintWriter out = null;
try{
in = new BufferedReader(new InputStreamReader(clientcon.getInputStream()));
out = new PrintWriter(clientcon.getOutputStream(), true);
}
catch (IOException e) {
System.out.println("in or out failed"); System.exit(-1); }
while(true){
hil (t ){
try{
line = in.readLine();
textArea append(line); //Append
textArea.append(line);
data to text area
}catch (IOException e) {
System.out.println("Read failed");
System.exit(-1);
} }}}
Client: Example
public class clientside implements
p
p
ActionListener
{
Socket socket;
PrintWriter out;
JButton bt;
JTextArea ta;
public clientside(){
JFrame jf=new JFrame();
bt=new JButton("send");
ta=new JTextArea("write something to send");
bt addActionListener(this);
bt.addActionListener(this);
jf.setLayout(new BorderLayout());
jf.add(bt,BorderLayout.SOUTH);
j
jf.add(ta,BorderLayout.CENTER);
(
y
)
jf.setSize(300,300);
jf.setVisible(true);
listenSocket();
}
public static void main (String[] arg){
clientside cs=new clientside();
}
BufferedReader in;
Example: Client
public
bli void
id listenSocket(){
li t S k t(){
//Create socket connection
try{
socket
k t = new Socket(localhost",
S k t(l
lh t" 4444);
4444)
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader( socket.getInputStream()));
}
catch (UnknownHostException e) {
System.out.println("Unknown host: ");
S t
System.exit(1);
it(1)
} catch (IOException e) {
System.out.println("No I/O");
S
System.exit(1);
i (1)
}
}
Example: Client
public void actionPerformed(ActionEvent event){
Object source = event.getSource();
if(source == button){
//Send data over socket
String text = textField.getText();
out.println(text);
textField.setText(new String(""));
}
/}
socket.close();
}
The run method executes independently in each thread
thread.
If three clients request connections, three ClientWorker
instances are created, a thread is started for each
ClientWorker instance, and the run method executes for
each thread.
The JTextArea.append
JTextArea append method is thread safe.
safe
If the JTextArea.append method were not thread safe.
public synchronized void appendText(line){
textArea.append(line); }
UDP
Java's support for UDP is contained in two
classes:
java.net.DatagramSocket
java.net.DatagramPacket
j
g
A datagram socket is used to send and receive
datagram
g
p
packets.
Datagram
Some applications that you write to communicate over the
network will not require the reliable, point-to-point channel
provided by TCP. Rather, your applications might benefit
from a mode of communication that delivers independent
packages of information.
The DatagramPacket and DatagramSocket classes in the
java net package implement system-independent
java.net
system independent datagram
communication using UDP.
Datagram packet
A datagram is an independent, self-contained message
sent over the network whose arrival,
arrival arrival time
time, and
content are not guaranteed
Datagram Socket
public DatagramSocket() throws SocketException
public DatagramSocket(int
p
g
( p
port)) throws SocketException
p
public DatagramSocket(int port, InetAddress laddr) throws SocketException